Behavior

Behavior

The base class for all behaviors. Create your own behaviors by deriving from this class. Important methods:

  • init() - Instantaneous actions when the behavior is created.
  • go() - Recurring actions performed every tick.
  • destroy() - Ends the behavior.
  • succeed() - Ends the behavior and reports success to its parent.
  • fail() - Ends the behavior and reports failure to its parent.

Extends

  • WorldcoreModel

Members

# actor :Actor

The actor that is running the behavior.

Type:
  • Actor

# parent :Behavior

The parent behavior in the behavior tree.

Type:

# tickRate :number

The tick rate in milliseconds. If a behavior doesn't return an immediate result, it will run periodically at the frequency of the tick rate. If the tick rate is set to 0, the behavior won't tick and won't run do().

Type:
  • number
Default Value:
  • 100

Methods

# init(optionsopt)

Called when the behavior is instantiated.

Parameters:
Name Type Attributes Description
options Object <optional>

Optional option object passed to set()

# set(options)

Sets one or more internal properties of the behavior. The internal name of each property will be the name of the option prefaced by "_". You should define corresponding getters to access these internal properties.

    class MyBehavior extends Behavior {
        get growth() {return this._growth || 0.5};
    }

    const behavior = MyBehavior.create();
    behavior.set({growth: 2});
Parameters:
Name Type Description
options Object

# do(delta)

This is where the work of the behavior actually happens. If you create your own behavior overload do. This will be called once per tick`.

Note: do will be called for for the first time at random interval less than the tick rate. (This is to prevent actors that spawn simultaneously from all processing their behaviors at the save time.) If you want something to happen instantly, put it in init instead of do.

Parameters:
Name Type Description
delta number

Time in milliseconds since the last invocation of do.

# destroy()

Ends the behavior. Usually you want to report succeed or fail instead.

# succeed(dataopt)

Ends the behavior and reports that it has succeeded to its parent.

Parameters:
Name Type Attributes Description
data Object <optional>

An optional data object that will be passed to the parent behavior.

# fail(dataopt)

Ends the behavior and reports that it has failed to its parent.

Parameters:
Name Type Attributes Description
data Object <optional>

An optional data object that will be passed to the parent behavior.