Skip to content

Callbacks

BenevolusGoat edited this page Jan 27, 2025 · 2 revisions

Trigger your own code during certain events related to the status effects

Adding callbacks

Adding callbacks within your mod is quite simple! It works similarly to how you would add a normal callback for your mod.

StatusEffectLibrary.AddCallback(string callbackID, function callback, any optionalArg)

  • callbackID: See available callback IDs below for available options.
  • callback: The function that the callbackID should call upon when it's triggered.
  • optionalArg: If applicable, provide a variable specific to the callback to help narrow down when the callback runs. For this library, this is almost always available as a StatusFlag.

Below is an example from Epiphany with their "Psychic Collapse" effect

local SEL = StatusEffectLibrary

---@param ent Entity
---@param customData table
function COLLAPSE:PreAddEffect(ent, _, customData)
	--Only works if the effect is already active, controlling how it functions when the effect is applied multiple times
	local statusEffectData = SEL:GetStatusEffectData(ent, SEL.StatusFlag.EP_PSYCHIC_COLLAPSE)
	if not statusEffectData then return end
	statusEffectData.Countdown = math.max(statusEffectData.Countdown, customData.Countdown)
	statusEffectData.CustomData.Damage = statusEffectData.CustomData.Damage + customData.Damage
	return true
end

SEL.Callbacks.AddCallback(
	StatusEffectLibrary.Callbacks.ID.PRE_ADD_ENTITY_STATUS_EFFECT,
	COLLAPSE.PreAddEffect,
	SEL.StatusFlag.EP_PSYCHIC_COLLAPSE
)

Callback IDs

StatusEffectLibrary.Callbacks.ID.ENTITY_STATUS_EFFECT_UPDATE - function(table modRef, EntityNPC | EntityPlayer entity, integer statusEffects)

Called on MC_POST_PEFFECT_UPDATE/MC_NPC_UPDATE for players/enemies respectively while having any custom status effect applied. You can provide a StatusFlag as an optional argument to have this callback only run for a specific status effect.

  • modRef: StatusEffectLibrary Mod Global.
  • entity: The affected enemy/player.
  • statusEffects: Bitflag of all applied status effects.

StatusEffectLibrary.Callbacks.ID.PRE_ADD_ENTITY_STATUS_EFFECT- function(table modRef, EntityNPC | EntityPlayer entity, StatusFlag statusEffect, table customData)

Called before a custom status effect is applied to an entity. Can return any value other than nil to stop it from being applied. You can provide a StatusFlag as an optional argument to have this callback only run for a specific status effect.

  • modRef: StatusEffectLibrary Mod Global.
  • entity: The affected enemy/player.
  • statusEffect: Bitflag of the status effect being applied.
  • customData: Table of arbitrary data that was passed when applying the status effect.

StatusEffectLibrary.Callbacks.ID.POST_ADD_ENTITY_STATUS_EFFECT- function(table modRef, EntityNPC | EntityPlayer entity, StatusFlag statusEffect, StatusEffectData statusEffectData)

Called after a custom status effect is applied to an entity. You can provide a StatusFlag as an optional argument to have this callback only run for a specific status effect.

  • modRef: StatusEffectLibrary Mod Global.
  • entity: The affected enemy/player.
  • statusEffect: Bitflag of the status effect being applied.
  • statusEffectData: Table of data related to the status effect.

StatusEffectLibrary.Callbacks.ID.PRE_REMOVE_ENTITY_STATUS_EFFECT- function(table modRef, EntityNPC | EntityPlayer entity, StatusFlag statusEffect, StatusEffectData statusEffectData)

Called before a custom status effect is removed from an entity. You can provide a StatusFlag as an optional argument to have this callback only run for a specific status effect.

  • modRef: StatusEffectLibrary Mod Global.
  • entity: The affected enemy/player.
  • statusEffect: Bitflag of the status effect being removed.
  • statusEffectData: Table of data related to the status effect.

StatusEffectLibrary.Callbacks.ID.POST_REMOVE_ENTITY_STATUS_EFFECT- function(table modRef, EntityNPC | EntityPlayer entity, StatusFlag statusEffect, StatusEffectData statusEffectData)

Called after a custom status effect is removed from an entity. You can provide a StatusFlag as an optional argument to have this callback only run for a specific status effect.

  • modRef: StatusEffectLibrary Mod Global.
  • entity: The affected enemy/player.
  • statusEffect: Bitflag of the status effect being removed.
  • statusEffectData: Table of data related to the status effect.