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.
I found the defines.events.on_gui_opened and defines.events.on_gui_closed. However I do not know enough about Lua to quickly read and set the positional data on open/close event hooks.
Remember last location for GUIs
Re: Remember last location for GUIs
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
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
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.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!
Re: Remember last location for GUIs
Ok thanks for the info.
I think this mod will be a bit out of my scope for now need to look into Lua bit more.
Thanks again for the help
I think this mod will be a bit out of my scope for now need to look into Lua bit more.
Are the base GUIs (player inventory, boxes, smelters, etc) able to be moved?
If they are not able to be moved then yes I would have to make a custom inventory which I do not have the skills to do (if even possible) at the moment lol
Thanks again for the help
- Deadlock989
- Smart Inserter
- Posts: 2528
- Joined: Fri Nov 06, 2015 7:41 pm
Re: Remember last location for GUIs
I use functions like this one in the on_gui_closed and on_gui_location_changed events:jdedmo wrote: ↑Sat Oct 08, 2022 2:02 pmI 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.
I found the defines.events.on_gui_opened and defines.events.on_gui_closed. However I do not know enough about Lua to quickly read and set the positional data on open/close event hooks.
Code: Select all
-- on_gui_location_changed
function CONTROL.on_gui_location_changed(event)
if event.element.name == CONTROL.transmat_gui then
CONTROL.set_player_global(event.player_index, "transmat_gui_location", event.element.location)
end
end
-- when creating or opening a gui
...
if CONTROL.get_player_global(player.index,"transmat_gui_location") then
frame.location = CONTROL.get_player_global(player.index,"transmat_gui_location")
else
frame.force_auto_center()
end
...
I have only tried doing this for custom GUIs made from scratch. However I think you could probably do something like cache GUIs per entity type and so forth, although I am not sure off the top of my head how much data about the GUI element gets passed in when it is a game engine GUI e.g. opening an assembling machine.
Re: Remember last location for GUIs
I don't think so. You could close them, however: player.opened is your friend here.
I think the "choose-elem-button" (see here) is the element you are looking for: "A button that lets the player pick from a certain kind of prototype, with optional filtering. Relevant event: on_gui_elem_changed".
You're welcome! If you want to mess around with GUIs, you should study this guide. The main part walks you through building a mod that constructs a simple GUI and makes it usable. This way, you learn not only how to create GUI elements, but also how a mod works, how you can react to events.Thanks again for the help
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!