WorldThingDied

From ZDoom Wiki
Jump to navigation Jump to search

StaticEventHandler

void WorldThingDied (WorldEvent e)

Usage

An event handler virtual function that is called when an actor is killed (its health is reduced to 0 and it enters its Death state).

Note: "killed" doesn't mean destroyed. For the cases when an actor is completely removed, see WorldThingDestroyed.

Passed values

This event gets a pointer to the WorldEvent struct and can read the following from it by preceding the value with e.:

  • Actor thing
Pointer to the actor that was killed.
  • Actor inflictor
Pointer to the actor directly responsible for the damage, which is either a puff or a projectile, if one was used (just like in DamageMobj and similar functions). Can be null.
Note: While this event doesn't get a pointer to the actor who did the kill, by default the killer is always put in the killed actor's target field, so e.thing.target pointer should point to the killer, unless the damage was dealt without a source (from example, by the level). If you need to get more detailed information, use WorldThingDamaged instead and check for e.thing.health <= 0 so it's only called when the thing dies.


Examples

This event handler puts all bosses in an array (which is globally accessible by finding the event handler with EventHandler.Find()), and removes them when they're killed (and re-adds when they're revived):

class MyEventHandler : EventHandler
{
  array<Actor> allBosses;

  override void WorldThingSpawned(WorldEvent e)
  {
    // if the actor has the ISMONSTER and BOSS flags,
    // put it into the array:
    if (e.thing.bIsMonster && e.thing.bBoss)
    {
      allBosses.Push(e.thing);
    }
  }
  
  override void WorldThingDied(WorldEvent e)
  {
    // when an actor is killed, check if it was in the array,
    // and if so, remove it from it:
    int index = allBosses.Find(e.thing);
    if (index < allBosses.Size())
    {
      allBosses.Delete(index);
    }
  }

  override void WorldThingRevived(WorldEvent e)
  {
    // if the actor is revived, put it into the array again
    // (just in case, check that it's not yet in the array):
    if (e.thing.bIsMonster && e.thing.bBoss && allBosses.Find(e.thing) == allBosses.Size())
    {
      allBosses.Push(e.thing);
    }
  }
}

// Example of reading it from elsewhere in the code:
let handler = MyEventHandler(EventHandler.Find('MyEventHandler'));
if (handler)
{
  // do something with allBosses here
}

See also