Is there any way to call a function that is defined further down in the lua file? If it’s not defined in order, I get errors.
I did try searching for the answer, but not having any luck. [import]uid: 50410 topic_id: 8698 reply_id: 308698[/import]
Is there any way to call a function that is defined further down in the lua file? If it’s not defined in order, I get errors.
I did try searching for the answer, but not having any luck. [import]uid: 50410 topic_id: 8698 reply_id: 308698[/import]
you can use forward declaration for the function that is defined further down
local func2
local function func1()
func2()
end
function func2()
print("function 2")
end
[import]uid: 6459 topic_id: 8698 reply_id: 31726[/import]
You know - I tried that, but still got an error. I must have done something wrong. lol. I’ll try it again…
Yep, Here’s the error:
Runtime error
D:\Users\Frank\DOCUME~1\CORONA~2\Sandbox\30\game.lua:133: attempt to call upvalue 'win\_or\_lose' (a nil value)
stack traceback:
[C]: in function 'win\_or\_lose'
D:\Users\Frank\DOCUME~1\CORONA~2\Sandbox\30\game.lua:133: in function 'trackOrbs
Here’s the source: (game.lua)
module(..., package.seeall);
function new()
...
local win\_or\_lose -- cannot be declared here, because it calls resetgame()
local function trackOrbs(obj) -- cannot be declared below because trackOrbs is called in resetgame()
obj:removeSelf()
o = o - 1
if (o == 0) then
win\_or\_lose("win")
end
end
...
--resetgame() is in here somewhere. lol - it calls spawnorbs which calls trackorbs... it gets messy.
...
local function win\_or\_lose(condition)
if(condition == "win") then
display\_txt.text = "You WIN!"
audio.play(win\_sound)
timer.cancel(gametmr)
m\_score = m\_score + (time\_remain \* total\_orbs)
m\_score\_txt.text = "Score: " .. m\_score
won = true
resetGame()
else
display\_txt.text = "Game Over!"
localscore.add({score = m\_score})
audio.play(fail\_sound)
timer.cancel(gametmr)
won = false
timer.performWithDelay ( 2000, exitToMenu, 1)
end
end
...
end
MAYBE I have to declare it outside of the new function? I’ll try that next. Nope - still same error. ( All I did was move the declaration outside of the new() function. Maybe I declared it wrong because it’s inside of new()? )
[import]uid: 50410 topic_id: 8698 reply_id: 31762[/import]
erase local from line 17, it’s already declared as local on line 5
[import]uid: 6459 topic_id: 8698 reply_id: 31797[/import]
ok, I’ll give that a shot later!
Thank you. I wont be able to do any corona coding for at least 20 hrs… but I’ll let you know the results as soon as I do. [import]uid: 50410 topic_id: 8698 reply_id: 31798[/import]