Events Guide

Sometimes you need to share your knowledge. This is where you can do it. A place for POL and Script guides.
Post Reply
User avatar
Austin
Former Developer
Posts: 621
Joined: Wed Jan 25, 2006 2:30 am

Events Guide

Post by Austin »

This guide will cover the basics for sending events between two scripts.

This example takes 1 script (A) and it starts another script (B) then sends events to it. Script B reports if it got an event or if it did not.
You can send anything in an event - a mobile reference, item reference, error, struct, dictionary, integer, etc. and it will be received that way.

A.src

Code: Select all

use uo;
use os;
use util;

program SomeScript()
	var script := Start_Script(":packagename:B");
	if ( script.errortext )
		print("Error starting B ->"+script.errortext);
		return 0;
	endif
	
	Print("MyPId="+GetPid());
	Print("Sending events to script with PId: "+script.pid);
	while ( 1 )
		var time := StrFormatTime("%H:%M:%S", POLCore().systime);
		SysLog(time+" - Sending Tick.");
		script.SendEvent("Tick!");
		
		Sleep(RandomInt(20)+1);
	endwhile
	
	return 1;
endprogram
B.src

Code: Select all

use uo;
use os;
use util;

program EventCatcher()
	while ( 1 )
		// Wait up to 5 seconds for an event
		var event := Wait_For_Event(5);
		
		var time := StrFormatTime("%H:%M:%S", POLCore().systime);
		
		if ( event )
		 	SysLog(time+" - Received Event. Value = "+event);
		else
			SysLog(time+" - No event received.");
		endif
	endwhile
	
	return 1;
endprogram
Console Output

Code: Select all

MyPId=20430
Sending events to script with PId: 20431

syslog [pkg/commands/test/A.ecl]: 18:37:49 - Sending Tick.
syslog [pkg/commands/B.ecl]: 18:37:49 - Received Event. Value = Tick!
syslog [pkg/commands/B.ecl]: 18:37:54 - No event received.
syslog [pkg/commands/B.ecl]: 18:37:59 - No event received.
syslog [pkg/commands/test/A.ecl]: 18:38:04 - Sending Tick.
syslog [pkg/commands/B.ecl]: 18:38:04 - Received Event. Value = Tick!
syslog [pkg/commands/test/A.ecl]: 18:38:08 - Sending Tick.
syslog [pkg/commands/B.ecl]: 18:38:08 - Received Event. Value = Tick!
syslog [pkg/commands/B.ecl]: 18:38:13 - No event received.
syslog [pkg/commands/B.ecl]: 18:38:18 - No event received.
syslog [pkg/commands/B.ecl]: 18:38:23 - No event received.
User avatar
Austin
Former Developer
Posts: 621
Joined: Wed Jan 25, 2006 2:30 am

Re: Events Guide

Post by Austin »

Explanation:
This is the most simple form of sending events in POL. There are two scripts. One is sending events to the other.

Script A will send a string to script B containing "Tick!" between 1 and 20 seconds.
Script B will wait up to 5 seconds for an event to come in before moving on.

Since B is looping, it could receive an event every 1 second. All the Wait_For_Event(5) means is that, if nothing within 5 seconds, move on. If an event comes before 5 seconds, it'll retrieve the event, then move on.
User avatar
Austin
Former Developer
Posts: 621
Joined: Wed Jan 25, 2006 2:30 am

Re: Events Guide

Post by Austin »

Where would this be useful?

A long time ago (and sadly some still do this) - Shards had poison scripts. The approach was to start 1 script, it would set/read a few cprops constantly.
If the poison level needed to go up, it stopped its self, then started another that would read/write. While those poison scripts are running, someone drinks a cure potion, then that must modify that cprop ... well as you can see, this can run into a messy situation of what comes before what? Things being missed... a race condition.

The timed scripts package in the distro is an example of how to take a bunch of things, tie them into 1 controller and let it handle events then manage what needs to be done:
http://poldistro.svn.sourceforge.net/vi ... xt%2Fplain

The timed scripts will start, stop, reduce time, increase time, etc. depending on the event it receives from other scripts. It works on a first in, first out model.
Post Reply