Page 1 of 1

How to modify entity.tags?

Posted: Sat Oct 25, 2025 9:40 pm
by FeelusM
Documantation says:
Note that the API returns tags as a simple table, meaning any modifications to it will not propagate back to the game. Thus, to modify a set of tags, the whole table needs to be written back to the respective property.
I try:

Code: Select all

script.on_event(defines.events.on_built_entity, function(event)
	if event.entity and event.entity.name == "signal-logger" then
		local tags = event.entity.tags or {}
		tags.logger_name = tags.logger_name or ""
		tags.signals = tags.signals or {}
		event.entity.tags = tags
		game.print(event.entity.tags and "entity.tags present" or "entity.tags absent")
	end
end)
It prints "entity.tags absent"

What am I doing wrong?

Re: How to modify tags?

Posted: Sat Oct 25, 2025 10:26 pm
by Pi-C
See the description of LuaEntity::tags:
The tags associated with this entity ghost. nil if this is not an entity ghost or when the ghost has no tags.
So I guess you can't add tags to your entity because you're dealing with real entities instead of entity ghosts …

Re: How to modify entity.tags?

Posted: Sat Oct 25, 2025 11:21 pm
by FeelusM
Can I store data associated with real entity somewhere in entity object? Is there canonical place for such purposes?

Or I should save it in `storage.some_data[entity.unit_number]` and write boilerplate code for transferring it between `storage` and `ghost_entity.tags` for numerous events?

Re: How to modify entity.tags?

Posted: Sun Oct 26, 2025 8:33 am
by Pi-C
FeelusM wrote: Sat Oct 25, 2025 11:21 pm Or I should save it in `storage.some_data[entity.unit_number]` and write boilerplate code for transferring it between `storage` and `ghost_entity.tags` for numerous events?
This is the way to go. :-)

Re: How to modify entity.tags?

Posted: Sun Oct 26, 2025 1:29 pm
by FeelusM
Where should I store data between deconstruction and cancelling deconstruction (Ctrl+Z) and
How should I set ghost_entity.tags? (On which event? and How to understand which real former entity corresponds to the restored ghost? (real former entity and restored ghost have different `unit_number`s))

Re: How to modify entity.tags?

Posted: Mon Oct 27, 2025 1:25 am
by Pi-C
FeelusM wrote: Sun Oct 26, 2025 1:29 pm Where should I store data between deconstruction and cancelling deconstruction (Ctrl+Z)
I have no practical experience with this, but these events look useful:
  • on_cancelled_deconstruction
  • on_marked_for_deconstruction
  • on_player_deconstructed_area
  • on_pre_ghost_deconstructed
How should I set ghost_entity.tags? (On which event?…)
on_post_entity_died will give you the ghost entity. You may want to combine this with on_entity_died where you can still access the original entity, but it may be sufficient to add a filter to on_post_entity_died.
How to understand which real former entity corresponds to the restored ghost? (real former entity and restored ghost have different `unit_number`s)
In addition to unit_number, ghosts have ghost_unit_number referring to the actual entity. Actually, you can read several properties of the original entity from ghosts:
  • ghost_has_flag
  • ghost_name
  • ghost_localised_name
  • ghost_localised_description
  • ghost_type
  • ghost_prototype
  • ghost_unit_number