Is there a way to NOT have functions run at the same time?

I am trying to print “Level Loaded” after a level is loaded, but it doesn’t wait for the level to load, it just prints it right away.
Inside loadLevel(1) there is a 5 second timer. My print statement shouldn’t print until after the 5 seconds. Why does it do this? It prints right when the level starts loading

[code]
function startGame(e)
if(e.phase == “ended”) then
loadLevel(1); --Load level, Wait 5 seconds, then print
print “Level Loaded”;
end
–spawnTimer = timer.performWithDelay(2000 - spawnRate * 100, spawnObjects, 1);
end

[code]

[import]uid: 39238 topic_id: 6877 reply_id: 306877[/import]

First, you didn’t post the loadlevel function so it is hard to guess what you do there. If that function contains only producual code, then your print command will be executed ones the function call to loadLevel returns.

Generally Corona is event based. A timer function tells the system to do something at a certain time and maybe repeat it in specified intervalls. It doesn’t stop the execution of the rest but goes further in the script. A good way to manage sonething like this is using the finite state machine and let it act on certain status which are set through events and timers. Of course there are other ways of doing it too.

Cheers
Michael

http://www.whiteskygames.com
http://www.twitter.com/mhartlef [import]uid: 5712 topic_id: 6877 reply_id: 24018[/import]

you said inside your loadLevel there is a 5 second timer. that must call a function at the end of it. put the print in there.

Lua commands are executed immediately, unlike something like GLBasic, where timer functions pause the execution [import]uid: 6645 topic_id: 6877 reply_id: 24042[/import]

Here is my loadLevel function:

local function loadLevel(level)  
 if(level == 1) then  
 correctType.text = "Color";  
 --slide Main menu to the left  
 transition.to( mainMenu, { time=400, x=(-999), y=(SCREEN\_HEIGHT / 2) } );  
 --make gameScreen1 visible and slide it into view  
 gameScreen1.alpha = 1;  
 transition.to( gameScreen1, { time=400, x=(0), y=(0) } );  
 end  
 delayTimer = timer.performWithDelay(1000, delay, 3);   
end  

Here is Delay and PreCount:

[code]
local function delay(e)
if(delayCount==0) then
preTimer = timer.performWithDelay(1000, preCount, 3);
else
delayCount = delayCount - 1;
end
end

–decreases 3,2,1 timer before level starts
local function preCount(e)
if(preCountDown > 1) then
preCountDown = preCountDown - 1;
preCountDownText.text = preCountDown;
elseif(preCountDown == 1) then
preCountDownText.alpha = 0;
resetCountDowns();
readyToPlay = true;
end
end

you’ve already defined the point where your code is ready to continue

[lua]readyToPlay=true[/lua]

don’t forget you can also dispatch custom events and add listeners for them

http://developer.anscamobile.com/reference/index/objectdispatchevent

[lua]function startGame(e)
if(e.phase == “ended”) then
loadLevel(1); --Load level, Wait 5 seconds, then print
game:addEventListener(“levelLoaded”, onLevelLoaded)
end
end

function onLevelLoaded(e)

print(“done”)
game:removeEventListener(“levelLoaded”, onLevelLoaded)

end [/lua]

[lua]elseif(preCountDown == 1) then
preCountDownText.alpha = 0;
resetCountDowns();
readyToPlay = true;
– i’m not sure about this target
– you’ll have to set it up for your system
local event = { name=“levelLoaded”, target=game }
game:dispatchEvent( event )
end[/lua]

i dont know what objects you have available and where the code is… if theyre in different modules maybe you will need to use [lua]Runtime:[/lua] rather than [lua]game:[/lua] etc

j
[import]uid: 6645 topic_id: 6877 reply_id: 24047[/import]

I used the “game” approach and that did exactly what I needed it to do. It is printing the text at the proper time. The only thing is that after it prints the text, I get this error:

Runtime error:
?:0: attempt to index a nil value
stack traceback:
[C]: ?
?: in function ‘?’
?: in function <?:214>
Do you know what this error means? [import]uid: 39238 topic_id: 6877 reply_id: 24051[/import]

whatever you’re referencing after that doesn’t exist. can you post the bit of code that’s likely generating that? to narrow it down normally i do something like this

[lua]print(“1”)
obj = whatever
print(“2”)
doSomething()
print(“3”)
game.do(whatever)
print(“4”)[/lua]

then i can determine at which step my error occurs, since the numbers will be interrupted by the error

[import]uid: 6645 topic_id: 6877 reply_id: 24054[/import]