while statement hanging (SOLVED)

Started by Kinoko, Sun 15/01/2006 12:26:29

Previous topic - Next topic

Kinoko

I know there's something ridiculously obvious I'm not seeing, and I'll probably regret posting this, but here goes.

I'm trying to have the screen scroll upwards in my game. This code worked fine;

Code: ags

yposit=680;
while (yposit > 80) {
    SetViewport(320, yposit);
    Wait(2);
}


But the problem was that I need this code to be unblocking, so I tried this:

Code: ags

yposit=680;
SetGlobalInt(27, 1);
while (yposit > 80) {
  if (GetGlobalInt(27)==1) {
    SetViewport(320, yposit);
    SetGlobalInt(27, 0);
    SetTimer(12, 2);
  }
}

//and in repeatedly_execute_always:

if (IsTimerExpired(12)) {
  yposit--;
  SetGlobalInt(27, 1);
}


This time, it hangs. The while is being run without the script moving on. For the life of me, I can't pinpoint why. I've never been very comfortable with while statements so I'm not sure if this method is possible or not.

DoorKnobHandle

#1
Edit: Nevermind. I just realized you used the timer to decrease the value of "yposit"...

I am not sure, but I think that the while-loop will be executed infinite times, because it runs in ONE loop.

Remember: The while-loop does not run ONE time and then run the next time in the next frame, but it will run in ONE frame as many times as you want it. Thus your loop runs infinite times.

I am currently trying to work around this in the easiest way...

Ashen

I think the simplest way would be:
Go back to the original while loop, but double the initial and final values of yposit, and use (yposit/2) to make it scroll at half speed. (I guess that's why the Wait(2) was there.)

Code: ags

yposit=1360;
while (yposit > 160) {
    SetViewport(320, (yposit/2));
    yposit --;
}
I know what you're thinking ... Don't think that.

DoorKnobHandle

Yes, that should work, Ashen.

You could also get rid of the while-loop and use a new variable and if-statements, but that'd be way more complicated than the solution Ashen just suggested.

Kinoko

When I tried that (ashen's suggestion), it DID scroll but instantly.

Gilbert

Hmmm I think:

yposit=1360;
while (yposit > 160) {
Ã,  Ã,  SetViewport(320, (yposit/2));
    Wait(1);
Ã,  Ã,  yposit --;
}


Ashen

It needs to be non-blocking.

You mean it just jumped to the top? Crap, didn't think of that - while loops do that without the Wait(x).

What if you move the whole thing to Timers, e.g.:
Code: ags

// In repeatedly_execute_always:
if (IsTimerExpired(12)) {
  if (yposit > 80) {
    yposit--;
    SetViewport(320, yposit);
    SetTimer(12, 2);
  }
}


And set yposit and start the Timer for the first time when you want the scrolling to start?
I know what you're thinking ... Don't think that.

Kinoko

FANtastic! Thanks so much! ^_^ It works wonderfully now.

EDIT: Just out of pure curiosity, is there a particular reason the code in my first post didn't work?

Gilbert

Because you used a while, and within that while there's nothing that will make it stop. When the first time it's executed the globalint was set to 0, however, in the while's statement regardless of that variable's stat (the condition was "yposit > 80") which is always true as that variable was never updated in the while loop.

Kinoko


SMF spam blocked by CleanTalk