Classes:Behavior

From ZDoom Wiki
Jump to navigation Jump to search
Note: This feature is for ZScript only.

(Pull request #2908 only)

Behavior

Behaviors are a type of play-scoped Object that's meant to store behavioral patterns that an Actor can have. Behavior itself isn't meant to be used as-is but instead must be inherited from. They're meant to act as a faux component system for Actors and as such, duplicate Behaviors can't exist on Actors; instead the Behavior should be expected to handle what happens if it is refreshed. Some advantages of Behaviors include:

  • A simple but flexible API that all Actors have access to.
  • Safety of Behavior management. The only way they can be modified is through the API itself.
  • Fast access that doesn't scale with amount of Behaviors. (Access doesn't slow down the more behaviors there are loaded.)

Fields

  • readonly Actor Owner
The Actor this Behavior belongs to.
  • readonly LevelLocals Level
The level this Behavior currently resides in.

Methods

Virtual

  • void Initialize()
Called when the Behavior is first attached to its owner.
  • void Reinitialize()
Called if the Behavior is attempting to be re-added to its owner.
  • void TransferredOwner(Actor oldOwner)
Called when the Behavior is moved from one owner to another. This can happen both when the player respawns or when (un)morphing, but can also be triggered at any time via the MoveBehaviors() function on Actors.
  • oldOwner
The Actor this Behavior used to belong to. Owner is updated to the one it's currently attached to.
  • void Tick()
Called every tick of the Actor it's attached to. Note that this will still be called if NOINTERACTION is set and, if the owner is a player, while they're predicting.

Non-static Actor Methods

Note: These methods are on the Actor class itself and not the Behavior class.
  • clearscope Behavior FindBehavior(class<Behavior> type) const
Returns the specified Behavior if it exists, otherwise returning null.
  • type
The type of Behavior to look for. This is exact and can't return child classes. A BehaviorIterator should be used for getting all child class types on a given Actor.
  • bool RemoveBehavior(class<Behavior> type)
Removes the given behavior if it exists. Returns true if it was removed or false if nothing was removed.
  • type
The type of Behavior to remove. This is exact and won't remove child classes. ClearBehaviors() passed with a type should be used for that.
  • Behavior AddBehavior(class<Behavior> type)
Adds a new Behavior to the Actor, returning the Behavior it either created or found. Calls Initialize() if created and Reinitialize() if it already existed.
  • type
The type of Behavior to add.
  • void TickBehaviors()
Calls Tick() on all the Actor's existing Behaviors. This is normally done automatically but if an Actor has a full Tick() override or more control is desired for when Behaviors can tick, this can be used to handle this.
  • void ClearBehaviors(class<Behavior> type = null)
Removes all Behaviors the Actor currently has.
  • type
If not null, only remove classes of this type. Note that if null is passed, the removal will be absolute and any residual Behaviors after clean up will be automatically removed as well.
  • void MoveBehaviors(Actor from)
Transfers all the Behaviors from an existing Actor to this one and calls TransferredOwner() in the process.
  • from
The Actor to transfer the Behaviors from.

BehaviorIterator

A unique feature of Behaviors is that they can be iterated across in full since the engine will track them. This means instead of having to store a list of Actors that have a given Behavior manually, all Actors that have that Behavior can be fetched at any given time. This is useful for doing interactions across all of them at once for e.g. an XP system.

These can be used with foreach loops similar to ThinkerIterators.

Methods

Static

  • BehaviorIterator CreateFrom(Actor mobj, class<Behavior> type = null)
Creates an iterator across all Behaviors on an existing Actor.
  • mobj
The Actor to get the Behaviors from.
  • type
If not null, only get Behaviors of this type.
  • BehaviorIterator Create(class<Behavior> type = null, class<Actor> ownerType = null)
Create an iterator from all Behaviors that currently exist.
  • type
If not null, only get Behaviors of this type.
  • ownerType
If not null, only get Behaviors that have an Owner of the given type.

Non-static

  • Behavior Next()
Gets the next Behavior in the iterator, returning null if it's at the end of the list.
  • void Reinit()
Resets the iterator back to the start of the list.