Using inheritance

From ZDoom Wiki
Jump to: navigation, search

Inheritance is a mechanism that lets you take all properties defined by a previous actor and only change a few of its properties, flags or states.

Inheritance has special meaning when used with monsters. Any related monsters belong to one 'species'. Monsters within the same species cannot hurt each other with projectiles. A species is defined as all monsters that have one monster as a common ancestor. For example, if you create various variations of imps which are all derived from DoomImp they will form one species. This automatic mechanism can be altered with the Species property.

Inheritance is an essential means to create new inventory items. Inventory items are derived from predefined inventory classes.


Examples:

This is a Zombie with a different attack and more health. But it inherits everything else from the already existing Doom zombie:

actor PlasmaZombie : ZombieMan 9600
{
  health 40
  dropitem Cell
  missiletype PlasmaBall
  states
  {
  Missile:
    POSS E 10 A_FaceTarget
    POSS F 5 A_MissileAttack
    POSS E 5 A_FaceTarget
    POSS F 5 A_MissileAttack
    POSS E 5 A_FaceTarget
    POSS F 5 A_MissileAttack
    goto See
  }
}


This uses SKIP_SUPER to reset the actor to default values. It only uses inheritance to get access to the parent's states:

actor DeadZombieMan : ZombieMan 18
{
  SKIP_SUPER
  States
  {
  Spawn:
    Goto Super::Death +4
  }
}


Note that using inheritance retains all states unless redefined. As an example, the ScriptedZombie below has an incomplete death animation because calling the Death state removes any previous inheritance from that state. This is a zombie which uses a script to perform its attack and opens a door when dying:

actor ScriptedZombie : ZombieMan 9604
{
  health 40
  states
  {
  Missile:
    POSS E 10 A_FaceTarget
    POSS F 5 ACS_ExecuteAlways (999,0,0)
    goto See
  Death:
    POSS A 1 Door_Open (1337, 16)
    stop
  }
}


Another possibility is to inherit from actors that have programmed capabilities. For example, Strife's LoreShot works like a grappling hook. You can easily get access to this behavior by inheriting from it and define your new actor around it:

actor GrapplingHook : LoreShot
{
  seesound "hook/shoot"
  deathsound "hook/hit"
  states
  {
  Spawn:
    WS12 AB 2 bright
    loop
  Death:
    WS12 CDEF 6
    stop
  }
}


States in parent classes which have been redefined in a subclass can still be accessed using the 'Super' scope. In this example 'Super' allows us to do stuff at the beginning of a pain state without rewriting the entire state.

 actor WimpyZombieMan : ZombieMan replaces ZombieMan
 {
   States
   {
   Pain:
     TNT1 A 0 A_ChangeFlag("FRIGHTENED", 1)
     goto Super::Pain
   }
 }


As an alternative to using 'Super,' you can use the name of the superclass whose state you want to jump to. This example has former humans calling ACS scripts when they die. It is especially useful to go farther up in the hierarchy than the direct parent:

 actor MyZombieMan : ZombieMan replaces ZombieMan
 {
   States
   {
   Death:
     TNT1 A 0 ACS_ExecuteAlways(85)
     goto Super::Death
   }
 }
 actor MyOtherZombieMan : MyZombieMan replaces ShotgunGuy
 {
   States
   {
   Death:
     TNT1 A 0 ACS_ExecuteAlways(86)
     goto ZombieMan::Death
   XDeath:
     TNT1 A 0 ACS_ExecuteAlways(86)
     goto ZombieMan::XDeath
 }