on_gui_opened Event

Place to get help with not working mods / modding interface.
pistols39
Burner Inserter
Burner Inserter
Posts: 16
Joined: Tue Feb 26, 2019 7:39 pm
Contact:

on_gui_opened Event

Post by pistols39 »

I am listening to the on_gui_opened event in my control.lua and have some behaviour I do not understand. Some sample code below:

Code: Select all

script.on_event(defines.events.on_gui_opened, function(event)
    game.print("called on_gui_opened")
    game.print(event.item)
    game.print(event.gui_type)
end)
If I right click some power armor in my main inventory then the event.item is a 'LuaItemStack' and event.gui.type is '5' (defines.gui_type.item). If I right click some power armor in a chest then event.item is 'nil' and event.gui.type is '5' (defines.gui_type.item). Both times the equipment grid is displayed as normal.

What I don't understand is where the armor is placed seems to make a difference to the event.item value. Isn't a chest just a LuaInventory (and the power armor a LuaItemStack), in the same way the player main inventory is?

This has come about as I would like to know which armor an equipment grid has been opened for.

Thanks in advance!
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3749
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: on_gui_opened Event

Post by DaveMcW »

Code: Select all

local item = game.players[event.player_index].opened
pistols39
Burner Inserter
Burner Inserter
Posts: 16
Joined: Tue Feb 26, 2019 7:39 pm
Contact:

Re: on_gui_opened Event

Post by pistols39 »

That will return the LuaEquipmentGrid. Any way to find out which armor item the grid it is owned by?
pistols39
Burner Inserter
Burner Inserter
Posts: 16
Joined: Tue Feb 26, 2019 7:39 pm
Contact:

Re: on_gui_opened Event

Post by pistols39 »

Did some more testing on this. Only armor opened from the players main inventory or the armor currently worn will set the item in the on_gui_opened event. Any armor in a chest, car, tank, spidertron or inventory created on the game object (using create_inventory()) will not set the item in the on_gui_opened_event.

Furthermore if you set player.opened to an armor itemstack that is in the main inventory via a script, then the item will be set in the on_gui_opened_event. Trying this with an itemstack that exists in a custom inventory for example, the item will not be set in the on_gui_opened_event (i.e. behaviour seems to be the same as the above paragraph).

I am not sure if this is intended behaviour, or a bug.

I have figured out a workaround for this though. I can detect which entity is opened i.e. the chest, I can then detect when an equipment grid is opened. I can loop through the item stacks in the entity and compare their equipment grids with the one opened and it gives me the correct armor itemstack.
Post Reply

Return to “Modding help”