Order of functions in code - scoping

I don’t seem to be be able to call a function in another function unless this function code is above the function calling it.

So for example

local function sayHello()  
  
 print ("Hello ")  
  
end  
local function sayHelloPaul()  
  
 sayHello()  
  
end  
local function sayPaul()  
  
 print ("Paul ")  
  
end  
  
sayHelloPaul()  
  

I am having problems adhering to this requirement and wondered if there is a way to predeclare functions or something like that? [import]uid: 7863 topic_id: 2632 reply_id: 302632[/import]

A better example. Here it gives me an error when calling Runtime:addEventListener( “enterFrame”, ballAction ) in the computerTakeShot function

[code]
local function computerTakeShot()
print (“computerTakeShot”)

shootBallTowardsPosition(numOpponentBallRandomIndex, targetBallPositionX, targetBallPositionY)

Runtime:addEventListener( “enterFrame”, ballAction )

end
local function changePlayer()

if boolPlayerTurn == true then

boolPlayerTurn = false
computerTakeShot()

else

boolPlayerTurn = true

end

end

local function ballAction (event)

moveBallObjects()

if checkIfBallsMoving() == false then

Runtime:removeEventListener( “enterFrame”, ballAction )

changePlayer()

end

end

local function onPressBall ( event )

if “began” == phase then

elseif “moved” == phase then

elseif “ended” == phase then

Runtime:addEventListener( “enterFrame”, ballAction )

end

end

[/code] [import]uid: 7863 topic_id: 2632 reply_id: 7536[/import]

@mediakitchen

try putting

local ballAction as the first line of your code. What is happening is trying to reference a function that is not declared yet.

  
local ballAction  
  
local function computerTakeShot()  
.  
.  
.  
.  

That should do it.

Carlos [import]uid: 24 topic_id: 2632 reply_id: 7539[/import]

Thanks Carlos but that didn’t work.

Taking my simpler example

  
local sayPaul  
local sayHello  
local sayHelloPaul  
  
local function sayHello()  
  
 print ("Hello ")  
  
end  
local function sayHelloPaul()  
  
 sayHello()  
 sayPaul()  
  
end  
local function sayPaul()  
  
 print ("Paul ")  
  
end  
  
sayHelloPaul()  
  

I get the following error

**Hello
Runtime error
…user/Documents/work/corona development/test/main.lua:15: attempt to call upvalue ‘sayPaul’ (a nil value)
stack traceback:
[C]: in function ‘sayPaul’
…user/Documents/work/corona development/test/main.lua:15: in function ‘sayHelloPaul’
…user/Documents/work/corona development/test/main.lua:26: in main chunk
Runtime error: …user/Documents/work/corona development/test/main.lua:15: attempt to call upvalue ‘sayPaul’ (a nil value)
stack traceback:
[C]: in function ‘sayPaul’
…user/Documents/work/corona development/test/main.lua:15: in function ‘sayHelloPaul’
…user/Documents/work/corona development/test/main.lua:26: in main chunk
logout

[Process completed]**
[import]uid: 7863 topic_id: 2632 reply_id: 7543[/import]

Try this…

[lua]local sayPaul
local sayHello
local sayHelloPaul

sayHello = function()

print ("Hello ")

end

sayHelloPaul = function()

sayHello()
sayPaul()

end

sayPaul = function()

print ("Paul ")

end

sayHelloPaul()[/lua] [import]uid: 5712 topic_id: 2632 reply_id: 7544[/import]

Thanks Mike - you had me perplexed there for a moment as I thought your code was identical to mine but yours was working but then I spotted the absence of the local on the functions

Is this common practice to do this function predeclaring or am I structuring my code badly? [import]uid: 7863 topic_id: 2632 reply_id: 7547[/import]

It’s a LUA “feature” i think. And it takes a while to get used to. In C /C++ you need to predeclare function headers too if you want to use them in a function above the called one.

Back to LUA… you need to predeclare a function if want to make it callable from all the positions in that file. [import]uid: 5712 topic_id: 2632 reply_id: 7549[/import]

Ah I see, Guess it is safest just to predeclare them all assuming this does not affect performance. In Flash it didn’t matter where a function was declared which is why I was confused. Anyway another hurdle conquered - onward with my game:) [import]uid: 7863 topic_id: 2632 reply_id: 7552[/import]