entity not destroyed by entity.destroy

Place to get help with not working mods / modding interface.
rooster407
Manual Inserter
Manual Inserter
Posts: 4
Joined: Sun Jul 27, 2025 7:42 pm
Contact:

entity not destroyed by entity.destroy

Post by rooster407 »

I have an entity which is placed by lua script when placing a different entity, both of which are recorded in a table by unit number. When the first is mined by the player destroy is called on the second, but the entity isn't removed.

Called from the on_player_mined_entity event,

Code: Select all

function unregister_hab (event)
	for i in pairs (storage.hab_index) do
		if storage.hab_index [i] == event.entity.unit_number then
		
			table.remove(storage.hab_index,i)--removing the primary entity from the index table

			local energy_control = game.get_entity_by_unit_number(storage.hab_energy_control[i]) -- getting the second by unit number
			if energy_control.valid then
				energy_control.destroy()
			end
			
			table.remove(storage.hab_energy_control,i)--removing the second entity from the index table
		end	
	end
end
As far as I can tell energy_control is removed ,but the entity is not removed
08-03-2025, 17-45-06.png
08-03-2025, 17-45-06.png (699.08 KiB) Viewed 78 times
Does getting an entity by unit number not give you the "real" entity where destroy is concerned?
rooster407
Manual Inserter
Manual Inserter
Posts: 4
Joined: Sun Jul 27, 2025 7:42 pm
Contact:

Re: entity not destroyed by entity.destroy

Post by rooster407 »

Found the problem, it was how I was creating the entity.
Original code,

Code: Select all

function register_hab (event)
		table.insert(storage.hab_index,event.entity.unit_number)
		
		game.surfaces[1].create_entity{name="hab_electrical", position=event.entity.position}
		
		local entity_energy_cotrol=game.surfaces[1].find_entity("habitation_dome", event.entity.position)
		
		table.insert(storage.hab_energy_control,entity_energy_cotrol.unit_number)	
end
New code,

Code: Select all

function register_hab (event)
		table.insert(storage.hab_index,event.entity.unit_number)
		
		local entity_energy_cotrol =game.surfaces[1].create_entity{name="hab_electrical", position=event.entity.position}
		
		table.insert(storage.hab_energy_control,entity_energy_cotrol.unit_number)	
end
I didn't know create_entity returned the entity it created, and was using a janky method to get the unit number.
Post Reply

Return to “Modding help”