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 » Fri Dec 21, 2012 3:59 pm

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 =/
Bittiez/Syp/Taz

Yukiko
Distro Developer
Posts: 2501
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Re: Endless loop with minimal delay?

Post by Yukiko » Fri Dec 21, 2012 11:28 pm

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.
Sincerely,
Yukiko

I would tell you a UDP joke but you might not get it.

Titus 2:13

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

Re: Endless loop with minimal delay?

Post by Poi » Sat Dec 22, 2012 4:45 am

Hmm, that seems to work very well, thank you.
Bittiez/Syp/Taz

Yukiko
Distro Developer
Posts: 2501
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Re: Endless loop with minimal delay?

Post by Yukiko » Sun Dec 23, 2012 5:03 am

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.
Sincerely,
Yukiko

I would tell you a UDP joke but you might not get it.

Titus 2:13

User avatar
CWO
POL Expert
Posts: 1141
Joined: Sat Feb 04, 2006 5:49 pm
Location: Chicago, IL USA
Contact:

Re: Endless loop with minimal delay?

Post by CWO » Wed Dec 26, 2012 9:04 pm

*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: 333
Joined: Fri Oct 31, 2008 3:18 am
Location: Italy
Contact:

Re: Endless loop with minimal delay?

Post by xeon » Thu Dec 27, 2012 2:19 am

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.
Scripter Xeon | Zulu Hotel Italia 7th Age | www.zhi.it
Image

Yukiko
Distro Developer
Posts: 2501
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Re: Endless loop with minimal delay?

Post by Yukiko » Thu Dec 27, 2012 5:35 am

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.
Sincerely,
Yukiko

I would tell you a UDP joke but you might not get it.

Titus 2:13

Post Reply