Jump to content

EVENT scripts: Difference between revisions

Added some more information about GAMEEVENT_ACTOR_SPAWNED and GAMEEVENT_ACTOR_DAMAGED, and added an example.
(Added the new event types in 3.1-alpha and a section that covers more information about GAMEEVENT_ACTOR_SPAWNED and GAMEEVENT_ACTOR_DAMAGED.)
(Added some more information about GAMEEVENT_ACTOR_SPAWNED and GAMEEVENT_ACTOR_DAMAGED, and added an example.)
Line 44: Line 44:
*AAPTR_DAMAGE_TARGET: Accesses the "victim" taking the damage. By default, this actor is already the activator of the script.
*AAPTR_DAMAGE_TARGET: Accesses the "victim" taking the damage. By default, this actor is already the activator of the script.
You may use these in ACS functions that deal with actor pointers, such as [https://zdoom.org/wiki/IsPointerEqual IsPointerEqual], [https://zdoom.org/wiki/SetActivator SetActivator], [https://zdoom.org/wiki/SetPointer SetPointer], or [https://zdoom.org/wiki/Warp Warp].
You may use these in ACS functions that deal with actor pointers, such as [https://zdoom.org/wiki/IsPointerEqual IsPointerEqual], [https://zdoom.org/wiki/SetActivator SetActivator], [https://zdoom.org/wiki/SetPointer SetPointer], or [https://zdoom.org/wiki/Warp Warp].
It's recommended not to use '''GAMEEVENT_ACTOR_SPAWNED''' or '''GAMEEVENT_ACTOR_DAMAGED''' in conjunction with <tt>CLIENTSIDE</tt> scripts, since the server must tell clients to execute the scripts every time the events are fired. Due to their frequent occurrence, this can create lots of network traffic, resulting in a huge drop in performance. Furthermore, the extra actor pointers available in the latter, along with the damage type stored in <tt>arg2</tt>, do not work properly in <tt>CLIENTSIDE</tt> scripts.
[[Category:Level Development]]
[[Category:Level Development]]
== Examples ==
Let's say we want to use '''GAMEEVENT_ACTOR_SPAWNED''' and '''GAMEEVENT_ACTOR_DAMAGED''' for our mod. For the sake of simplicity, we want the events to be triggered by all actors, so we'll create a <tt>MAPINFO</tt> lump in our WAD file and add the following:
<syntaxhighlight lang="c" line="1">
GameInfo
{
    ForceSpawnEventScripts = true
    ForceDamageEventScripts = true
}
</syntaxhighlight>
Next, we'll create an ACS EVENT script that prints a message whenever an actor is spawned or takes damage. '''GAMEEVENT_ACTOR_DAMAGED''' gives us access to a lot of information about how an actor takes damage, so let's also print that stuff.
<syntaxhighlight lang="c" line="1">
script "ActorEvent" (int type, int arg1, int arg2) EVENT
{
    if (type == GAMEEVENT_ACTOR_SPAWNED)
    {
        // This prints every time an actor is spawned.
        PrintBold(s:GetActorClass(0), s:" has spawned!");
    }
    else if (type == GAMEEVENT_ACTOR_DAMAGED)
    {
        // This event is called just before the actor's health is reduced. We can get
        // their new health by subtracting their current health by arg1.
        int oldHealth = GetActorProperty(0, APROP_HEALTH);
        int newHealth = oldHealth - arg1;
        // Construct a string that displays everything.
        str message = StrParam(s:GetActorClass(0), s: " got damaged by ", d:arg1, s:" points! The damage type is: ", s:arg2);
        message = StrParam(s:message, s:"\nTheir health was ", d:oldHealth, s:", now it's ", d:newHealth);
        // Print the names of the source and inflictor classes, if they exist.
        if (SetActivator(0, AAPTR_DAMAGE_SOURCE));
            message = StrParam(s:message, s:"\nThe source was ", s:GetActorClass(0));
        if (SetActivator(0, AAPTR_DAMAGE_INFLICTOR));
            message = StrParam(s:message, s:"\nThe inflictor was ", s:GetActorClass(0));
        // Set the activator back to the target.
        SetActivator(0, AAPTR_DAMAGE_TARGET);
        // Print the message!
        PrintBold(s:message);
    }
}
</syntaxhighlight>
[[Category:ACS Functions]]
[[Category:ACS Functions]]