Go Back   RunUO - Ultima Online Emulation > RunUO > FAQ Forum

FAQ Forum A place to find answers to the most frequently asked questions, and a place to post said answers. Do NOT use this forum to ask questions.

Reply
 
Thread Tools Display Modes
Old 01-17-2007, 05:14 PM   #1 (permalink)
Forum Expert
 
wieganka's Avatar
 
Join Date: Oct 2004
Location: Chesterton, IN
Age: 29
Posts: 288
Default 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:
using Server;
 
namespace Server.Misc
{
   public class EventSinkTest
   {
        public static void Initialize()
       {
           EventSink.Login += new LoginEventHandler(EventSink_Login);
           EventSink.PlayerDeath += new PlayerDeathEventHandler(EventSink_PlayerDeath);
           EventSink.Logout += new LogoutEventHandler(EventSink_Logout);
       }
 
        public static void EventSink_Login(LoginEventArgs e)
       {
            //Broadcast a message to everyone
           World.Broadcast(0x35, true, string.Format("{0} just logged in.", e.Mobile.Name));
       }
 
        public static void EventSink_Logout(LogoutEventArgs e)
       {
            //Broadcast a message to everyone
           World.Broadcast(0x35, true, string.Format("{0} just logged out.", e.Mobile.Name));
       }
 
        public static void EventSink_PlayerDeath(PlayerDeathEventArgs e)
       {
            //Auto resurrect
           e.Mobile.Resurrect();
           e.Mobile.Hits = e.Mobile.HitsMax;
           e.Mobile.Stam = e.Mobile.StamMax;
           e.Mobile.Mana = e.Mobile.ManaMax;
       }
   }
}
OK, so we created a class, and called it EventSinkTest. In that class, we have the following:

Code:
public static void Initialize()
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 += new LoginEventHandler(EventSink_Login);
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:
public static void EventSink_Login(LoginEventArgs e)
{
}
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!
wieganka is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5