PDA

View Full Version : Have to wait 2 frames to check changes



C-Dawg
06-01-2008, 03:39 PM
I have several global scripts that react to changes in game variables. When link's max health increases, a little message pops up going "HEALTH UP!" When you change screens, enemies frozen by the freeze ray dissapear.

In both cases, I detect the change by having variables called "last_health" or "last_screen." At the end of the script's while loop, I load the current health or current screen into this variable. At the beginning of the loop, I check to see if last_health or last_screen no longer agrees with the current screen. Works great.

But, in the case of health and screen changes, one frame isn't enough. For some reason, I need to compare the current screen to the screen TWO frames ago, not one, to detect the change. Ditto for health.

So at the end of each loop, I load last_screen into last_screen2, then load the current screen into last_screen. So it takes two frames to propagate through the script.

Current screen -> last screen -> last screen2.

Now, this works. But why? And for what other game variables can changes only be detected every two frames instead of each frame?

DarkDragon
06-14-2008, 05:50 PM
So if you have a script


int lasthealth = Link->HP;
while(true)
{
if(lasthealth != Link->HP)
{
//DO STUFF
}
lasthealth = Link->HP;
Waitframe();
}

what happens exactly? The if is never true?

C-Dawg
06-23-2008, 12:24 PM
Appears that way.

But modify it so that the code loads the health from 2 frames ago, and it works fine.