Wait(1) - A non-blocking equivalent [SOLVED]

Started by magintz, Sun 02/05/2010 12:50:19

Previous topic - Next topic

magintz

So I'm trying to get a GUI to fade in and fade out, to do this I have a while loop reducing the transparency bit by bit on each pass through the loop, see code:

Code: ags

function fade(this GUI*, String inOut, int speed) {
	int i = 100 - (100 - ((100/speed)*speed));
	
	while(i > 0) {
		if (inOut == "in") { this.Transparency = i; }
		else { this.Transparency = 100-i; }
		i-=speed;
		Wait(1);
	}
}


But as you can see I'm using a Wait(1) to make the fade out gradual rather than happening before you can see it.

I've tried to replicate this with a setTimer but I don't actually see the guifade in or out (I think it's happening too quickly). Here's the code I put inside the while loop:

Code: ags

SetTimer(5, 1);
while(IsTimerExpired(5) == false){
  // do nothing
}


When I up the SetTimer to 2 the game crashes saying that the while loop ran 150001 times or something ridiculous. I've even tried nesting several timeout, while loops in the hope that it will prevent a crash but with no luck :(

Thanks in advance,
Maggi
When I was a little kid we had a sand box. It was a quicksand box. I was an only child... eventually.

Matti

Quote from: magintz on Sun 02/05/2010 12:50:19
Code: ags

SetTimer(5, 1);
while(IsTimerExpired(5) == false){
  // do nothing
}


When I up the SetTimer to 2 the game crashes saying that the while loop ran 150001 times or something ridiculous. I've even tried nesting several timeout, while loops in the hope that it will prevent a crash but with no luck :(

Of course it crashes. The while-loop runs as long as the timer isn't expired... so it doesn't stop running.

As for the speed problem, why not simply use a variable instead of the wait() ?


Code: ags

function fade(this GUI*, String inOut, int speed) {
	int i = 100 - (100 - ((100/speed)*speed));
        int slow=0;
	
	while(i > 0) {
		if (inOut == "in") { this.Transparency = i; }
		else { this.Transparency = 100-i; }
                if (slow<1) slow++;
                else slow=0;
		if (slow==1) i-=speed;
	}
}


That should be aquivalent to wait(1);

Calin Leafshade

#2
I do this alot in Hope and Eternally Us and the best way to do it is to have a flag which denotes whether or not a gui should be in or out.

something like this:

Code: ags


bool FadeInGui = false;

function repeatedly_execute_always(){

if (FadeInGui) gGui.Transparency = ClampInt(0, 100, gGui.Transparency - 4);
else gGui.Transparency = ClampInt(0, 100, gGui.Transparency + 4);


}



The 'ClampInt' function I use basically negates the need for checks on the tranparency values and looks like this

Code: ags


function ClampInt(int min, int max, int value){

if (value < min) return min;
else if (value > max) return max;
else return value;

}



You could further expand this by having an array of bools, one entry for each gui and then iterate through them like this

Code: ags


while (i < Game.GuiCount){


if (FadeInGui) gui[i].Transparency = ClampInt(0, 100, gui[i].Transparency - 4);
else gui[i].Transparency = ClampInt(0, 100, gui[i].Transparency + 4);

i ++;

}



hope that helps

magintz

Thanks, I was hoping to avoid repeatedly execute because of the constant extra work but it seems there is no way around it. I had to tweak the code slightly but it's all good, thanks Calin.
When I was a little kid we had a sand box. It was a quicksand box. I was an only child... eventually.

SMF spam blocked by CleanTalk