calling functions in storyboard.api

I’m using the storyboard, and when Im in the enterScene part to call functions it is not calling.
The function is at line 81(level1Presents()). I thought it would call itself, and I tried calling it elsewhere. to no avail… The program will run right up to that point and print ‘1:enterScene event’

[lua]----------------------------------------------------------------------------------

– scenetemplate.lua


local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
local p = 0
local bg1, floor1, santa, triangleShape, lever, present1, present2, present3, dropButton

local phys1 = {friction = .7, bounce = .1, density = .3}
local h = display.contentHeight
local w = display.contentWidth

physics = require(‘physics’)
physics.start()
physics.setGravity(0,9.3)–gravity 3-5 good
–physics.setDrawMode(‘hybrid’)



– NOTE:

– Code outside of listener functions (below) will only be executed once,
– unless storyboard.removeScene() is called.



– BEGINNING OF YOUR IMPLEMENTATION

– Called when the scene’s view does not exist:
function scene:createScene( event )
local group = self.view

bg1 = display.newImage(‘bg1.jpg.’)
group:insert(bg1)

floor1 = display.newRect(0, h-1, w, 1)
floor1.name = ‘floor1’
physics.addBody(floor1, “static”,{density = 9, friction = .7, bounce = .2})
–floor1:addEventListener(‘collision’, onCollision)
group:insert(floor1)

santa = display.newImageRect(‘santa.jpg’, 100, 100)
santa.x = w/2
santa.y = h-40
triangleShape = { 0,-50, 50,50, -50,50 }
physics.addBody( santa, {density=9, friction=0.5, bounce=0, shape=triangleShape } )
group:insert(santa, triangleShape)

lever = display.newRect(w/2-350, h-125, 700, 30)
lever:setFillColor(0, 230, 29)
lever.name = ‘lever’
physics.addBody(lever,‘dynamic’, {density = 3, friction = .7, bounce = 0})
myJoint = physics.newJoint(‘pivot’, santa, lever, w/2, h-140)
group:insert(lever, myJoint)


– CREATE display objects and add them to ‘group’ here.
– Example use-case: Restore ‘group’ from previously saved state.


end

– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
–local group = self.view
print( “1: enterScene event” )
–level1Presents()

local function level1Presents()
p = p+1
print §
if (p ==1) then
present1 = display.newImageRect(‘present1.jpg’, 50, 50)
physics.addBody(present1, ‘kinematic’, phys1)
present1.x = w/2
present1.y = h/7
present1:addEventListener(‘touch’, onTouch)

elseif (p == 2) then

present2 = display.newImageRect(‘present2.gif’,50, 50)
physics.addBody(present2, ‘kinematic’, phys1)
present2.x = w/2
present2.y = h/6
present2:addEventListener(‘touch’, onTouch)

elseif (p == 3) then

present3 = display.newImageRect(‘present3.gif’, 50, 50)
physics.addBody(present3, ‘kinematic’, phys1)
present3.x = w/2
present3.y = h/6
present3.rotation = 5
present3:addEventListener(‘touch’, onTouch)
end

end
end [import]uid: 75779 topic_id: 19583 reply_id: 319583[/import]

Where do you ever call level1Presents()

When the enterScene event fires, it prints your string and then has nothing else to do. You define a function in it, but you never call it.

[import]uid: 19626 topic_id: 19583 reply_id: 75663[/import]

Yes you are exactly correct. I define it but I cannot call it. Notice line 79 I commented out because I tried calling it there and it gave me error.

Where do I call it? [import]uid: 75779 topic_id: 19583 reply_id: 75701[/import]

Try this:

-- Called immediately after scene has moved onscreen:  
function scene:enterScene( event )  
 --local group = self.view  
 print( "1: enterScene event" )  
  
 local function level1Presents()  
 p = p+1  
 print (p)  
 if (p ==1) then  
 present1 = display.newImageRect('present1.jpg', 50, 50)  
 physics.addBody(present1, 'kinematic', phys1)  
 present1.x = w/2  
 present1.y = h/7  
 present1:addEventListener('touch', onTouch)  
  
 elseif (p == 2) then  
   
 present2 = display.newImageRect('present2.gif',50, 50)  
 physics.addBody(present2, 'kinematic', phys1)  
 present2.x = w/2   
 present2.y = h/6  
 present2:addEventListener('touch', onTouch)  
  
 elseif (p == 3) then  
  
 present3 = display.newImageRect('present3.gif', 50, 50)  
 physics.addBody(present3, 'kinematic', phys1)  
 present3.x = w/2   
 present3.y = h/6  
 present3.rotation = 5  
 present3:addEventListener('touch', onTouch)  
 end   
   
 end  
  
 level1Presents()  
  
end  

You are running into a problem called “Forward Declaration”. Basically you cannot use any variable or function name until its been defined.

When you call “level1Presents()” at the top of the function, Lua doesn’t know about that function yet, so it throws an error.

You declare the function after you try to use it. Simply move the call to execute the function to the bottom.
[import]uid: 19626 topic_id: 19583 reply_id: 75703[/import]

Awesome thank you!

Also can I forward reference all of my functions in the create scene so I don’t have to worry about order?

I see many programmers setting up their functions

local function = {}

at the beginning than calling them later? [import]uid: 75779 topic_id: 19583 reply_id: 75710[/import]

Generally its best practice to declare the functions at the top of the program and then call them later when you need them. Your “level1Presents” function could have been declared above the createScene function.

If you can do this, then you don’t have to worry about forward declarations.

In many 2-pass compiled languages like C and C++ we don’t have to worry about forward declaration very much so some programmers believe that functions should be defined near where you are using them. This philosophy tries to rear its head in Lua because its kind of the new way of doing things.

There will be times in Lua where you want to declare a function or variable near where you want to use it. The variable names have to exist before you first reference them, then you may need to declare the variable name first. That is the variable = function() syntax that you see:

local myFunction -- pre-define the variable name  
  
...  
  
myFunction()   
  
...  
  
myFunction = function()  
 ...  
 -- do stuff  
 ...  
end  

Lets you use a function before the code is written.
[import]uid: 19626 topic_id: 19583 reply_id: 75712[/import]

Thanks I’m forward declaring all my variables and functions from now on. There doesn’t seem to be anything ‘bad’ that can come from it. Is there?

One question, you forward declared your function as a variable. I’ve been studying some code where the author forward declared his functions as a table. Will their be differences in the outcome when using one or the other?

[import]uid: 75779 topic_id: 19583 reply_id: 75719[/import]

Interesting, I forward declared ‘level1Presents’ as both a variable and as a table(separate attempts) before ‘createScene’ and then tried calling it before defining it like I had done previously.(in the enterScene)

It should have worked but it didn’t. Simulator says
'Attempt to call upvalue ‘level1Presents’ ’

So no worries, I will go with calling it after defining and it works perfectly. Any ideas why that didn’t work?

[import]uid: 75779 topic_id: 19583 reply_id: 75723[/import]

Personally I would have coded it calling it afterwards. I don’t like the forward declaration method myself.
[import]uid: 19626 topic_id: 19583 reply_id: 75724[/import]

lol i thought u did like it. but if you follow the flow of your program and the order of operations you shouldn’t have any silly issues like i did.

Thanks robmiracle you’ve given me the christmas gift of knowledge!

[lua]local function robMiracle

if (robmiracle == 1) then
print (‘Merry Christmas’)

elseif (robmiracle == 2) then
print (‘Happy Hannukah’)

elseif (robmiracle == 3) then
print (‘Happy Kwanza’)

end
end

[import]uid: 75779 topic_id: 19583 reply_id: 75728[/import]