How correctly use "self" + colon

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Krizzu
Prole
Posts: 20
Joined: Sun Apr 15, 2012 8:02 pm

How correctly use "self" + colon

Post 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.
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: How correctly use "self" + colon

Post 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)
Last edited by coffee on Wed Sep 12, 2012 7:34 pm, edited 2 times in total.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: How correctly use "self" + colon

Post 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
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: How correctly use "self" + colon

Post 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
User avatar
Krizzu
Prole
Posts: 20
Joined: Sun Apr 15, 2012 8:02 pm

Re: How correctly use "self" + colon

Post 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

Code: Select all

X={}
and then

Code: Select all

X:func(1, 1)
print(X.x)
Will it print 1?
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: How correctly use "self" + colon

Post by Nixola »

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
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: How correctly use "self" + colon

Post by coffee »

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 4 guests