Page 1 of 1

[0.16.x] Setting LuaControl::opened with LuaGuiElement

Posted: Sun Dec 31, 2017 4:20 pm
by Therax
From the 0.16.0 changelog:
Added support for setting a LuaGuiElement as the opened GUI for a player causing it to close with the normal close-GUI methods.
How exactly is this intended to work?

AFAICT the only way to get a new LuaGuiElement is to add it to an existing root container in a LuaGui. Setting opened to this LuaGuiElement works, but when pressing E or Esc to close the GUI, opened is reset to nil, but the LuaGuiElement is still displayed onscreen because it's still part of the LuaGui's root container. I haven't found a way to detach it from a LuaGui, since children is read-only and clear() and destroy() remove the LuaGuiElement even if it's the current target of opened.

My test code:

Code: Select all

local function show_gui(player)
    local frame = player.gui.center.add{
        type = "frame",
        name = "my-config",
        direction = "vertical",
    }
    frame.add{
        type = "label",
        name = "title",
        caption = "My Config",
    }
    player.opened = frame
end

Re: [0.16.x] Setting LuaControl::opened with LuaGuiElement

Posted: Sun Dec 31, 2017 5:08 pm
by Bilka
You have to subscribe to the on_gui_closed event to then close your gui yourself. Code example for a mod I'm maintaining: https://github.com/Bilka2/Quill/commit/ ... 499bd7f22d

Re: [0.16.x] Setting LuaControl::opened with LuaGuiElement

Posted: Sun Dec 31, 2017 10:05 pm
by Therax
Aha, interesting, if somewhat counterintuitive. It never occurred to me that the on_gui_closed event would be fired when an action hadn't been already completed by the engine to, you know, close a GUI. I guess setting opened to nil counts? In any case, thanks for the help!