Skip to main content

Trait examples and pitfalls

These examples define behavior. Bind each definition to an affect as shown in binding.

Reduce incoming damage​

forge.sys.game.traits["define"]{
id = "example_reduction",
hooks = {
on_damage_taken_final = function(ch, attacker, dam, dam_type, ctx)
ctx.damage = ctx.damage * 0.7
end,
},
}

This transform includes damage-over-time and reflected damage. Filter dam_type when those should be excluded.

Count damage and reset on a tick​

forge.sys.game.traits["define"]{
id = "example_counter",
counters = { { name = "dealt", accumulate = "DAMAGE_DEALT_POST" } },
hooks = {
on_tick = function(ch, ctx)
if ctx.counter("dealt") > 1000 then
ctx.reset_counters()
end
end,
},
}

The post amount is the amount handed to the victim before victim-side transforms and later critical and pierce adjustments, not necessarily health lost.

Pitfalls​

SymptomCause and fix
Equipping a point does not activate a traitEquipment uses ApplyPoint, not AddAffect. Add the bound affect to open the gate.
A bump does nothing or lasts too longUse it only in the two damage pre hooks. Other hooks may be outside the guard or run inside one that outlives the hook.
A bump reaches unintended attacksFilter dam_type; the same pre hooks cover melee, ranged, and skill damage.
A formula uses its fallbackPass live variables such as dam in vars; check that the named formula and constants exist.
A restored affect has no constantsConstants are session-scoped. Use ctx.gate_value() for durable values or a no-save affect.
A hook never firesUnknown hook names are ignored. Check the spelling and the definition's logged mask.
A counter stays at zeroOnly the triggers listed under automatic counters feed counters. Other recognized trigger names do not.
A Lua bind returns 0Define the ID before binding and check the return value.
A scheduled callback outlives its traitUse ctx.schedule; it is cancelled on deactivation and destruction. Do not retain a ctx in an external closure.
A disabled trait starts running againOpening its gate enables it. Disable it again if required.
A fractional cooldown lasts longer than expectedCooldowns round up to whole seconds.
A teardown callback cannot use ctx functionsAt character destruction ch remains valid, but ctx functions are inert.

See definition and hooks and Traits foundations.