Infinite while loop?

Started by , Mon 28/03/2005 23:58:33

Previous topic - Next topic

monkey0506

Is it possible to create a while loop that runs indefinitely?  I have a function to accept keypresses during speech (via rep_ex_always) but rep_ex_always runs so fast that the GUIs I'm supposed to be able to turn on and off with the same button just appear to blink.  I thought "while (IsKeyPressed(pressed)) {}" would work, but it produces a run-time error "150001 while loop iterations without update."  I want to check for a pressed key, if one is found, run the interaction for it, then not do anything until the key is released.  Help greatly appreciated :)

DoorKnobHandle

Yes, that's possible, but you wouldn't wanna do that, because it causes your game to crash.

Example:

Code: ags

int i = 0;
while ( i <= 1 )
{
   // do something
}


This while-loop will never ever be true and so your game crashes!

Your game does not react to anything anymore in such a case so it won't help you for what you wanna do.

By the way, this is what misses in that loop to make it work:

Code: ags

int i = 0;
while ( i <= 1 )
{
   // do something
   i++;
}


This will check if 0 smaller or equals 1 ( it is, so: true ), then do something and then increase i by 1 making it 1 ( from 0 ) and then run again ( it will be true since 1 is still smaller or equal 1 ) and then do that stuff again.
Then it is done.

So far the while-loop theory...

stuh505

I would be surprised if an infinite loop in AGS script would actually crash the computer...but then again I didn't program it so I don't know.  But, AGS is pretty good about catching potential infinite loops with an exception (which isn't crashing :P).

I wish I could understand better what you're trying to do, though...because whatever it is, I'm sure that an infinite loop is not the answer

Gilbert

QuoteI would be surprised if an infinite loop in AGS script would actually crash the computer
It would, before the check was added. But now it's not possible to make an "infinite loop" anymore, since there's a safe-guard feature that will exits the game nicely with an error message after a while loop had been executed for some large number of loops (can't remember the exact limit).
It's not recommended to use infinite loop and it's not a good practice anyways, you should just do that kind of things with some scriptings in places like repeated_execte, etc.
(Note in a while loop, the game loops won't be advanced automatically unless you use blocking functions like Wait(), etc., so infinite loops can be causes of troubles like hanging your computer and crashes, so it's not allowed anymore.)

Radiant

You can work around this, though, by considering rep_ex_al an infinite loop by itself (well, it does run repeatedly while the game is active, yes?) So, do something like this,

Code: ags

int mode = 0;

function rep_ex_al () {
  if (mode == 0) {
     // basic stuff
     if (KeyPressed (27)) mode = 1; // and do stuff for pressing key
  } else if (mode == 1) {
     // user is holding the key
     if (KeyPressed (27) == 0) mode = 0; // and act on releasing key
  }
}


for instance. Or the same with the keyup/keydown on_events.

monkey0506

Basically I want the game to not do anything until the player releases the key initially pressed, i.e. I have a paused GUI that turns on when you press the space bar, and it stays on until space is pressed again.  I have to use rep_ex_always to allow this GUI to come on during speech, but it appears to blink.  You can't use Wait() statements within rep_ex_always, so I figured that while (IsKeyPressed27)) {} would work but like I said, that causes the game to exit.  Radiant's code should probably work, I'll try that as soon as I get home.  And if it does, I owe you a million.  Thanks to the others for responding.

EDIT:  Also, a more appropriate title would have been "Indefinite while loop"...I thought that's what I put, but I was pretty frustrated when I made this thread, so...

stuh505

QuoteIt would, before the check was added. But now it's not possible to make an "infinite loop" anymore, since there's a safe-guard feature that will exits the game nicely with an error message after a while loop had been executed for some large number of loops (can't remember the exact limit).

Yes, I mentioned that...but I think you can still probably write an infinite loop by doing something clever like bouncing between loops and changing variable states or something.

scotch

#7
You can have 15000 iterations, this should be enough for most things, the thing is, you don't put in anything that slows it down in your original code, it will run as much time as it takes to do 15000 key checks, then crash, and that's not much time at all.

if you did something like while (IsKeyPressed(pressed)) {Wait(2);}

it'd check for the key press 20 times per second, more than enough, but after 12.5 minutes you'd get the error, it is unlikely anyone would press a key that long, but you can put a check in to make sure that at least it fails without error.

int i = 0;
while (IsKeyPressed(pressed) && i<15000) {Wait(2); i++;}

I'm assuming that IsKeyPressed will detect changes, because I can't find a function for refreshing the keyboard, like the one that exists for mouse position.  if it doesn't then that still won't work.

monkey0506

Actually I believe it's 150000 (the max, more causes the game to exit).  Hence the error reading "150001 while loop iterations without update."  And you can't call Wait() from within repeatedly_execute_always.  I'm about to test Radiant's code (I've typed it in, and have my fingers crossed...).

monkey0506

#9
EDIT:  Interestingly enough, commenting out the code for the PAUSED GUI in on_key_press (not _always) prevented the blinking, and it even works during speech!  THANKS RADIANT!


However I have noticed that key-combinations don't appear to be working...for example, I have Alt-X set to turn on my Quit GUI (yes, I changed the game.abort_key) and it turns on fine.  However, it only stays on while I hold the keys...Perhaps because it's a key-combination and so if I don't release them at exactly the same time it runs it as whichever key was still held down for that fraction of a second (as any non-'y' key turns it off)?  I'll try to figure it out but any help would be appreciated. :) :) :)  THANKS AGAIN!

EDIT:  Commenting the Quit code out of the regular on_key_press stopped the Quit GUI from turning on at all.  Even during speech and on a mouse-click(?) (I have a GUI with a clickable button that is supposed to turn it on... :/).  I'll look further into it before posting my code...


EDIT (AGAIN):  I commented out the part to turn off the QUIT GUI if the key pressed isn't 'y', but I'm still having trouble.  I think I may have localized the problem, but I'm not sure if it will be an easy thing to fix.  I think that it has to do with the fact that the game is registering the Alt press before the X press and not running them at the same time.  I tried doing:

if ((keyPressed == 405) || (keyPressed == 406) || (keyPressed == 407)) isKeyHeld = 0;

in the if (isKeyHeld == 1) part, but out of about 10 - 15 presses I only got the GUI to turn on once...  Any suggestions greatly appreciated.

SMF spam blocked by CleanTalk