[0.12.33; mod API] Disconnected Inventories lose item state

Bugs that are actually features.
AenAllAin
Long Handed Inserter
Long Handed Inserter
Posts: 61
Joined: Sat Apr 02, 2016 3:10 am
Contact:

[0.12.33; mod API] Disconnected Inventories lose item state

Post by AenAllAin »

I don't know if this would be considered a bug or just flagrant abuse of the system. To sum up...
A. I create a dummy player entity:

Code: Select all

    local s_bpr = game.get_surface("blueprinter") -- after creating the other surface of course
    player.force.chart(s_bpr, {{-40,-40}, {40,40}}) -- chart the new surface to try and get the next line to work
    local l_pos = s_bpr.find_non_colliding_position("player", {0,0}, 40, 1) or {player.index,player.index} -- ...nevermind, this POS has never worked; just use a default pos
    local p_bpr = s_bpr.create_entity({name="player", position=l_pos, force=player.force})
    global.blueprinter.inventory[player.index] = p_bpr -- using player.index because player.name is not consistent between the events
B. I insert a completed/setup blueprint into the dummy players inventory:

Code: Select all

        local l_inventory = global.blueprinter.inventory[ l_player.index ].get_inventory( defines.inventory.player_main )
        local s_stack = l_player.cursor_stack
        if s_stack and s_stack.valid_for_read then
            if s_stack.type == "blueprint" and s_stack.is_blueprint_setup() then
                -- Insert
                l_inventory.insert(s_stack)
...
                s_stack.clear()
...
C. I take the blueprint back out of the inventory:

Code: Select all

local l_inventory = global.blueprinter.inventory[ l_player.index ].get_inventory( defines.inventory.player_main ) -- same code from above
...
            -- Get target-element table index
            local l_si = nil
            for xi, xv in ipairs(e_registry.children_names) do
                if e_schematic.name == xv then -- ...is there a better way to get the GUI elements child index, other than matching?
                    l_si = xi
                end
            end

            -- GUI selection toggle; i.e. if already selected
            if l_si == global.blueprinter.selected then
                global.blueprinter.selected = nil
                if l_inventory[l_si] and l_inventory[l_si].valid_for_read then
                    -- set to the players cursor_stack
                    s_stack.set_stack(l_inventory[l_si]) -- ...am I misreading it, or does this actually work opposite from the way the documentation describes?
                end
D. TADA! Your blueprint is now blank! ...you are so welcome.

Ok, I know everyone is probably going to focus on the part about creating zombie players on other worlds and storing stuff in their inventories ...but, I'm ok with that; I just need to know why my blueprints are getting washed. Every other item I've tried seems to go in and come out just fine; it's just the blueprints getting corpsified.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14345
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [0.12.33; mod API] Disconnected Inventories lose item state

Post by Rseding91 »

Code: Select all

l_inventory.insert(s_stack)
That doesn't make an exact copy of the stack, that just inserts a new empty blueprint into the inventory as if you did "insert({name="belueprint", count=1})"

What you want is: http://lua-api.factorio.com/0.12.33/Lua ... #set_stack which will clone the source into the destination.
If you want to get ahold of me I'm almost always on Discord.
AenAllAin
Long Handed Inserter
Long Handed Inserter
Posts: 61
Joined: Sat Apr 02, 2016 3:10 am
Contact:

Re: [0.12.33; mod API] Disconnected Inventories lose item state

Post by AenAllAin »

Rseding91 wrote:

Code: Select all

l_inventory.insert(s_stack)
That doesn't make an exact copy of the stack, that just inserts a new empty blueprint into the inventory as if you did "insert({name="belueprint", count=1})"

What you want is: http://lua-api.factorio.com/0.12.33/Lua ... #set_stack which will clone the source into the destination.
Ah! Thank you sir. But, how does one get an insert-able stack from the inventory to call upon? iterate through the inventory with the '[]' operator calling 'can_insert' until you find one? ...not that, that is a bad idea; I just didn't think that was how the Player Inventory seemed to work (containers, yes; but not inventories).
AenAllAin
Long Handed Inserter
Long Handed Inserter
Posts: 61
Joined: Sat Apr 02, 2016 3:10 am
Contact:

Re: [0.12.33; mod API] Disconnected Inventories lose item state

Post by AenAllAin »

Alright, I got it all working. Thanks for the help. Hopefully, my approach is correct. Here is the working version of the code just for future reference:

Code: Select all

        local l_inventory = global.blueprinter.inventory[ l_player.index ].get_inventory( defines.inventory.player_main )
        local s_stack = l_player.cursor_stack
        if s_stack and s_stack.valid_for_read then
            if s_stack.type == "blueprint" and s_stack.is_blueprint_setup() then

                -- Insert Blueprint
                for xi =1, #l_inventory do
                    if l_inventory[xi].can_set_stack(s_stack) and not l_inventory[xi].valid_for_read then
                        l_inventory[xi].set_stack(s_stack)
                        s_stack.clear()
                        break
                    end
                end

            end
        else
...GUI stuff...
Post Reply

Return to “Not a bug”