Function execution speed and order

Started by abstauber, Sun 16/05/2010 11:14:58

Previous topic - Next topic

abstauber

Hey,

I'm a little uncertain how to handle functions taking more than one gamecycle to finish.

Here's what I'm trying to do:
Code: ags

save_to_file ("room10"); // very slow function
clear_all_struts(); // not so slow function
player.ChangeRoom(11);


When I'm doing it like this, my game's going haywire. The clearing function is called before the saving function has been finished and sometimes even ChangeRoom is called before save_to_file is done.

This is my current approach:

Code: ags

if (save_to_file ("room10")) {
  if (clear_all_struts()) {
    player.ChangeRoom(11);
  }
}


It works fine, but somehow it looks quite messy. Is there a better way to wait for slow functions?

Cheers!

Sslaxx

Quote from: abstauber on Sun 16/05/2010 11:14:58
Hey,

I'm a little uncertain how to handle functions taking more than one gamecycle to finish.

Here's what I'm trying to do:
Code: ags

save_to_file ("room10"); // very slow function
clear_all_struts(); // not so slow function
player.ChangeRoom(11);


When I'm doing it like this, my game's going haywire. The clearing function is called before the saving function has been finished and sometimes even ChangeRoom is called before save_to_file is done.

This is my current approach:

Code: ags

if (save_to_file ("room10")) {
  if (clear_all_struts()) {
    player.ChangeRoom(11);
  }
}


It works fine, but somehow it looks quite messy. Is there a better way to wait for slow functions?

Cheers!
This might be really dumb, but what about the Wait command itself?
Quote from: Wait

Wait (int time)
Pauses the script and lets the game continue for TIME loops. There are normally 40 loops/second (unless you change it with SetGameSpeed), so using a value of 80 will wait 2 seconds. Note that no other scripts can run while the Wait function is in the background.
Example:
cEgo.Walk(120, 140, eBlock, eWalkableAreas);
Wait(80);
cEgo.FaceLocation(1000,100);
will move the character EGO to 120,140, wait until he gets there then wait for 2 seconds (80 game cycles) and then face right.
See Also: WaitKey, WaitMouseKey
Stuart "Sslaxx" Moore.

abstauber

Ah okay,
something like

Code: ags

while (!save_to_file ("room10")) Wait(1);
while(!clear_all_struts()) Wait(1);
player.ChangeRoom(11);


Thanks! This might be actually even faster :)

Wyz

Quote from: abstauber on Sun 16/05/2010 12:17:16
Code: ags

//...
while(!clear_all_struts()) Wait(1);
//...


Beware: This will call the 'clear_all_struct' function over and over till one returns true. This will only work if that function internally keeps track of the progress.
I've seen in some cases that placing a single Wait(1) between the functions suffices. I don't know if that works in this case, but you could give it a try.

Code: ags

save_to_file ("room10");
Wait(1);
clear_all_struts();
Wait(1);
player.ChangeRoom(11);


If that doesn't work then you need to do something with a wait loop after all.
Life is like an adventure without the pixel hunts.

abstauber

Hey Wyz,

thanks for the tip - I've adjusted my code.

SMF spam blocked by CleanTalk