Difference between revisions of "Event override"

From Dragon Age Toolset Wiki
Jump to: navigation, search
m (Reformatting)
Line 1: Line 1:
 
{{Infobox script}}
 
{{Infobox script}}
  
== How to override an event? ==
+
The broadest definition of [[Event override|overriding an event]] means redirecting it from the default handler (or script) to some form of a custom handler.
  
=== Via a wrapper script ===
+
== Via a wrapper script ==
  
 
The safest and most common way to override an event is to create an event script for the resource whose event-handling you want to override, and use that script to handle the specific event you want to override while passing on all other events to the core script to handle.
 
The safest and most common way to override an event is to create an event script for the resource whose event-handling you want to override, and use that script to handle the specific event you want to override while passing on all other events to the core script to handle.
Line 16: Line 16:
 
void main()
 
void main()
 
{
 
{
     event ev = GetCurrentEvent();
+
    int nEventHandled;
     int nEventType = GetEventType(ev);
+
 
     string sDebug;   
+
    // decompose the event
     object oPC = GetHero();
+
     event evCurrent = GetCurrentEvent();
     object oParty = GetParty(oPC);   
+
     int nEventType = GetEventType(evCurrent);
    int nEventHandled = FALSE;
+
 
 +
     // common variable
 +
     object oHero = GetHero();
 +
     object oParty = GetParty(oHero);   
  
 
     switch(nEventType)
 
     switch(nEventType)
Line 27: Line 30:
 
         case EVENT_TYPE_DEATH:
 
         case EVENT_TYPE_DEATH:
 
         {
 
         {
             ...
+
             // your custom code goes here
             nEventHandled = TRUE; //set this if no further action is required for this event
+
 
 +
             // set nEventHandled if no further action is required for this event
 +
            nEventHandled = TRUE;
 +
 
 
             break;
 
             break;
 
         }
 
         }
 
     }
 
     }
     if (!nEventHandled) //If this event wasn't handled by this script, let the core script try
+
 
 +
     // if this event wasn't handled by this script, let the core script try
 +
    if(!nEventHandled)
 
     {
 
     {
 
         HandleEvent(ev, RESOURCE_SCRIPT_CREATURE_CORE);
 
         HandleEvent(ev, RESOURCE_SCRIPT_CREATURE_CORE);
 
     }
 
     }
 +
 
}
 
}
 
</dascript>
 
</dascript>
  
And set this script as the event-handling script for Death Tyrants. When the game sends the Death Tyrant any event other than EVENT_TYPE_DEATH, it'll get sent to the core creature_core event-handling script by the function call HandleEvent(ev, RESOURCE_SCRIPT_CREATURE_CORE). EVENT_TYPE_DEATH, on the other hand, will get intercepted and handled by your own code.
+
And set this script as the event-handling script for Death Tyrants. When the game sends the Death Tyrant any event other than <code>EVENT_TYPE_DEATH</code>, it will get sent to the core <code>creature_core</code> event-handling script by the function call <code>HandleEvent(ev, RESOURCE_SCRIPT_CREATURE_CORE)</code>. However <code>EVENT_TYPE_DEATH</code> will be intercepted and handled by your own code.
  
=== Via the Events 2DA ===
+
== Via the events 2DA ==
  
 
This is how you can do the same thing on a global scale, catching and overriding an event for all resources in the game.
 
This is how you can do the same thing on a global scale, catching and overriding an event for all resources in the game.
  
Create a [[Events.xls|Events]] M2DA File - for instructions look [[2DA#Extending_the_game_via_M2DAs|here]]:
+
Create a [[Events.xls|engineevents]] [[2DA|m2da]] fragment (see [[2DA#Extending_the_game_via_M2DAs|extending the game via m2da files]] for details).
  
 
[[File:Events.png]]
 
[[File:Events.png]]
  
In this case i used the "EVENT_TYPE_INVENTORY_ADDED" Event and Handle it in my custom Event-Handler-Script: my_event_override
+
In this case the <code>EVENT_TYPE_INVENTORY_ADDED</code> event will be overridden and handled in a custom script: <code<my_event_override</code>
  
Now just handle the event in your script as you like.
+
This custom script can do whatever is required, for example, displaying a message on screen every time the Player picks up an item (or steal one).
 
+
For example i want to have a screen-message every time i pick up an item (or steal one):
+
  
 
<dascript>
 
<dascript>
// --- script start ---
 
// file: my_event_override.nss
 
// author: Pheelon
 
 
 
#include "utility_h"
 
#include "utility_h"
 
#include "wrappers_h"
 
#include "wrappers_h"
 
#include "events_h"
 
#include "events_h"
 +
 
void main()
 
void main()
 
{
 
{
     event ev  = GetCurrentEvent();
+
     // decompose the event
     int nEvent = GetEventType(ev);
+
    event evCurrent = GetCurrentEvent();
     object oOwner = GetEventCreator(ev);
+
     int nEventType = GetEventType(evCurrent);
 +
     object oOwner = GetEventCreator(evCurrent);
  
     switch (nEvent)
+
     switch(nEventType)
 
     {
 
     {
 
         case EVENT_TYPE_INVENTORY_ADDED:
 
         case EVENT_TYPE_INVENTORY_ADDED:
 
         {
 
         {
 
             object oItem = GetEventObject(ev, 0);
 
             object oItem = GetEventObject(ev, 0);
            if (IsPartyMember(oOwner)) // only show if the item went into the player's inventory
+
 
 +
            // only show if the item went into the player's inventory
 +
            if(IsPartyMember(oOwner))
 
             {
 
             {
 
                 DisplayFloatyMessage(oOwner, GetName(oItem) + " added", FLOATY_MESSAGE, 14654488, 10.0);
 
                 DisplayFloatyMessage(oOwner, GetName(oItem) + " added", FLOATY_MESSAGE, 14654488, 10.0);
 
             }
 
             }
             //Now we didn't handle the event itself but just displayed a floaty --> let the original event handler take over.
+
 
             HandleEvent(ev); // this passes the event to the default handler of the event's target object
+
             // Since this script doesn't handle the event itself, but merely adds functionality (displaying the message),
 +
             // the event is passed back to its default event handler for the event's target object
 +
            HandleEvent(evCurrent);
 +
 
 
             break;
 
             break;
 
         }
 
         }
 
 
     }
 
     }
 +
 
}
 
}
  
 
// --- script end ---
 
// --- script end ---
 
</dascript>
 
</dascript>
Note that the "default handler" is the script specified on the resource template. So, even if events.xls is used to make a global change, HandleEvent(ev) ensures that any custom script on an object template is still executed.  
+
Note that the "default handler" is the script specified on the resource template. So, even if events.xls is used to make a global change, <code>HandleEvent(evCurrent)</code> ensures that any custom script on an object template is still executed.  
  
 
This is especially useful for compatibility purposes. If mods that affect all campaigns use events.xls, while custom campaigns use custom scripts on resource templates, the two can coexist.  
 
This is especially useful for compatibility purposes. If mods that affect all campaigns use events.xls, while custom campaigns use custom scripts on resource templates, the two can coexist.  
 
{{Languages}}
 
{{Languages}}
 
[[Category:Events]]
 
[[Category:Events]]

Revision as of 16:18, 8 May 2014

Scripting

The broadest definition of overriding an event means redirecting it from the default handler (or script) to some form of a custom handler.

Via a wrapper script

The safest and most common way to override an event is to create an event script for the resource whose event-handling you want to override, and use that script to handle the specific event you want to override while passing on all other events to the core script to handle.

For example, if you have a creature called a "Death Tyrant" that does something special when it receives EVENT_TYPE_DEATH, you could create a script named death_tyrant_events with the following layout:

#include "utility_h"
#include "wrappers_h" 
#include "events_h"
 
void main()
{
    int nEventHandled;
 
    // decompose the event
    event evCurrent = GetCurrentEvent();
    int nEventType = GetEventType(evCurrent);
 
    // common variable
    object oHero = GetHero();
    object oParty = GetParty(oHero);   
 
    switch(nEventType)
    {
         case EVENT_TYPE_DEATH:
         {
             // your custom code goes here
 
             // set nEventHandled if no further action is required for this event
             nEventHandled = TRUE; 
 
             break;
         }
    }
 
    // if this event wasn't handled by this script, let the core script try
    if(!nEventHandled)
    {
        HandleEvent(ev, RESOURCE_SCRIPT_CREATURE_CORE);
    }
 
}

And set this script as the event-handling script for Death Tyrants. When the game sends the Death Tyrant any event other than EVENT_TYPE_DEATH, it will get sent to the core creature_core event-handling script by the function call HandleEvent(ev, RESOURCE_SCRIPT_CREATURE_CORE). However EVENT_TYPE_DEATH will be intercepted and handled by your own code.

Via the events 2DA

This is how you can do the same thing on a global scale, catching and overriding an event for all resources in the game.

Create a engineevents m2da fragment (see extending the game via m2da files for details).

Events.png

In this case the EVENT_TYPE_INVENTORY_ADDED event will be overridden and handled in a custom script: <code<my_event_override</code>

This custom script can do whatever is required, for example, displaying a message on screen every time the Player picks up an item (or steal one).

#include "utility_h"
#include "wrappers_h"
#include "events_h"
 
void main()
{
    // decompose the event
    event evCurrent = GetCurrentEvent();
    int nEventType = GetEventType(evCurrent);
    object oOwner = GetEventCreator(evCurrent);
 
    switch(nEventType)
    {
        case EVENT_TYPE_INVENTORY_ADDED:
        {
            object oItem = GetEventObject(ev, 0);
 
             // only show if the item went into the player's inventory
            if(IsPartyMember(oOwner))
            {
                DisplayFloatyMessage(oOwner, GetName(oItem) + " added", FLOATY_MESSAGE, 14654488, 10.0);
            }
 
            // Since this script doesn't handle the event itself, but merely adds functionality (displaying the message),
            // the event is passed back to its default event handler for the event's target object
            HandleEvent(evCurrent); 
 
            break;
        }
    }
 
}
 
// --- script end ---

Note that the "default handler" is the script specified on the resource template. So, even if events.xls is used to make a global change, HandleEvent(evCurrent) ensures that any custom script on an object template is still executed.

This is especially useful for compatibility purposes. If mods that affect all campaigns use events.xls, while custom campaigns use custom scripts on resource templates, the two can coexist.

Language: English  • русский