entity.die(anything, nil)
Posted: Mon Jun 14, 2021 4:28 pm
There's been a few times I want to call entity.die() in a damaged event, and pass in the right parameters.
event.entity.die(event.force) will always work, even if force is nil. force is optional in the event and for entity.die(), though in the damage event I don't think it can ever be nil (e.g. entity.damage() requires a force, normal damage comes from *something*)
event.entity.die(event.force, event.cause) won't always work, because if the second argument is present, it must be non-nil. The argument is optional, but can only be not present. It's not a big issue, but it makes for some extra logic, which is a bit ugly:
edit: a time event.force can be nil is when a scenario/mod merges the force in their on_entity_damaged. The event still propagates to more listeners, but the force is nil. Thanks JanSharp.
event.entity.die(event.force) will always work, even if force is nil. force is optional in the event and for entity.die(), though in the damage event I don't think it can ever be nil (e.g. entity.damage() requires a force, normal damage comes from *something*)
event.entity.die(event.force, event.cause) won't always work, because if the second argument is present, it must be non-nil. The argument is optional, but can only be not present. It's not a big issue, but it makes for some extra logic, which is a bit ugly:
Code: Select all
local function on_entity_damaged(event)
local entity = event.entity
local force = event.force
local cause = event.cause
--special logic
if should_die then
--error without cause
--entity.die(force, cause)
if cause then
entity.die(force, cause)
else
entity.die(force)
end
end
end