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.
How correctly use "self" + colon
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: How correctly use "self" + colon
Very easy. You almost need nothing to do self and use : operator. Just declaring the tableKrizzu 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.
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
Code: Select all
function t.func(self, x, y)
self.x = x
self.y = y
end
t.func(t, 1, 1)
Last edited by coffee on Wed Sep 12, 2012 7:34 pm, edited 2 times in total.
Re: How correctly use "self" + colon
Small typo, fixedcoffee wrote:Very easy. You almost need nothing to do self and use : operator. Just declaring the tableKrizzu 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.
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:The definition and call translate to:Code: Select all
t = {} function t:func(x, y) self.x = x self.y = y end t:func(1, 1) print(t.x) -- 1
Code: Select all
function t.func(self, x, y) self.x = x self.y = y end t.func(t, 1, 1)
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Re: How correctly use "self" + colon
Ah thanks. I didn't noticed that "t" was not code tagged! Fixed!Nixola wrote:Small typo, fixed
And dude, your avatar keeps me confusing! lol
Re: How correctly use "self" + colon
If I got function
And I will add table X
and then
Will it print 1?
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
Code: Select all
X={}
Code: Select all
X:func(1, 1)
print(X.x)
Re: How correctly use "self" + colon
No, because X:func() doesn't exist yet; you still have to declare it
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Re: How correctly use "self" + colon
As Nixola said.
Check this now:
http://www.lua.org/pil/16.html
and then
http://www.lua.org/pil/16.1.html
and eventually
http://www.lua.org/pil/16.2.html
Check this now:
http://www.lua.org/pil/16.html
and then
http://www.lua.org/pil/16.1.html
and eventually
http://www.lua.org/pil/16.2.html
Who is online
Users browsing this forum: Amazon [Bot], Bing [Bot] and 15 guests