Hi
.
I'm new to Lua and I have some difficulties in understanding Classes/OOP in Lua.
I'm just trying to create a simple "player"-class. This is what I have so far:
--[[
filename : Player.lua
--]]
Player = Core.class()
function Player:init()
self.health = 100
self.speed = 3
end
function Player:startRunning()
end
function Player:stopRunning()
end
I create the instance and call a method like this:
require ("Class/Player")
local player = Player.new()
player.startRunning()
Is this the right approach? How do I declare local variables like health and access them from outside the class?
I've read several articles about this issue but after that I'm more confused than before
. There are many different oppinions out there.
Another thing. What's the point with the . and : syntax? I have seen some methods/functions like this
function object.print(self)
print("Hello from ", self)
end
What's the difference between function object.print(self) and function object:print(self)?
Comments
question about . and : here it is: object:method() - calls object method and object.property - access to objects property.
When you use the double dot ( : ) method, the lua interpreter actually puts "self" as a hidden first parameter so
You can call the above function in two ways
If the above has completely confused you then just remember it like this.
For accessing variables stored in a table use ( . )
For defining and calling functions stored in a table use ( : )
You can then always use "self" within a function to refer to the current "instance" of the class for accessing variables and related functions.
Likes: gyrosp
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
But I have two more questions left ...
1. Why don't I need
2. So I don't need to declare my class variables in Lua? I just write self.myVar in my class, this is all?
1. Yes, unless you specifically 'Exclude from execution" your file (right click on your file in the project hierarchy)
2. The following works :
Likes: aimoi
If you "exclude" a file from execution, then it won't be run and Lua will not know anything about your file ( and will then need "requiring" )
2. When Lua run's "self.foo = 10" it''s actually setting the variable "foo" in the "current" table to equal "10", if that variable doesn't exist then it's added to the table, so effectively by running that statement you are both "declaring" and initialising the variable in a single step.
Because your working on "self" ie the "instance" of the table that's created when you call Class.new() then each "instance" of the table will be initialised to contain the same variables.
You mentioned "class" variables, in this instance I'm talking about "instance" variables - one's where each object "instance" has their own separate copy. I would normally use the term "class" variable to mean a (in C++ terms) static variable, one that is shared across all instances of a class. You can create a static class variable by simply declaring a variable as being "local" at the top of the file, this will also mean it can only be accessed from within the file (which effectively makes it "private" )
see the following...
So to use this in main you could write.
Likes: gyrosp, plamen
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
This is usually the number 1 source of "bugs" in Lua apps! - remember what @plamen said.
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
I stumbled about another thing:
I have read a post about class inheritance with gideros (http://www.giderosmobile.com/documentation/classes_in_gideros.html) but have some issues I didn't get.
I have following inheritance:
Edit:
What is the tag I need to use to format my code like techdojo did ?
If you want to display your code, you can use < pre lang="lua" > Your code </ pre>
(remember to clear blank on tags)
Any idea why the inheritance is not working?
Likes: vitalitymobile
when you call the
return self
cheers,
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
Can you create a test file? No doubt that your problem will be solved in no time.
Looking at all the answers you got above, isn't this community amazing?
I'm sorry but I installed Gideros just today. What do you mean with test file? My project source files?
you create a very simplified version of your two classes + main.lua so we can see better where the problem is.
You just have to duplicate your project and remove unnecessary files.
Right click to SlowZombie and add Code Dependencies (Check on ZombieBase)
And ... 'Gideros-Magic' will happen :P
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill