Endless loop with minimal delay?

Here you can post threads requesting help on the official POL Ultima Online Emulator Core 096.
Note: Core 096 is no longer officially supported.

Moderator: POL Developer

Post Reply
Poi
Grandmaster Poster
Posts: 298
Joined: Fri Apr 14, 2006 9:36 am

Endless loop with minimal delay?

Post 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 =/
Yukiko
Distro Developer
Posts: 2825
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Re: Endless loop with minimal delay?

Post 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.
Poi
Grandmaster Poster
Posts: 298
Joined: Fri Apr 14, 2006 9:36 am

Re: Endless loop with minimal delay?

Post by Poi »

Hmm, that seems to work very well, thank you.
Yukiko
Distro Developer
Posts: 2825
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Re: Endless loop with minimal delay?

Post 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.
User avatar
CWO
POL Expert
Posts: 1158
Joined: Sat Feb 04, 2006 5:49 pm
Location: Chicago, IL USA

Re: Endless loop with minimal delay?

Post 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.
xeon
Forum Regular
Posts: 338
Joined: Fri Oct 31, 2008 3:18 am
Location: Italy

Re: Endless loop with minimal delay?

Post 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.
Yukiko
Distro Developer
Posts: 2825
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Re: Endless loop with minimal delay?

Post 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.
Post Reply