How to modify entity.tags?

Place to get help with not working mods / modding interface.
FeelusM
Burner Inserter
Burner Inserter
Posts: 8
Joined: Thu Oct 09, 2025 10:54 am
Contact:

How to modify entity.tags?

Post 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?
Last edited by FeelusM on Sat Oct 25, 2025 11:11 pm, edited 1 time in total.
Pi-C
Smart Inserter
Smart Inserter
Posts: 1788
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: How to modify tags?

Post 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 …
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!
FeelusM
Burner Inserter
Burner Inserter
Posts: 8
Joined: Thu Oct 09, 2025 10:54 am
Contact:

Re: How to modify entity.tags?

Post 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?
Pi-C
Smart Inserter
Smart Inserter
Posts: 1788
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: How to modify entity.tags?

Post 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. :-)
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!
FeelusM
Burner Inserter
Burner Inserter
Posts: 8
Joined: Thu Oct 09, 2025 10:54 am
Contact:

Re: How to modify entity.tags?

Post 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))
Pi-C
Smart Inserter
Smart Inserter
Posts: 1788
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: How to modify entity.tags?

Post 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
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!
Post Reply

Return to “Modding help”