jdedmo wrote: ↑Sat Oct 08, 2022 2:02 pm
I am just looking to see if anyone has seen mods that will simply remember where GUI Windows were last and keep them there when they are reopened.
One way to achieve that would be to construct the main frame just once:
Code: Select all
local function create_gui(player)
local gui = player.gui.screen
if not gui["your-gui-name"] then
gui = gui.add({
type = "frame",
name = "your-gui-name",
direction = "vertical",
visible = true,
autocenter = true
})
AD.created_msg(gui)
end
end
This is the container for your GUI, you can place other frames, flows, tables, buttons, textfields etc. inside this container. On creation, it will be placed prominently in the center of the screen. When you disable the GUI, you don't destroy it, but just hide it:
Code: Select all
local function show_or_hide(gui)
if gui and gui.valid then
gui.visible = not gui.visible
gui.autocenter = false
-- Optional:
if not gui.visible then
-- Remove all child elements of the main frame
gui.clear()
else
-- Rebuild child elements
populate(gui)
end
end
This will always keep your GUI's main frame alive and just toggle between visible and invisible state. While the GUI was centered when it was created, autocentering will be turned off (and remain so) the first time the player hides the GUI. Thus, it will always remember the last position it has been moved to by the player.
Whether or not you want to add the optional part depends on the nature of your GUI. If the child elements never change, there's no need to destroy/rebuild the GUI each time it's toggled. However, if you expect the contents to change a lot (e.g. labels or buttons linked to specific entities), gui.clear() and a complete rebuild of the inner GUI may be safer than keeping track of and updating the individual elements.