Skip to content

ModularBehaviorScript ​

Inherits: DatabaseResource < Resource

ModularBehaviorScript coordinates behavior schedules and reactions for entities. Clean signal-based version that doesn't require references to state components. Communicates purely through signals for better decoupling.

Properties ​

boolenabledtrue
Array[TaskSchedule]schedules[]
Array[BehaviorReaction]schedule_reactions[]
FallbackBehaviorTypefallback_behaviorFallbackBehaviorType.WANDER
floatwander_radius5.0
boolreturn_to_spawntrue

Variables ​

Entityentity
TaskSchedulecurrent_schedulenull
TaskSchedulefallback_schedulenull
TaskScheduleprevious_schedulenull
floatlast_evaluation_time0.0
floatevaluation_interval2.0

Methods ​

voidsetup( system_hub: GameHost.SystemHub, entity_ref: Entity )
boolis_held_by_reaction()
voidreevaluate_now()
boolset_active_schedule( new_schedule: TaskSchedule )
boolset_active_schedule_by_index( index: int )
boolactivate_schedule_by_name( schedule_name: String )
TaskScheduleget_current_schedule()
voidreset_current_schedule()
BehaviorTaskget_current_task()
voidpause_behavior()
voidresume_behavior()
boolis_behavior_active()
voidprocess_behavior( delta: float )
voidon_player_detection_changed( player_nearby: bool )
voidhandle_formation_update()
voidhandle_entity_death()
intget_id()
voidset_id( new_id: int )
Dictionaryto_save_data()
voidfrom_save_data( save_data: Dictionary )
voidcleanup()

Signals ​

schedule_changed( old_schedule: TaskSchedule, new_schedule: TaskSchedule ) ​

Emitted when the active schedule changes

behavior_paused() ​

Emitted when behavior is paused

behavior_resumed() ​

Emitted when behavior is resumed

schedule_activation_failed( schedule_name: String, reason: String ) ​

Emitted when a schedule activation fails

Enumerations ​

enum FallbackBehaviorType ​

The options of fallback behavior type: wander, move to spawn, idle at current, follow nearest ally.

  • WANDER = 0 - Auto-create WanderSchedule with specified radius
  • MOVE_TO_SPAWN = 1 - Single task: move to spawn point then idle
  • IDLE_AT_CURRENT = 2 - Single task: idle with animations
  • FOLLOW_NEAREST_ALLY = 3 - For pets without orders

Property descriptions ​

Script Settings

bool enabled = true ​

Whether this script is currently enabled

Array[TaskSchedule] schedules = [] ​

Available schedules in priority order (index 0 = highest priority)

Array[BehaviorReaction] schedule_reactions = [] ​

Event-driven reactions that can switch schedules

Fallback Behavior

FallbackBehaviorType fallback_behavior = FallbackBehaviorType.WANDER ​

What to do when no schedule is valid or available

float wander_radius = 5.0 ​

Radius for wander fallback behavior

bool return_to_spawn = true ​

Whether to return to spawn point for fallback

Variable descriptions ​

Entity entity ​

Reference to the entity using this script

TaskSchedule current_schedule = null ​

Currently active schedule

TaskSchedule fallback_schedule = null ​

Fallback schedule created automatically

TaskSchedule previous_schedule = null ​

Previous schedule (for reaction returns)

float last_evaluation_time = 0.0 ​

Last time we evaluated schedule conditions

float evaluation_interval = 2.0 ​

Evaluation interval to prevent excessive checking

Method descriptions ​

void setup( system_hub: GameHost.SystemHub, entity_ref: Entity ) ​

Initialize the behavior script with entity reference only

bool is_held_by_reaction() ​

Is a reaction that switched the schedule still in charge (it is until it returns, or for good when it does not return)

void reevaluate_now() ​

Picks the schedule that fits now, without waiting for the next periodic check

bool set_active_schedule( new_schedule: TaskSchedule ) ​

Set the active schedule with signal emission

bool set_active_schedule_by_index( index: int ) ​

Set active schedule by index

bool activate_schedule_by_name( schedule_name: String ) ​

Find and activate schedule by name

TaskSchedule get_current_schedule() ​

Get the currently active schedule

void reset_current_schedule() ​

Reset current schedule to beginning

BehaviorTask get_current_task() ​

Get the current task being executed

void pause_behavior() ​

Pause behavior processing

void resume_behavior() ​

Resume behavior processing

bool is_behavior_active() ​

Check if behavior is currently active

void process_behavior( delta: float ) ​

Called every frame to process behavior logic

void on_player_detection_changed( player_nearby: bool ) ​

Handle player detection changes (called via signal)

void handle_formation_update() ​

Handle formation updates (called via signal)

void handle_entity_death() ​

Handle entity death (called via signal)

int get_id() ​

Get the ID of this behavior script

void set_id( new_id: int ) ​

Set the ID of this behavior script

Dictionary to_save_data() ​

Save runtime state for persistence

void from_save_data( save_data: Dictionary ) ​

Restore runtime state from saved data

void cleanup() ​

Cleanup all resources when script is no longer needed