Page 1 of 1
How correctly use "self" + colon
Posted: Wed Sep 12, 2012 7:15 pm
by Krizzu
Hi
It's me again with newbie questions/requests.
I'm wondering if is there anyone who can explain in the simples possible way how to correctly use "self" in functions and mighty Colon.
I readed bunch of guides, tried to do it myself but it seems I didnt get it well.
Re: How correctly use "self" + colon
Posted: Wed Sep 12, 2012 7:30 pm
by coffee
Krizzu wrote:Hi
It's me again with newbie questions/requests.
I'm wondering if is there anyone who can explain in the simples possible way how to correctly use "self" in functions and mighty Colon.
I readed bunch of guides, tried to do it myself but it seems I didnt get it well.
Very easy. You almost need nothing to do self and use : operator. Just declaring the table
From blackbullet excelent tutorial
http://nova-fusion.com/2012/09/07/lua-f ... rs-part-3/
self
Lua gives us a neat piece of syntactic sugar for calling and defining functions. When you have a function inside a table, you can do stuff like this:
Code: Select all
t = {}
function t:func(x, y)
self.x = x
self.y = y
end
t:func(1, 1)
print(t.x) -- 1
The definition and call translate to:
Code: Select all
function t.func(self, x, y)
self.x = x
self.y = y
end
t.func(t, 1, 1)
Re: How correctly use "self" + colon
Posted: Wed Sep 12, 2012 7:32 pm
by Nixola
coffee wrote:Krizzu wrote:Hi
It's me again with newbie questions/requests.
I'm wondering if is there anyone who can explain in the simples possible way how to correctly use "self" in functions and mighty Colon.
I readed bunch of guides, tried to do it myself but it seems I didnt get it well.
Very easy. You almost need nothing to do self and use : operator. Just declaring the table
From blackbullet excelent tutorial
http://nova-fusion.com/2012/09/07/lua-f ... rs-part-3/
self
Lua gives us a neat piece of syntactic sugar for calling and defining functions. When you have a function inside a table, you can do stuff like this:
Code: Select all
t = {}
function t:func(x, y)
self.x = x
self.y = y
end
t:func(1, 1)
print(t.x) -- 1
The definition and call translate to:
Code: Select all
function t.func(self, x, y)
self.x = x
self.y = y
end
t.func(t, 1, 1)
Small typo, fixed
Re: How correctly use "self" + colon
Posted: Wed Sep 12, 2012 7:36 pm
by coffee
Nixola wrote:Small typo, fixed
Ah thanks. I didn't noticed that "t" was not code tagged! Fixed!
And dude, your avatar keeps me confusing! lol
Re: How correctly use "self" + colon
Posted: Wed Sep 12, 2012 7:45 pm
by Krizzu
If I got function
Code: Select all
t = {}
function t:func(x, y)
self.x = x
self.y = y
end
t:func(1, 1)
print(t.x) -- 1
And I will add table X
and then
Will it print 1?
Re: How correctly use "self" + colon
Posted: Wed Sep 12, 2012 7:56 pm
by Nixola
No, because X:func() doesn't exist yet; you still have to declare it
Re: How correctly use "self" + colon
Posted: Wed Sep 12, 2012 8:01 pm
by coffee