How to replace entities on tiles in a migration

Place to get help with not working mods / modding interface.
Pi-C
Smart Inserter
Smart Inserter
Posts: 1656
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: How to replace entities on tiles in a migration

Post by Pi-C »

AlmightyCrumpet wrote:
Fri Jun 09, 2023 6:51 pm
YES!
Got it working!
Thank you so much!
Great that it works now -- it was just an untested idea. :-)
This is how it ended up after your last reply
Some small improvements:

Code: Select all


local function swap_entities(surface, tiles)
  -- widget is implicitly declared in the header of the for-loop, but you don't want Widgets to be global!
	--~ local position, newEntity, widget, force
	local position, newEntity, Widgets, force
	
	for t, tile in pairs(tiles) do
		position = tile.position
	  
		-- check the same x/y coordinates for the existing entity on the tile
		Widgets = surface.find_entities_filtered{name = "powered-floor-widget", position = position, radius = 1}
		-- if it is only a 'power-floor-widget' then remove it and add a 'circuit-floor-widget' in it's place

    -- Not necessary: If Widgets is empty, the for loop will do nothing!
		--~ if next(Widgets) then
			for w, widget in pairs(Widgets) do
        --Not necessary: If the table isn't empty, there is guaranteed to be a widget for each w!  
				--~ if widget then
					-- Store force and delete widget
					force = widget.force
					widget.destroy{raise_destroy = true}

					-- Add new widget
					newEntity = surface.create_entity{
						name = 'circuit-floor-widget',
						position = {position.x, position.y},
						force = force,
						raise_built = true,
					}

					IncludeControlWiresToNeighbors(newEntity, surface)
					if newEntity then
						newEntity.destructible = false
					end
				--~ end
			end
		--~ end
	end
end
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”