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!

Mobile events

_Epila_

Sorceror
Mobile events

Would be great if the current method triggers(OnSpeech, OnDeath...) could be converted to events

incase we want to add an specific event only when someone has an item equiped for example

not just mobiles, but itens too
 

Humpster

Wanderer
_Epila_;835293 said:
Would be great if the current method triggers(OnSpeech, OnDeath...) could be converted to events

incase we want to add an specific event only when someone has an item equiped for example

not just mobiles, but itens too

like OnEquip?
 

_Epila_

Sorceror
I found a way to do

in your PlayerMobile.cs > PlayerMobile class add the following code:
Code:
        #region Delegates/Events
        public delegate void OnMoveEventHandler();
        public event OnMoveEventHandler OnMoveEvent;
        #endregion
(or if want another event instead of OnMove)
now you have the handlers set, with no function

go to your PlayerMobile OnMove(Direction d) method and add the following code
Code:
            if (OnMoveEvent != null) //check if we have any event set
                OnMoveEvent();

ok, now we want to fire this event when someone equip an item, so:

at your item class, declare a variable that will hold your event
Code:
private static PlayerMobile.OnMoveEventHandler ev = new PlayerMobile.OnMoveEventHandler(pm_OnMove);


override the OnEquip(Mobile from) on your item and do something like that
Code:
        public override bool OnEquip(Mobile from)
        {
            PlayerMobile pm = from as PlayerMobile;
            pm.OnMoveEvent += ev;
            Console.WriteLine(pm.Name + " Just equiped"); //just debug
            return true;
        }

them create the method that we specified when declaring the ev variable
Code:
        private static void pm_OnMove()
        {
            Console.WriteLine("Just moved");
        }


so now we have an event that will fire only when the player has the item equiped
(the code above didnt remove the event handler from the player, so if you unequip, or delete the item, the event will remain - dont forget to remove the handler in these cases)

the only trouble that i got is, how we will serialize/deserialize the handlers on PlayerMobile

any idea how to improve, please tell me
thanks
 

Jeff

Lord
Movement already exists with EventSink.Movement...

Also, you are leaving lots of open event subscriptions

Code:
      public override bool OnEquip(Mobile from)
        {
            PlayerMobile pm = from as PlayerMobile;
            pm.OnMoveEvent += ev;
            Console.WriteLine(pm.Name + " Just equiped"); //just debug
            return true;
        }

Each time someone equips a weapon, you add the event handler... but no where is it removed... awful...
 

_Epila_

Sorceror
This is just an example
imagine if you have gloves, that when the player turn warmode on, appear a magical bow, and when he turn back to peace, the bow disapear
 

Jeff

Lord
_Epila_;836368 said:
This is just an example
imagine if you have gloves, that when the player turn warmode on, appear a magical bow, and when he turn back to peace, the bow disapear

That should be coded in the gloves, not in a event....
 

Jeff

Lord
You need to code that. The problem with exposing events, is most users do not know how to use them properly. For each += you need a -= as well. Without it, you leave potential issues that could cause a server crash and/or extreme slowdown...For example your example above...

People who know how to use them, know how to make them, we aren't going to code everything for you, you are going to need to code some things yourself. This is why it is open source.
 
Top