entity.die(anything, nil)

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
Post Reply
Honktown
Smart Inserter
Smart Inserter
Posts: 1025
Joined: Thu Oct 03, 2019 7:10 am
Contact:

entity.die(anything, nil)

Post by Honktown »

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:

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
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.
I have mods! I guess!
Link

andrei
Burner Inserter
Burner Inserter
Posts: 14
Joined: Tue Aug 27, 2019 8:32 pm
Contact:

Re: entity.die(anything, nil)

Post by andrei »

I had a similar issue with game.print. It can accept one argument (message) or two argument (message, color), but the second argument cannot be nil. So I also ended up doing

Code: Select all

    if color then
      game.print(message, color)
    else
      game.print(message)
    end
Would be nice to fix this one as well.

Post Reply

Return to “Modding interface requests”