passing self as an argument..
Posted: Tue Jul 28, 2009 12:21 pm
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;
ofcourse i've used background to create the background_object object (they share the metatable)
edit:
solved it! 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
when i do self:moveX(5) etc it works fine now.
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
edit:
solved it! 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
when i do self:moveX(5) etc it works fine now.