Timed Events
Ever wanted to make an efficient way of hooking events in a script? I'll show you how, but you need to have a BASIC knowledge of C++. I recommend you read up on the definitions of a: Class, pointer, void, typedef, callback. If you already know what they are, then making a timed event will be easy! Before we begin, TIMED EVENT ARE A HARD TOPIC. Most of you won't get it on your first try so just look at examples and PRACTICE.
First we will start off by defining the class we want to hook to. For this demonstration, we are going to make players resurrect 5 seconds after they die. The class and void for the script looks like this:
Remember the color codes!
Now in order to link to our script, we will have to make a hook when the player dies. The hook when a player is sent to the graveyard (after releasing spirit) is called OnRepop. The hook looks like this:Code:class OurClass { public: void Revive(Player * pPlayer) { pPlayer->ResurrectPlayer(); pPlayer->SetUInt32Value(UNIT_FIELD_HEALTH, pPlayer->GetUInt32Value(UNIT_FIELD_MAXHEALTH)); pPlayer->SetUInt32Value(UNIT_FIELD_POWER1, pPlayer->GetUInt32Value(UNIT_FIELD_MAXPOWER1)); pPlayer->CastSpell(pPlayer, 46165, 0); } };
In order to hook the two, we need to define the class we want to be executed. We can do this simply by typing the class name, followed with a variable. Then, I will give you the structure of the timed event. Just know that there will be numerous arguments in the parenthesis. Our product looks like this:Code:void Repop(Player *pPlayer) { //Our code }
The color coding is pretty self-explanatory, but I want to go over the CallBackPX. The X is the amount of pointers you have in your void. In our demonstration, we only have 1 pointer. So it will become CallBackP1. delay is the wait time IN MILLISECONDS before executing the function. Since we will wait 5 seconds, delay becomes 5000. And for repeats, since we only want this happening once, it becomes 1. Now all you have to do is fill in the blanks. Our product now looks like this:Code:void Repop(Player *pPlayer) { OurClass oc; TimedEvent * pointer = TimedEvent::Allocate(&class, new CallbackPX<Class, ClassPointer>(&class, &Class::Void, Pointer), 0, delay, repeats); }
Last but not least, we want to add the event to the player. This can be done with a simple function called event_addEvent. Our final product, all together, will look like this:Code:void Repop(Player *pPlayer) { OurClass oc; TimedEvent * ReviveMe = TimedEvent::Allocate(&oc, new CallbackP1<OurClass, Player>(&oc, &OurClass::Revive, pPlayer), 0, 5000, 1); }
Code:class OurClass { public: void Revive(Player * pPlayer) { pPlayer->ResurrectPlayer(); pPlayer->SetUInt32Value(UNIT_FIELD_HEALTH, pPlayer->GetUInt32Value(UNIT_FIELD_MAXHEALTH)); pPlayer->SetUInt32Value(UNIT_FIELD_POWER1, pPlayer->GetUInt32Value(UNIT_FIELD_MAXPOWER1)); pPlayer->CastSpell(pPlayer, 46165, 0); } }; void Repop(Player *pPlayer) { OurClass oc; TimedEvent * ReviveMe = TimedEvent::Allocate(&oc, new CallbackP1<OurClass, Player>(&oc, &OurClass::Revive, pPlayer), 0, 5000, 1); pPlayer->event_AddEvent(ReviveMe); }