Good catch! The target is not stored inside the StatusEffect instance in your current design, so it’s not available inside the tick method.
How to fix this?
You have two main options:
Option 1: Pass target as an argument to tick
Change your tick method to accept target:
function StatusEffect:tick(target, deltaTime)
self.Elapsed = self.Elapsed + deltaTime
self.onTick(target, deltaTime)
if self.Elapsed >= self.Duration then
self:expire(target)
end
end
Then, when you call tick, pass the target explicitly:
effect:tick(target, deltaTime)
Option 2: Store target inside the StatusEffect instance when applying
Modify your apply method to save the target:
function StatusEffect:apply(target)
self.Target = target
self.Elapsed = 0
self.onApply(target)
end
Then your tick method can use self.Target:
function StatusEffect:tick(deltaTime)
self.Elapsed = self.Elapsed + deltaTime
self.onTick(self.Target, deltaTime)
if self.Elapsed >= self.Duration then
self:expire(self.Target)
end
end
Which one to choose?
- Option 1 is more functional and stateless — you always pass the target explicitly.
- Option 2 is more object-oriented — the effect “knows” its target.
If your effect is always tied to exactly one target, Option 2 is cleaner and easier to use.
Example with Option 2 (recommended):
function StatusEffect:apply(target)
self.Target = target
self.Elapsed = 0
self.onApply(target)
end
function StatusEffect:tick(deltaTime)
self.Elapsed = self.Elapsed + deltaTime
self.onTick(self.Target, deltaTime)
if self.Elapsed >= self.Duration then
self:expire(self.Target)
end
end
function StatusEffect:expire(target)
self.onExpire(target, true)
-- cleanup code here
end
And in your ticker:
function EffectTicker.update(deltaTime)
for target, effects in pairs(activeEffects) do
for effect, _ in pairs(effects) do
effect:tick(deltaTime) -- no need to pass target now
if effect.Elapsed >= effect.Duration then
effect:expire(target)
effects[effect] = nil
end
end
if next(effects) == nil then
activeEffects[target] = nil
end
end
end
If you want me to, I can help you refactor your full code with this approach!