RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Tutorial: Harnessing the power of EventSink

wieganka

Sorceror
Tutorial: Harnessing the power of EventSink

Well, I've seen several people lately ask about how they can do things when players login, etc, so, I've decided to put together a very quick tutorial on using EventSink.

Q: What is the EventSink Class?
A: The EventSink Class is a class that gives you the ability to "subscribe" to specific events that happen in the game.

Q: Subscribe? Huh?
A: Think of it as subscribing to your local newspaper. Every morning (or whenever the newspaper is delivered), you get a newspaper at your door. In effect, you are "informed" of this "event", and can take action on it. This may sound strange, but It'll make sense later.

Q: OK, enough talk, show me!
A: Alright, scroll down below for a sample EventSink class.

Code:
[SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][SIZE=2] Server;[/SIZE]
 
[SIZE=2][COLOR=#0000ff]namespace[/COLOR][/SIZE][SIZE=2] Server.Misc[/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#0000ff]   public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]class[/COLOR][/SIZE][SIZE=2] EventSinkTest[/SIZE]
[SIZE=2]   {[/SIZE]
[SIZE=2][COLOR=#0000ff]        public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]static [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][SIZE=2] Initialize()[/SIZE]
[SIZE=2]       {[/SIZE]
[SIZE=2]           EventSink.Login += [/SIZE][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][SIZE=2] LoginEventHandler(EventSink_Login);[/SIZE]
[SIZE=2]           EventSink.PlayerDeath += [/SIZE][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][SIZE=2] PlayerDeathEventHandler(EventSink_PlayerDeath);[/SIZE]
[SIZE=2]           EventSink.Logout += [/SIZE][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][SIZE=2] LogoutEventHandler(EventSink_Logout);[/SIZE]
[SIZE=2]       }[/SIZE]
 
[SIZE=2][COLOR=#0000ff]        public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]static [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][SIZE=2] EventSink_Login(LoginEventArgs e)[/SIZE]
[SIZE=2]       {[/SIZE]
[SIZE=2][COLOR=#008000]            //Broadcast a message to everyone[/COLOR][/SIZE][COLOR=#008000]
[/COLOR][SIZE=2]           World.Broadcast(0x35, [/SIZE][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][SIZE=2].Format([/SIZE][SIZE=2][COLOR=#800000]"{0} just logged in."[/COLOR][/SIZE][SIZE=2], e.Mobile.Name));[/SIZE]
[SIZE=2]       }[/SIZE]
 
[SIZE=2][COLOR=#0000ff]        public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]static [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][SIZE=2] EventSink_Logout(LogoutEventArgs e)[/SIZE]
[SIZE=2]       {[/SIZE]
[SIZE=2][COLOR=#008000]            //Broadcast a message to everyone[/COLOR][/SIZE][COLOR=#008000]
[/COLOR][SIZE=2]           World.Broadcast(0x35, [/SIZE][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][SIZE=2].Format([/SIZE][SIZE=2][COLOR=#800000]"{0} just logged out."[/COLOR][/SIZE][SIZE=2], e.Mobile.Name));[/SIZE]
[SIZE=2]       }[/SIZE]
 
[SIZE=2][COLOR=#0000ff]        public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]static [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][SIZE=2] EventSink_PlayerDeath(PlayerDeathEventArgs e)[/SIZE]
[SIZE=2]       {[/SIZE]
            [SIZE=2][COLOR=#008000]//Auto resurrect[/COLOR][/SIZE][COLOR=#008000]
[/COLOR][SIZE=2]           e.Mobile.Resurrect();[/SIZE]
[SIZE=2]           e.Mobile.Hits = e.Mobile.HitsMax;[/SIZE]
[SIZE=2]           e.Mobile.Stam = e.Mobile.StamMax;[/SIZE]
[SIZE=2]           e.Mobile.Mana = e.Mobile.ManaMax;[/SIZE]
[SIZE=2]       }[/SIZE]
[SIZE=2]   }[/SIZE]
[SIZE=2]}[/SIZE]

OK, so we created a class, and called it EventSinkTest. In that class, we have the following:

Code:
[COLOR=#0000ff]public [/COLOR][SIZE=2][COLOR=#0000ff]static [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][SIZE=2] Initialize()[/SIZE]

This code is run when the class gets loaded by RunUO, so you don't actually have to call this to get your code to run. Inside this procedure, lets look at the first line:

Code:
EventSink.Login += [SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][SIZE=2] LoginEventHandler(EventSink_Login);[/SIZE]

This line is doing the actual "subscribing". This is basically adding your callback function to the stack of callbacks that get run when a player logs into the shard. If you were to use "-=" instead of the "+=", you would "unsubscribe" your callback....but that's a different topic, so I won't go into depth on that.

Now that you have the subscription setup, you need the procedure call:

Code:
[COLOR=#0000ff]public [/COLOR][SIZE=2][COLOR=#0000ff]static [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][SIZE=2] EventSink_Login(LoginEventArgs e)[/SIZE]
{
}

That is the code that will be called now when a player logs in. You can do just about anything in here. I just do a simple World.Broadcast, just to exemplify what can be done.

That is the end of this tutorial...yes it's small, but I wanted to try and make this as easy as possible. Poke around the EventSink class to find out other events that you can subscribe to. Also, take a look at the other 2 EventSinks that I use in this class.

Good luck, and happy coding!
 
Top