0 Members and 5 Guests are viewing this topic.
Quote from: Jonius7 on July 26, 2013, 12:43:42 amCode: [Select]foo = Foo()Is this really needed?If you're asking this question, you may not know that Lua is case sensitive.Here, Foo is the class (or the "template to copy") and foo is the new object (or an "instance of this class").Foo() is equivalent to "new Foo()" in some other POO languages.Foo() calls Foo:init().
Code: [Select]foo = Foo()Is this really needed?
foo = Foo()Is this really needed?
Foo():bar(42)
I am aware that Lua is case sensitive, but can Foo() just be used directly in conjunction with . and :eg: Code: [Select]Foo():bar(42)Actually I'm not sure that looks valid...
when you do foo = Foo(), that's similar to something like ball = Class() right?It clicked for me when you said that Foo() was the class, and foo became the object.
About creating new instances with Foo():bar(42),does that have any other disadvantages, such as some sort of memory leak?
edit 2:FINNALY!!!, i made it!i just need var.store() all vars and its done.Thank you guys i love you all!!
yi=math.eval("nSolve(fl=fr,yi)")
ball = class() -- creates a class called ball using the class function that is predefinedfunction ball:init(speed, size) -- initialises the class, it can be called by just ball() elsewhere in the code self.speed = spd -- these are added to the table, ball? self.size = sizeend
So clarification,Code: [Select]ball = class() -- creates a class called ball using the class function that is predefinedfunction ball:init(speed, size) -- initialises the class, it can be called by just ball() elsewhere in the code self.speed = spd -- these are added to the table, ball? self.size = sizeend
Ball = class()
ballarray = {{3,4},{6,10},{4,11}...}
self.speed = spd
PointClass = class()function PointClass:init(x, y) self.x = x self.y = yendBallClass = class(PointClass)function BallClass:init(x, y, radius) PointClass.init(self, x, y) self.radius = radiusendfunction BallClass:paint(gc) gc:fillArc(self.x, self.y, self.radius*2, self.radius*2, 0, 360)end
local ball1, ball2ball1 = BallClass(0, 0, 10)ball2 = BallClass(50, 50, 15)ball_tbl = {ball1, ball2}function on.paint(gc) for _, ball in ipairs(ball_tbl) do ball:paint(gc) endend
PointClass.init(self, x, y)
PointClass:init(x, y)
for _, ball in ipairs(ball_tbl) do
for _, values in ipairs(ball_tbl) do --Do something hereend
Code: [Select]PointClass.init(self, x, y)What does the first argument (self) represent in relation to PointClass?as compared toCode: [Select]PointClass:init(x, y)
":" is just a shortcut for a.b(self)
Ah, I'm making stupid mistakes again, I go back and look at it again and see that that line is within the function BallClass:init(x, y, radius)Oh of course, I get what ipairs is now. Takes all the indices in numbered order (ones that can be ordered anyway), and has the indicies and keys from the table in pairs. Ok.Oh wow I didn't know that _ could be used as a variable. Kinda like a wild card. Is it?Spoiler For This question isn't so important, the more pressing stuff is below: so can _ be used mixed like how you would search using * in other languagesEg: you have variables bark, bork, berkcan you look for b_rk somehow?
local _o = 42for _=1, _o do print(_, _+1, _o/_)end
Quote":" is just a shortcut for a.b(self)So PointClass:init(x, y) is a shortcut for PointClass.init(PointClass, x, y)I'm trying to work through in my mind the connections between all this stuff, and I still don't quite get it. I even drew a diagram, draft version, probably some mistake in there .To describe what i'm not getting, it's the syntax that's getting me. I'm having trouble understanding the meaning of certain parts of the syntax lua classes use.
Toto = class()function Toto:init() print(self)endToto:init() -- equivalent to Toto.init(Toto)lol = Toto() -- equivalent to Toto.init(temp) where "temp" will be stored in "lol"lal = lol.init({}) -- here we force "self" to be "{}", an empty table
How does that translate to something in a table?
Bar = class(Foo)