Skip to content

StatsComponent ​

Inherits: RefCounted

The numbers of one entity: its stats, pools, immunities and status effect tracking, and the entry point of the damage and healing pipeline.

Description ​

Every Entity owns one (entity.components.stats()), built from a StatsData. It holds a StatInstance for every stat in the database (the core stats included), a PoolInstance for each of the entity's pools, the damage layers that decide which pool takes damage in which order, the ImmunityComponent and the StatusEffectComponent. It never talks to the interface directly: it emits signals, which the EntityComponentMediator forwards to the entity and the event system. take_damage and take_healing complete a DamageResult or HealingResult: the target's calculation phase, the pools, the death check.

Variables ​

CombatManagercombat_manager
ChronoManagerchrono_manager
Dictionarystat_instances{}
Array[EffectInstance]active_redirection_effects[]
Array[PoolInstance]health_pools[]
Dictionaryresource_pools{}
Dictionary:core_stats
ImmunityComponentimmunity_component
StatusEffectComponentstatus_effect_component
intmaster_pool_id0
boolis_deadfalse
boolcurrent_combat_statefalse
Variantowner_entitynull
intlevel1
Array[int]multiplier_stats[] # Cache of multiplier stat names
Dictionarymultiplier_bonuses{}
intdefault_growth_profile_id0
Array[DamageLayer]damage_layers[]
Array[Dictionary]heal_absorbs[]
Array[EffectInstance]done_boosts[]

Methods ​

voidsetup_from_stats_data( stats_data: StatsData )
voidset_level( new_level: int )
floatgrowth_of( definition: Resource, context: FormulaContext = null )
voidrefresh_conditional_effects()
floatmodify_gain( channel: String, amount: float )
intmodify_gain_int( channel: String, amount: int, probabilistic: bool = false )
Dictionaryapply_status_effect( status_effect_id: int, base_duration: float, source_entity: Variant = null, effect: EffectInstance = null )
boolis_immune_to_status_effect( status_effect_id: int )
boolis_immune_to_damage_type( damage_type: int )
boolis_immune_to_school_type( school_id: int )
boolcan_apply_school_effect( school_id: int, source_entity: Entity = null, effect: EffectInstance = null )
boolactivate_immunity( immunity_id: int, duration: float = -1.0 )
booldeactivate_immunity( immunity_id: int )
voidclear_all_immunities()
boolhas_weapon_equipped()
floatget_weapon_damage()
floatroll_weapon_damage()
floatget_swing_timer()
floatget_attacks_per_second()
floatget_damage_per_second()
Dictionaryget_attack_speed_info()
voidreact_to_hit_dealt( result: DamageResult )
voidreact_to_hit_received( result: DamageResult )
floatget_stat( stat_id: int )
boolset_stat_base( stat_id: int, value: float )
booladd_stat_bonus( stat_id: int, amount: float )
boolmodify_stat_multiplier( stat_id: int, amount: float )
boolset_stat_multiplier( stat_id: int, multiplier: float )
boolhas_stat( stat_id: int )
Array[int]get_stat_ids_in_group( group_id: int )
floatget_pool_value( pool_id: int, value_type: String = "current" )
PoolInstanceget_pool( pool_id: int )
boolhas_pool( pool_id: int )
Array[PoolInstance]get_all_pools()
Array[int]get_all_pool_ids()
boolis_master_pool( pool_id: int )
intget_base_resource_pool_id()
boolis_resource_pool( pool_id: int )
voidadd_health_pool( pool_instance: PoolInstance )
voidadd_resource_pool( pool_instance: PoolInstance )
boolremove_health_pool( pool_id: int )
boolremove_health_pool_instance( pool: PoolInstance )
voidadd_damage_layer( layer: DamageLayer )
voidremove_damage_layer( layer: DamageLayer )
voidremove_damage_layers_of_pool( pool: PoolInstance )
Array[DamageLayer]get_ordered_damage_layers()
boolremove_resource_pool( pool_id: int )
PoolInstanceget_master_pool()
PoolInstanceget_health_pool( pool_id: int )
PoolInstanceget_resource_pool( pool_id: int )
DamageResulttake_damage( result: DamageResult )
voidregister_redirection_effect( effect_instance: EffectInstance )
voidunregister_redirection_effect( effect_instance: EffectInstance )
HealingResulttake_healing( result: HealingResult )
voidadd_done_boost( instance: EffectInstance )
voidremove_done_boost( instance: EffectInstance )
floatapply_done_boosts( calculation_type: int, value: float, context: Dictionary, steps: Array[ModifierStep] )
voidadd_heal_absorb( instance: EffectInstance, amount: float )
voidremove_heal_absorb( instance: EffectInstance )
floatget_heal_absorb_total()
Dictionarycan_consume_resource( pool_id: int, amount: float )
Dictionaryconsume_resource( pool_id: int, amount: float )
floatrestore_resource( pool_id: int, amount: float )
Dictionaryto_save_data()
voidfrom_save_data( save_data: Dictionary, entity: Variant = null )
voidrestore_pool_values_after_equipment()
voidcleanup()
voidrestore_all_pools_to_max()
boolis_master_pool_depleted()
Arrayget_ability_modifier_entries( ability_def: AbilityDefinition, property_name: String )

Signals ​

pool_added( pool_instance: PoolInstance, pool_type: String ) ​

Pool events

pool_removed( pool_id: int, pool_type: String ) ​

Emitted when a pool is removed.

pool_value_changed( pool_id: int, current_value: float, max_value: float ) ​

Emitted when the value of a pool changes: the pool, the current and the maximum value.

pool_overfill_changed( pool_id: int, overfill_amount: float ) ​

Emitted when the overfill of a pool changes.

pool_depleted( pool_id: int ) ​

Emitted when a pool reaches zero.

pool_restored( pool_id: int ) ​

Emitted when a pool goes up from zero.

master_pool_depleted() ​

Emitted when the master pool (health) reaches zero: the entity dies.

damage_incoming( damage_amount: float, damage_type: int, damage_source: Entity, effect: EffectInstance ) ​

Combat signals

damage_taken( damage_amount: float, damage_source: Entity, effect: EffectInstance, damage_type: int ) ​

Emitted when the entity takes damage: the amount, the source, the effect and the damage type.

damage_absorbed_by_pool() ​

Protective pool absorption (shields, barriers)

healing_applied( pool_id: int, amount: float, healing_source: Entity, effect: EffectInstance ) ​

Healing signals

stat_changed( stat_id: int, old_value: float, new_value: float ) ​

Stat events

ability_property_modification_requested( ability_name: String, property_name: String, effect_value: float, calculation_type: int ) ​

Ability property modification signal

immunity_activated( immunity_id: int, duration: float, immunity_type: ImmunityDefinition.ImmunityType, protected_targets: Array[int] ) ​

Immunity signals (forwarded from ImmunityComponent)

immunity_deactivated( immunity_id: int, immunity_type: ImmunityDefinition.ImmunityType, protected_targets: Array[int] ) ​

Emitted when a temporary immunity ends.

school_effect_blocked_by_immunity( school_name: String, immunity_id: int, source_entity: Entity, effect: EffectInstance ) ​

School effect blocking (forwarded from ImmunityComponent)

Enumerations ​

enum CoreStats ​

The options of core stats: movement speed, attack speed, global cooldown duration, weapon speed, cast speed, bag slots, sight range, weapon damage, weapon damage variance, carry capacity.

  • MOVEMENT_SPEED = 1
  • ATTACK_SPEED = 2
  • GLOBAL_COOLDOWN_DURATION = 3
  • WEAPON_SPEED = 4
  • CAST_SPEED = 5
  • BAG_SLOTS = 6
  • SIGHT_RANGE = 7
  • WEAPON_DAMAGE = 8
  • WEAPON_DAMAGE_VARIANCE = 9
  • CARRY_CAPACITY = 10

Constants ​

  • int MAX_MULTIPLIER_ITERATIONS = 10 - Safety limit for convergence

Variable descriptions ​

CombatManager combat_manager ​

========== CORE PROPERTIES ==========

ChronoManager chrono_manager ​

The manager of timers.

Dictionary stat_instances = ​

The stats of the entity, by stat id.

Array[EffectInstance] active_redirection_effects = [] ​

The effects that send damage to someone else.

Array[PoolInstance] health_pools = [] ​

Pool systems

Dictionary resource_pools = ​

The resource pools of the entity, by pool id.

Dictionary: core_stats ​

The totals of all core stats (read-only snapshot)

ImmunityComponent immunity_component ​

Immunity systems

StatusEffectComponent status_effect_component ​

The component that tracks the status effects.

int master_pool_id = 0 ​

Configuration

bool is_dead = false ​

True when the master pool is empty.

bool current_combat_state = false ​

Combat state tracking (updated by mediator)

Variant owner_entity = null ​

The entity this component belongs to (set by the component registry). Formulas read its level from it

int level = 1 ​

The level growth and level-scaled formulas read (set by set_level; level 1 until the entity says otherwise)

Array[int] multiplier_stats = [] # Cache of multiplier stat names ​

========== MULTIPLIER TRACKING ==========

Dictionary multiplier_bonuses = ​

Track which stats have multiplier bonuses applied

int default_growth_profile_id = 0 ​

The profile that closes the chain of this entity (a GrowthProfile id, 0 = none). The NPCs get the project's default (Gameplay Config), set before setup_from_stats_data

Array[DamageLayer] damage_layers = [] ​

The pools that take damage, with the order they take it in. Health and shields have a permanent layer from their definition; an effect can give any pool a temporary one (a mana shield). Damage goes to the layers by priority (highest first, equal priorities newest first); what no layer takes is overkill.

Array[Dictionary] heal_absorbs = [] ​

The heal absorbs on this entity (HealAbsorbEffect), oldest first: {"instance": EffectInstance, "remaining": float}

Array[EffectInstance] done_boosts = [] ​

The AbilityBoostEffect instances that raise what this entity deals or heals with some abilities and effects

Method descriptions ​

void setup_from_stats_data( stats_data: StatsData ) ​

Builds the component from a StatsData: the immunity and status components, the pools, an instance for every stat of the database, the base values and active states, the core stat overrides, the permanent immunities, then the capacity bonuses and multiplier effects of the stats

void set_level( new_level: int ) ​

Tells the stats the entity's level (Entity.current_level calls this whenever it changes, and once after the components are built). Recomputes everything that depends on it: the growth of every stat, the growth of every pool maximum, and the stat effects whose formulas scale with the level. Pools whose maximum changes follow the level-up capacity rule (GameplayConfig, Pools), not the equipment rule. Stat and pool signals fire as usual.

float growth_of( definition: Resource, context: FormulaContext = null ) ​

The level growth of a stat or pool at the current level, from the first layer that has an answer: the entity's own override, then the growth profiles (its own, the parents, the project default). Growth belongs to the entity only: a stat or pool no layer lists does not grow. The weapon damage of an entity without an intrinsic weapon does not grow

void refresh_conditional_effects() ​

Recalculates the multiplier and pool effects whose stat effects have conditions, after the state they depend on (health, combat) changed

float modify_gain( channel: String, amount: float ) ​

What this entity gains on channel when amount is given to it: the amount after every GainModifierStatEffect of its stats. Flat additions first, then the percentages together, then the multiplies; never below 0. With no effect on the channel the amount comes back unchanged. See docs/systems/entity-stats.md, section 24.2.

int modify_gain_int( channel: String, amount: int, probabilistic: bool = false ) ​

modify_gain for whole numbers (experience, gold, item counts): rounds to the nearest whole number, except that a fraction of an item is a chance of one more when probabilistic is set (1.2 items = 20 % chance of 2)

Dictionary apply_status_effect( status_effect_id: int, base_duration: float, source_entity: Variant = null, effect: EffectInstance = null ) ​

Asks the status component to apply a status effect definition for base_duration seconds. The duration first goes through the status duration gain channel (tenacity). Returns the dictionary of process_status_effect_application (can_apply, effective_duration ...), or can_apply false with an error when the component is missing

bool is_immune_to_status_effect( status_effect_id: int ) ​

Is the entity immune to this status effect definition right now?

bool is_immune_to_damage_type( damage_type: int ) ​

Is the entity immune to this damage type right now?

bool is_immune_to_school_type( school_id: int ) ​

Is the entity immune to this ability school right now?

bool can_apply_school_effect( school_id: int, source_entity: Entity = null, effect: EffectInstance = null ) ​

Can an effect of this school be applied to the entity? False (and school_effect_blocked_by_immunity is emitted) while a school immunity covers it

bool activate_immunity( immunity_id: int, duration: float = -1.0 ) ​

Switches an immunity (ImmunityDefinition id) on for duration seconds (0 or less: 10 seconds). Returns false when it could not be activated

bool deactivate_immunity( immunity_id: int ) ​

Switches an immunity off before its time

void clear_all_immunities() ​

Ends every immunity and forgets the status effect immunity state

bool has_weapon_equipped() ​

True when the weapon damage core stat is above 0

float get_weapon_damage() ​

The weapon damage core stat

float roll_weapon_damage() ​

A weapon hit: the weapon damage, plus or minus a random amount up to the weapon damage variance core stat

float get_swing_timer() ​

Seconds between weapon attacks: the weapon speed divided by the attack speed (3.0 and 1.0 when those are 0)

float get_attacks_per_second() ​

1 divided by the swing timer

float get_damage_per_second() ​

Weapon damage times attacks per second

Dictionary get_attack_speed_info() ​

A dictionary with the weapon speed, the attack speed, the swing timer, the attacks per second and the damage per second, for tooltips

void react_to_hit_dealt( result: DamageResult ) ​

A hit this entity dealt was resolved (entity_hit_dealt): damage-dealt and kill restoration

void react_to_hit_received( result: DamageResult ) ​

A hit on this entity was resolved (entity_hit_received): damage-taken restoration and reflection

float get_stat( stat_id: int ) ​

The total of a stat ((base + growth + bonus) x multiplier), or 0 when the entity does not have it. Multiplier effects of other stats are already inside the bonus

bool set_stat_base( stat_id: int, value: float ) ​

Sets the base of a stat; bonuses (gear, effects) and the multiplier stay

bool add_stat_bonus( stat_id: int, amount: float ) ​

Adds amount to the bonus of a stat (negative to take it away). Equipment and effects work through it. False when the entity does not have the stat

bool modify_stat_multiplier( stat_id: int, amount: float ) ​

Adds amount to the multiplier of a stat (it starts at 1.0, so +0.5 makes x1.5)

bool set_stat_multiplier( stat_id: int, multiplier: float ) ​

Sets the multiplier of a stat outright (1.0 = x1)

bool has_stat( stat_id: int ) ​

Does the entity have this stat?

Array[int] get_stat_ids_in_group( group_id: int ) ​

The stats this entity has that are in a stat group (StatGroupDefinition), by id

float get_pool_value( pool_id: int, value_type: String = "current" ) ​

A value of a pool: "current" (overfill included), "max" (overfill capacity included), "base_max", "effective_max", "percentage" (of the effective maximum), "normal_percentage" or "overfill". 0 for a pool the entity does not have

PoolInstance get_pool( pool_id: int ) ​

A pool by its definition id, health pools first, or null

bool has_pool( pool_id: int ) ​

Does the entity have this pool?

Array[PoolInstance] get_all_pools() ​

Every pool of the entity, health pools first (in their order), then the resource pools. Health pools and resource pools are two stores because the editor and the nameplate group them that way; nothing else depends on which store a pool is in

Array[int] get_all_pool_ids() ​

The definition ids of every pool, health pools first

bool is_master_pool( pool_id: int ) ​

Is this the master pool: the one whose depletion kills the entity?

int get_base_resource_pool_id() ​

The id of the first resource pool, or 0 when there is none

bool is_resource_pool( pool_id: int ) ​

Is this pool in the resource store (mana, rage) and not in the health store?

void add_health_pool( pool_instance: PoolInstance ) ​

Adds a pool to the health store. When its definition absorbs damage it gets its permanent damage layer. Emits pool_added

void add_resource_pool( pool_instance: PoolInstance ) ​

Adds a pool to the resource store. Emits pool_added

bool remove_health_pool( pool_id: int ) ​

Removes a health pool and the damage layers of it. False when the entity does not have it

bool remove_health_pool_instance( pool: PoolInstance ) ​

Removes one health pool instance (not "the first pool of this definition": an entity can hold two shields of one kind)

void add_damage_layer( layer: DamageLayer ) ​

Adds a damage layer: a pool that takes part of the damage the entity suffers

void remove_damage_layer( layer: DamageLayer ) ​

Removes one damage layer

void remove_damage_layers_of_pool( pool: PoolInstance ) ​

Removes every damage layer that belongs to a pool

Array[DamageLayer] get_ordered_damage_layers() ​

The layers in the order they take damage

bool remove_resource_pool( pool_id: int ) ​

Removes a resource pool and the damage layers of it. False when the entity does not have it

PoolInstance get_master_pool() ​

The pool whose depletion kills: the master pool, or the first health pool when none is set. Use this where code wants "the main health" (never health_pools[0]: shields and other pools can come first)

PoolInstance get_health_pool( pool_id: int ) ​

A pool of the health store by its definition id, or null

PoolInstance get_resource_pool( pool_id: int ) ​

A pool of the resource store by its definition id, or null

DamageResult take_damage( result: DamageResult ) ​

Completes the DamageResult for a hit on this entity: redirection, damage immunity, the defender's calculation phase (dodge, block, armor), protective pools, health and death. The attacker's phase has already run, so result.taken holds the damage done. Returns the same result, or null when a script error aborted the function (the caller turns null into a FAILED result).

void register_redirection_effect( effect_instance: EffectInstance ) ​

Registers a damage redirection effect (a guardian) that gets the first look at the damage the entity takes

void unregister_redirection_effect( effect_instance: EffectInstance ) ​

Forgets a damage redirection effect

HealingResult take_healing( result: HealingResult ) ​

Completes the HealingResult for a heal on this entity: the target's calculation phase (healing taken modifiers), then the pools, then the death check (a heal can revive). The healer's phase has already run, so result.taken holds the healing done. Returns the same result, or null when a script error aborted the function.

void add_done_boost( instance: EffectInstance ) ​

A boost is now on this entity (the instance of an AbilityBoostEffect)

void remove_done_boost( instance: EffectInstance ) ​

The boost of this instance ends

float apply_done_boosts( calculation_type: int, value: float, context: Dictionary, steps: Array[ModifierStep] ) ​

Applies the boosts that fit to the number of a damage done or healing done calculation, after its modifiers. Flat first, then the percentage, each stack adding its share; a step is recorded for every boost that changed the number. The ability and the effects come from the context

void add_heal_absorb( instance: EffectInstance, amount: float ) ​

A heal absorb of amount is now on this entity; it soaks up the healing the entity receives until it is used up

void remove_heal_absorb( instance: EffectInstance ) ​

The heal absorb of instance ends (its effect ended, or it was used up)

float get_heal_absorb_total() ​

How much healing the heal absorbs on this entity can still soak up

Dictionary can_consume_resource( pool_id: int, amount: float ) ​

Can the resource pool pay amount? A dictionary with can_consume, requested, available and shortage

Dictionary consume_resource( pool_id: int, amount: float ) ​

Spends amount from a resource pool. A dictionary with success, consumed and remaining; the check result when it could not be paid

float restore_resource( pool_id: int, amount: float ) ​

Adds amount to a resource pool and returns what was added

Dictionary to_save_data() ​

The state a save keeps: pool values, stat bases and the immunities that are running. Bonuses and multipliers come back from equipment and effects

void from_save_data( save_data: Dictionary, entity: Variant = null ) ​

Restores the state of to_save_data: stat bases first (capacity follows them), then immunities and the pool values

void restore_pool_values_after_equipment() ​

Applies the saved pool values again. The entity calls this after it restored its equipment: gear raises the maximum and the capacity rule could otherwise move the restored current value

void cleanup() ​

Clears the damage layers and cleans up and disconnects every pool. Called when the entity is freed

void restore_all_pools_to_max() ​

Fills every pool to its maximum (a respawn, a reset)

bool is_master_pool_depleted() ​

Is the master pool empty (the entity is dead)?

Array get_ability_modifier_entries( ability_def: AbilityDefinition, property_name: String ) ​

What the stats of this entity change on an ability's property ("cooldown_duration", "cost_amount", "gain_amount"): entries for the ability's modifier set, worked out from the stat points as they are now (nothing is stored, so nothing can go stale)