Think of the three callbacks as hooks that the StatusEffect system will invoke at the right moment.
Each hook receives the target (the entity that has the effect) and, for onTick, the delta-time since the last tick.
You don’t put any logic inside StatusEffect.new; you just store the functions so the effect manager can call them later.
Example implementation:
local StatusEffect = {}
StatusEffect.__index = StatusEffect
function StatusEffect.new(name, duration, onApply, onTick, onExpire, stackable)
local self = setmetatable({}, StatusEffect)
self.Name = name
self.Duration = duration or 0
self.Elapsed = 0
self.onApply = onApply -- function(target)
self.onTick = onTick -- function(target, dt)
self.onExpire = onExpire -- function(target, wasExpired)
self.Stackable = stackable or false
return self
end
Now, somewhere in your effect manager / heartbeat loop:
-- Called once when the effect is first added to the target
function StatusEffect:Apply(target)
if self.onApply then
self.onApply(target)
end
end
-- Called every frame / step
function StatusEffect:Tick(target, dt)
self.Elapsed = self.Elapsed + dt
if self.onTick then
self.onTick(target, dt)
end
end
-- Called when the effect is removed (duration ran out or was cleansed)
function StatusEffect:Remove(target, wasExpired)
if self.onExpire then
self.onExpire(target, wasExpired)
end
end
How you structure the actual callbacks is up to you.
They can be:
- Inline lambdas (quick & dirty):
local burn = StatusEffect.new(
"Burn",
5,
function(target) target.Humanoid.Health -= 5 end, -- onApply
function(target, dt) target.Humanoid.Health -= 3*dt end, -- onTick
function(target) print(target.Name .. " stopped burning") end,
false
)
- Named functions (cleaner, reusable):
local function burnApply(target) target.Humanoid.Health -= 5 end
local function burnTick(target, dt) target.Humanoid.Health -= 3*dt end
local function burnExpire(target) print(target.Name .. " stopped burning") end
local burn = StatusEffect.new("Burn", 5, burnApply, burnTick, burnExpire, false)
- Module-defined effect blueprints (data-driven):
-- Burn.lua
return {
Name = "Burn",
Duration = 5,
Stackable = false,
onApply = function(t) t.Humanoid.Health -= 5 end,
onTick = function(t, dt) t.Humanoid.Health -= 3*dt end,
onExpire = function(t) print(t.Name .. " stopped burning") end,
}
-- usage
local burnBlueprint = require(script.Burn)
local burnEffect = StatusEffect.new(
burnBlueprint.Name,
burnBlueprint.Duration,
burnBlueprint.onApply,
burnBlueprint.onTick,
burnBlueprint.onExpire,
burnBlueprint.Stackable
)
Key points:
• onApply runs once when the effect is first attached.
• onTick runs every frame / step while the effect is active.
• onExpire runs once when the effect is removed, regardless of why (duration ended, cleansed, replaced).