passing self as an argument..

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Pliskin09
Citizen
Posts: 89
Joined: Fri Jul 24, 2009 8:30 am

passing self as an argument..

Post by Pliskin09 »

right now i have a background object. i want to do a list of things to do each update(dt) event love calls.

so i say background_object:update()

but the update function for the background object needs to use more of the objects functions. for example, i want to move the background with background_object:moveX(5,10) ( ignore the fact that in this example i can solve the problem by just merging the update and move functions....this is just an example) . background_object inside the update function is "self", right? because if i wanted to get a variable out of the background_object object, i'd do self.var. so why cant i use functions like this? if i do self.moveX(5,10) i get an error. :(

to elaborate a little bit more, heres a snippit of the problem;

Code: Select all

background_object:update()

function background:update() 
   xToMove = self.x + 5 -- works fine
   self.moveX(xToMove) -- nope..
end

function background:moveX(move)
   self.x = move
end
ofcourse i've used background to create the background_object object (they share the metatable)


edit:

solved it! :ultraglee: sorry. for those who also run into the problem;

i forgot that ':' was used to pass the extra self variable to functions when you call the function from one of your objects :cry:

when i do self:moveX(5) etc it works fine now.

:)
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 240 guests