Page 1 of 1

Endless loop with minimal delay?

Posted: Fri Dec 21, 2012 3:59 pm
by Poi
I need to make an endless while loop that only breaks when the character moves, I'm hoping to have it break with little to no delay(so as soon as the character moves it breaks), but the only way I've been able to do this is a 1+ second delay(doing a os:wait_for_event(1);) but this causes a delayed action, is there any way around this without it freezing up the server and turning into a "runaway" script?

Code: Select all

			while(!who.dead && who.connected)
			
				while(who.x == lx && who.y == ly)
					if (GetMana(who) < GetIntelligence(who) - 19 && timer + 20 <= ReadGameClock())
					PlaySoundEffect(who, sfx_meditation);
					SetMana(who, GetMana(who) + 20);
					SetObjProperty(created, "lastmana1", ReadGameClock());
					timer := GetObjProperty( created, "lastmana1");
					endif
				//var ev := os::wait_for_event(1);
				endwhile
				
			MoveItemToLocation(created, who.x, who.y, who.z + zz);

			endwhile
That turns into a runaway script and freezes up the server without the wait for event thing in there =/

Re: Endless loop with minimal delay?

Posted: Fri Dec 21, 2012 11:28 pm
by Yukiko
Try putting a Sleepms(50) right before the endwhile. That function is in the OS module. You might have to play with the value you pass to the Sleepms function to get things just right.

Re: Endless loop with minimal delay?

Posted: Sat Dec 22, 2012 4:45 am
by Poi
Hmm, that seems to work very well, thank you.

Re: Endless loop with minimal delay?

Posted: Sun Dec 23, 2012 5:03 am
by Yukiko
You're welcome. By the way, that's a tip I remember seeing posted by someone else, maybe CWO or Muad Dib.

The problem is that the script is cycling too quickly and POL thinks it's, well, a runaway script. I never researched the definition of a "runaway script" so I'm not sure what a runaway script is exactly.

Re: Endless loop with minimal delay?

Posted: Wed Dec 26, 2012 9:04 pm
by CWO
*toots his own horn* You were probably reading my performance guide

A runaway script is a script that has gone x amount of instructions without a sleep, sleepms, or wait_for_event. x is defined in pol.cfg as RunawayScriptThreshold defaulting to 5000. I don't suggest setting it any higher than default either (my shard runs with it set to 2000). As far as sleepms(), you can get a really nice and quick sleep and react time on your script with it. And the fact that sleepms(50) was suggested and not sleepms(1) means Yukiko has been paying attention :D Experiment with it a bit though. The longer you can set the sleepms and be comfortable with the result, the better.

Re: Endless loop with minimal delay?

Posted: Thu Dec 27, 2012 2:19 am
by xeon
Just an hint, nevermind putting in your script a sleepms() with a value less than 10, because the core it's unable to stop the script and reschedule it for so little time.

Re: Endless loop with minimal delay?

Posted: Thu Dec 27, 2012 5:35 am
by Yukiko
It might have been your guide CWO. I'm not sure. I just remember someone more knowledgeable than I posting something to the effect that "to prevent a runaway script error try inserting a sleepms function call in the script." I'm pretty sure it was your post though. As for the duration of 50 milliseconds, that was a guess. : ) Poi had mentioned he wanted the loop to exit with minimal delay so I took a shot with 50. I figured 1 was way to short but anything over 100 would introduce a noticeable delay.