Duplicating inventory slots

Things that already exist in the current mod API
Post Reply
Pi-C
Smart Inserter
Smart Inserter
Posts: 1652
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Duplicating inventory slots

Post by Pi-C »

With inventory[slot].transfer_stack(), it is already possible to move a stack from one inventory to another. However, I want to duplicate an inventory, so moving stacks isn't really helpful. Could we get something that would make an identical copy of the old contents (including items that have an inventory themselves), please? I really don't care what it's called, could be copy_stack() or clone_stack() or something in that spirit. :-)
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Pi-C
Smart Inserter
Smart Inserter
Posts: 1652
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Duplicating inventory slots

Post by Pi-C »

By the way, I know about surface.clone_entities{}, but I can't use that directly for my use case: I want to clone an entity's inventory as a backup to another entity of the same type because the original entity isn't guaranteed to exist between saves. If the original entity was removed, the clone would be removed along with it, its inventory's contents would be lost and I couldn't copy it to the inventory of my fall-back entity. (There's also the issue that an entire entity contains more data than just an inventory, so it should be cheaper to clone the latter.)

The way I could use surface.clone_entities to make a backup of an inventory would be like this:
  • Create a dummy entity (D) once.
  • If the inventory of the original entity (A) should be copied, use surface.clone_entities to get a copy (C) of it.
  • Use inventory.transfer_stack to move the inventory stacks from C to D.
  • Destroy C.
If inventory stacks could be cloned, it would be like this:
  • Create a dummy entity (D) once.
  • Use inventory.clone_stack to copy the inventory from A to D.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5150
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Duplicating inventory slots

Post by Klonan »

Pi-C wrote:
Mon Jul 27, 2020 12:26 pm
With inventory[slot].transfer_stack(), it is already possible to move a stack from one inventory to another. However, I want to duplicate an inventory, so moving stacks isn't really helpful. Could we get something that would make an identical copy of the old contents (including items that have an inventory themselves), please? I really don't care what it's called, could be copy_stack() or clone_stack() or something in that spirit. :-)
You can use set_stack:
https://lua-api.factorio.com/latest/Lua ... .set_stack

Or is there some reason this won't work for you?

Pi-C
Smart Inserter
Smart Inserter
Posts: 1652
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Duplicating inventory slots

Post by Pi-C »

Klonan wrote:
Mon Jul 27, 2020 1:59 pm
You can use set_stack:
https://lua-api.factorio.com/latest/Lua ... .set_stack
Indeed! No complicated workaround was needed, I just had to change one command to solve my problem.
Or is there some reason this won't work for you?
Just that I've been blind, or too focused on keywords like "copy" or "clone" during my search. I'm slightly embarrassed now. But thank you very much for pointing out what should have been obvious! :-)

So, this thread can be moved to wherever you see fit …
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Post Reply

Return to “Already exists”