How to access inventory of a disconnected/editor player?

Place to get help with not working mods / modding interface.
Data
Manual Inserter
Manual Inserter
Posts: 2
Joined: Sat Nov 23, 2024 10:36 am
Contact:

How to access inventory of a disconnected/editor player?

Post by Data »

Currently the way to access inventory of a connected player is (ignoring nil checks):

Code: Select all

-- we access inventory on the character, not the player since player has no inventory while being in remote view
if player.character then
    -- player is alive
    local inventory = player.character.get_main_inventory() -- get_inventory() can be used too
end
However, if a player either toggles editor or leaves the server while being in remote view (by closing the game externally), I haven't found a way to access their inventory:

Code: Select all

if player.connected then
    if player.physical_controller_type == defines.controllers.editor then
        -- player.character is nil, can't access inventory of the player character
        if player.controller_type == defines.controllers.remote then
            -- can't access any inventory at all, neither character nor editor
        else
            -- player.get_main_inventory() is the editor inventory
            -- player.get_inventory(defines.inventory.character_main) not even correct usage, it's not a character, returns editor inventory
        end
    end
else
    -- the player is disconnected
    if player.controller_type == defines.controllers.remote then
        -- can't access any inventory
    elseif player.controller_type == defines.controllers.character then
         local inventory = player.get_main_inventory() -- works just fine
    end
end
How to access inventory of a disconnected/in editor mode player character in any given situation?
Pi-C
Smart Inserter
Smart Inserter
Posts: 1743
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: How to access inventory of a disconnected/editor player?

Post by Pi-C »

Data wrote: Thu Dec 19, 2024 7:05 am How to access inventory of a disconnected/in editor mode player character in any given situation?
I'd say you can't, you have to work around it. The events on_pre_player_left_game/on_player_joined_game and on_pre_player_toggled_map_editor are your friends …
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!
Data
Manual Inserter
Manual Inserter
Posts: 2
Joined: Sat Nov 23, 2024 10:36 am
Contact:

Re: How to access inventory of a disconnected/editor player?

Post by Data »

Pi-C wrote: Thu Dec 19, 2024 8:28 am I'd say you can't, you have to work around it. The events on_pre_player_left_game/on_player_joined_game and on_pre_player_toggled_map_editor are your friends …
Well, that's just disappointing.
Thanks for the clever workaround, might try to apply it
luc
Fast Inserter
Fast Inserter
Posts: 231
Joined: Sun Jul 17, 2016 9:53 pm
Contact:

Re: How to access inventory of a disconnected/editor player?

Post by luc »

This thread was one of the top web search results for me. I was about to post that I found elsewhere that you can simply use `/open` nowadays, when I noticed this is the modding forum... and then thought, it would still help future searchers to post it here as well

/open PlayerName

Type this into the chat. The player's name has tab completion! You need to be admin, or open a save from a multiplayer game locally. Console commands reference page

Hope this helps future searchers! Sorry for the off-topic
Post Reply

Return to “Modding help”