Skip to main content

Stunnable

Overview

The Stunnable component implements a damage-over-time stun mechanic: when an entity receives a cumulative amount of damage (greater than stun_threshold) within a defined time window (stun_period), the entity becomes stunned for a fixed duration. It tracks incoming damage in a rolling window, disables further stuns during a cooldown period (stun_cooldown), and emits events to signal when the stun begins and ends.

Dependencies & Tags

  • Listens to the "healthdelta" event on the entity (inst) to detect incoming damage.
  • Pushes "stunned" and "stun_finished" events to the entity.
  • No other components or tags are explicitly added or required by this script.

Properties

PropertyTypeDefault ValueDescription
damagetable{}A dictionary mapping timestamps (floats) to absolute damage amounts received. Used for computing rolling damage within stun_period.
stun_thresholdnumber1000The total damage required within stun_period to trigger a stun. Increases by stun_resist after each stun.
stun_periodnumber5The time window (in seconds) over which damage is summed to determine if stun threshold is met.
stun_durationnumber10How long (in seconds) the entity remains stunned once triggered.
stun_resistnumber150The amount added to stun_threshold after each stun, making future stuns harder to trigger.
stun_cooldownnumber60Seconds after a stun before new stun attempts are processed (i.e., TakeDamage ignores damage during this time).
valid_stun_timenumber0Timestamp indicating when the current stun cooldown ends; damage is only considered for stun potential after this time.

Main Functions

Stunnable:Stun()

  • Description: Triggers the stun state. Resets the damage log, extends the stun threshold by stun_resist, sets a cooldown timestamp, and schedules a delayed "stun_finished" event.
  • Parameters: None.

Stunnable:TakeDamage(damage)

  • Description: Records incoming damage if outside the stun cooldown; checks if the total damage in the current window exceeds stun_threshold. If so, calls Stun().
  • Parameters:
    • damage (number): The damage amount from the "healthdelta" event. Its absolute value is recorded.

Stunnable:GetDamageInPeriod()

  • Description: Calculates and returns the sum of damage recorded within the last stun_period seconds. Also prunes outdated entries from the damage table to keep memory usage reasonable.
  • Parameters: None.

Events & Listeners

  • Listens to:
    • "healthdelta" → triggers TakeDamage(data.amount)
  • Triggers:
    • "stunned" — pushed immediately upon initiating stun.
    • "stun_finished" — pushed via DoTaskInTime after stun_duration seconds.