Forward reference member functions?

I’d like to forward reference members functions, but haven’t been able to figure out how to go about it. I have an ‘actor’ module that creates a sprite, and contains a number of member functions to manipulate that sprite:

[lua]local DogginsSprite = display.newSprite(imageSheet, sequenceData)

function DogginsSprite.move (x, y)
– move the sprite
end[/lua]

Some of the member functions reference each other, and for easier organization I’d like to forward reference them so the order of the functions doesn’t matter. I tried something like this:

[lua]local DogginsSprite = display.newSprite(imageSheet, sequenceData)

DogginsSprite.idle
DogginsSprite.move
DogginsSprite.rest

function DogginsSprite.move (x, y)
– move the sprite
end[/lua]

…but I get an error saying that an ‘=’ is expected after the variable names. Normally I would declare those with ‘local’, but that doesn’t work, I assume because they are already local to DogginsSprite.

Is there a way to do this?

Thanks!

  • David [import]uid: 149111 topic_id: 35758 reply_id: 335758[/import]

Lua has a couple of different ways to define a function:

local forwardFunction  
  
local function secondFunction()  
 forwardFunction()  
end  
  
forwardFunction = function(some,parameters)  
 -- forward function code  
end  

[import]uid: 199310 topic_id: 35758 reply_id: 142243[/import]

@Rob Miracle: Thanks for the reply! I understand the basic idea of Forward Referencing, but am running into trouble when doing it with member functions. I suppose I could do something like this:

[lua]local DogginsSprite = display.newSprite(imageSheet, sequenceData)

local idle
local move
local rest

DogginsSprite.idle = idle
DogginsSprite.move = move
DogginsSprite.rest = rest

idle = function(x, y)
rest()
end

move = function(x, y)
– move the sprite
end

rest = function(x,y)
– change sprite animation
end
[/lua]

It’s just more cumbersome than I was hoping for, and involves creating double the number of variables I would need otherwise. [import]uid: 149111 topic_id: 35758 reply_id: 142600[/import]

Lua has a couple of different ways to define a function:

local forwardFunction  
  
local function secondFunction()  
 forwardFunction()  
end  
  
forwardFunction = function(some,parameters)  
 -- forward function code  
end  

[import]uid: 199310 topic_id: 35758 reply_id: 142243[/import]

@Rob Miracle: Thanks for the reply! I understand the basic idea of Forward Referencing, but am running into trouble when doing it with member functions. I suppose I could do something like this:

[lua]local DogginsSprite = display.newSprite(imageSheet, sequenceData)

local idle
local move
local rest

DogginsSprite.idle = idle
DogginsSprite.move = move
DogginsSprite.rest = rest

idle = function(x, y)
rest()
end

move = function(x, y)
– move the sprite
end

rest = function(x,y)
– change sprite animation
end
[/lua]

It’s just more cumbersome than I was hoping for, and involves creating double the number of variables I would need otherwise. [import]uid: 149111 topic_id: 35758 reply_id: 142600[/import]

you only need a table variable
a sprite is a table

local DogginsSprite = display.newSprite(imageSheet, sequenceData)  
DogginsSprite.idle=function(x,y)  
 DogginsSprite.rest(x,y)  
end  
DogginsSprite.move=function(x,y)  
 --move code   
end  
DogginsSprite.rest=function(x,y)  
 --rest code  
end  

[import]uid: 6459 topic_id: 35758 reply_id: 145018[/import]

@tetu: Thanks for the reply! I’m a bit confused, though. Won’t this generate an error, as DogginsSprite.rest doesn’t exist before the DogginsSprite.rest call in line 3? [import]uid: 149111 topic_id: 35758 reply_id: 145139[/import]

@david, you are welcome
if we call idle right below line 4 we will get an error because rest is nil at that point
if we call idle anywhere below line 10 we won’t get any errors because both idle and rest have been defined earlier
[import]uid: 6459 topic_id: 35758 reply_id: 145162[/import]

you only need a table variable
a sprite is a table

local DogginsSprite = display.newSprite(imageSheet, sequenceData)  
DogginsSprite.idle=function(x,y)  
 DogginsSprite.rest(x,y)  
end  
DogginsSprite.move=function(x,y)  
 --move code   
end  
DogginsSprite.rest=function(x,y)  
 --rest code  
end  

[import]uid: 6459 topic_id: 35758 reply_id: 145018[/import]

@tetu: Thanks for the reply! I’m a bit confused, though. Won’t this generate an error, as DogginsSprite.rest doesn’t exist before the DogginsSprite.rest call in line 3? [import]uid: 149111 topic_id: 35758 reply_id: 145139[/import]

@david, you are welcome
if we call idle right below line 4 we will get an error because rest is nil at that point
if we call idle anywhere below line 10 we won’t get any errors because both idle and rest have been defined earlier
[import]uid: 6459 topic_id: 35758 reply_id: 145162[/import]

you only need a table variable
a sprite is a table

local DogginsSprite = display.newSprite(imageSheet, sequenceData)  
DogginsSprite.idle=function(x,y)  
 DogginsSprite.rest(x,y)  
end  
DogginsSprite.move=function(x,y)  
 --move code   
end  
DogginsSprite.rest=function(x,y)  
 --rest code  
end  

[import]uid: 6459 topic_id: 35758 reply_id: 145018[/import]

@tetu: Thanks for the reply! I’m a bit confused, though. Won’t this generate an error, as DogginsSprite.rest doesn’t exist before the DogginsSprite.rest call in line 3? [import]uid: 149111 topic_id: 35758 reply_id: 145139[/import]

@david, you are welcome
if we call idle right below line 4 we will get an error because rest is nil at that point
if we call idle anywhere below line 10 we won’t get any errors because both idle and rest have been defined earlier
[import]uid: 6459 topic_id: 35758 reply_id: 145162[/import]

@tetu: Thanks that makes sense. I appreciate the help! [import]uid: 149111 topic_id: 35758 reply_id: 145373[/import]

you only need a table variable
a sprite is a table

local DogginsSprite = display.newSprite(imageSheet, sequenceData)  
DogginsSprite.idle=function(x,y)  
 DogginsSprite.rest(x,y)  
end  
DogginsSprite.move=function(x,y)  
 --move code   
end  
DogginsSprite.rest=function(x,y)  
 --rest code  
end  

[import]uid: 6459 topic_id: 35758 reply_id: 145018[/import]

@tetu: Thanks for the reply! I’m a bit confused, though. Won’t this generate an error, as DogginsSprite.rest doesn’t exist before the DogginsSprite.rest call in line 3? [import]uid: 149111 topic_id: 35758 reply_id: 145139[/import]

@david, you are welcome
if we call idle right below line 4 we will get an error because rest is nil at that point
if we call idle anywhere below line 10 we won’t get any errors because both idle and rest have been defined earlier
[import]uid: 6459 topic_id: 35758 reply_id: 145162[/import]

@tetu: Thanks that makes sense. I appreciate the help! [import]uid: 149111 topic_id: 35758 reply_id: 145373[/import]

Normally I put all logic and initialisation of objects INSIDE functions. Put all that stuff inside an init() function at the top. But don’t call init() until the very last line of code. That way everything is always forwarded. Make sense?
Basically make the last line execute the very first instruction.

i.e

[lua]function init()

    doSomething()
    doSomethingElse()

end

local function  doSomething()
    print ( “doSomething” )
end

local function  doSomethingElse()
    print ( “doSomethingElse” )
end

init()[/lua]