Replace entity in running game with entity from another mod

Place to get help with not working mods / modding interface.
Lupin
Burner Inserter
Burner Inserter
Posts: 8
Joined: Sat Apr 09, 2016 12:41 pm
Contact:

Replace entity in running game with entity from another mod

Post by Lupin »

I have two mods that provide very similar additional things (in my case 5dim and FactorioBasics). I have built solar panels, accumulators, robots and a few other things from both to compare them, but now I have decided, which of those I want to use.
Is there a way I can convert the entity of one of the mods into another one from the other mod? E. g. make all solar panels of type A to be a solar panel of type B or change all logistic robots of mod A into logistic robots of mod B? The entities I want to replace are the same size in both mods (and identical to the Factorio stock items).
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Replace entity in running game with entity from another mod

Post by prg »

Code: Select all

local original = "stone-furnace"
local replacement = "steel-furnace"
local s = game.local_player.surface
for chunk in s.get_chunks() do
    local entities = s.find_entities_filtered{name=original, area={{chunk.x*32, chunk.y*32}, {(chunk.x+1)*32, (chunk.y+1)*32}}}
    for _, entity in pairs(entities) do
        s.create_entity{name=replacement, position=entity.position}
        entity.destroy()
    end
end
This does not preserve recipes in assembling machines etc., but for things like solar panels it should be fine.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
Lupin
Burner Inserter
Burner Inserter
Posts: 8
Joined: Sat Apr 09, 2016 12:41 pm
Contact:

Re: Replace entity in running game with entity from another mod

Post by Lupin »

Thanks! That worked for solar panels and accumulators. But when I do the same for robots, my personal laser defense starts attacking them! They've become enemies. And what would I do for the robots that currently aren't out but in the inventory of the robo ports?
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Replace entity in running game with entity from another mod

Post by prg »

Ah, right, created entities are on the enemy force by default. The solar panels will now be, too. Add force=game.local_player.force to the create_entity call.

As for the robots, maybe something like

Code: Select all

local original = "construction-robot"
local replacement = "logistic-robot"
local s = game.local_player.surface
for chunk in s.get_chunks() do
    local entities = s.find_entities_filtered{name="roboport", area={{chunk.x*32, chunk.y*32}, {(chunk.x+1)*32, (chunk.y+1)*32}}}
    for _, entity in pairs(entities) do
        local inv = entity.get_inventory(1)
        local count = inv.get_contents()[original]
        if count then
            inv.remove{name=original, count=count}
            inv.insert{name=replacement, count=count}
        end
    end
end
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
Lupin
Burner Inserter
Burner Inserter
Posts: 8
Joined: Sat Apr 09, 2016 12:41 pm
Contact:

Re: Replace entity in running game with entity from another mod

Post by Lupin »

Tripped up a little at first, because my roboports aren't stock either, so the filter didn't find their inventory of course, but in the end it's fine. Since I apparently don't value my games and overwrote my original save after I replaced the panels and accumulators (but before I turned my robots into hostiles), I thought I'd re-replace entities with themselves and the new "force" option. But it did not seem necessary for them. They still produced and stored power for me and not the biters ;) . (and I can deconstruct them as normal).

Many thanks for the help! I never touched LUA before and I defnitely wouldn't have found the factorio API-calls that fast.
Post Reply

Return to “Modding help”