Page 1 of 1

Adding new leftside panels from the mods folder?

Posted: Thu Apr 26, 2018 6:50 pm
by harpy eagle
Is it possible to add new GUI elements from the mod folder?

Recently I tried to create a new leftside pane to show a report on the current empire's population demographics. I was able to get the game to load and create the plane using this code:

Code: Select all

reload_script [[GUI\~GalaxyMap\GalaxyMap.lua]]
PlanetPopPane(gui.GalaxyMap)
I was able to verify that create_leftside_pane() was being called and the pane was created and accessible from inside @TopBarContents.lua, but when I actually tried to open the pane the game would immediately crash to desktop.

If I put my code into Lua state the pane works just fine.

Re: Adding new leftside panels from the mods folder?

Posted: Thu Apr 26, 2018 10:27 pm
by sven
harpy eagle wrote: I was able to verify that create_leftside_pane() was being called and the pane was created and accessible from inside @TopBarContents.lua, but when I actually tried to open the pane the game would immediately crash to desktop.

If I put my code into Lua state the pane works just fine.
Hmm.. I'm not quite sure what's going wrong here. Would you send me a copy of your crashy attempted mod? (In theory, I think something along the lines of what you're describing ought to work.)

Re: Adding new leftside panels from the mods folder?

Posted: Fri Apr 27, 2018 12:25 am
by harpy eagle
sven wrote:Hmm.. I'm not quite sure what's going wrong here. Would you send me a copy of your crashy attempted mod? (In theory, I think something along the lines of what you're describing ought to work.)
Here it is. The crash happens when I click on the food icon in the top bar to open the panel.

Re: Adding new leftside panels from the mods folder?

Posted: Fri Apr 27, 2018 4:38 pm
by sven
harpy eagle wrote:
sven wrote:Hmm.. I'm not quite sure what's going wrong here. Would you send me a copy of your crashy attempted mod? (In theory, I think something along the lines of what you're describing ought to work.)
Here it is. The crash happens when I click on the food icon in the top bar to open the panel.
Ok, so first some background (for the benefit of anyone other than harpy eagle who may be reading this post). UI_File is an interesting hack (see file_env.lua). The idea is that if you wrap a file's definitions in UI_File | function(_ENV), then what you're doing is creating a function with the same name as your file. Calling that function will add a new set of function / table definitions to a given environment. So, for example, when GalaxyMap.lua is run, it creates the new environment gui.GalaxyMap, and then populates it with all sorts of inter-dependent UI functions by calling various UI_File functions.

harpy eagle clearly understands all this, and has thus attempted to add a new set of tables and function definitions to gui.GalaxyMap by calling

Code: Select all

reload_script [[GUI\~GalaxyMap\GalaxyMap.lua]]
PlanetPopPane(gui.GalaxyMap)
However, that doesn't work. Why, exactly, it doesn't work is a bit mystifying. As best as I can tell, the core of the issue is that the resize_callbacks defined in PlanetPopPane.lua are never getting called. The exact details of *why* it's not getting called continue to elude me (there's a lot of fiddly stuff that goes on involving resize callbacks), but, at a high level, the answer is probably "because I never expected mods to patch into UI ENVs like this".

The pattern used in most places in the code is to call

Code: Select all

PlanetPopPane(_ENV)
in some function that's evaluating during the the execution of GalaxyMap.lua. If I switch harpy eagle's mod to use a version of this pattern, say by inserting PlanetPopPane(_ENV) into the top of the UI_File function call in @TopBarContents.lua; the mod appears to work fine. So... I guess that's the fix.

Re: Adding new leftside panels from the mods folder?

Posted: Fri Apr 27, 2018 5:02 pm
by sven
sven wrote:

Code: Select all

reload_script [[GUI\~GalaxyMap\GalaxyMap.lua]]
PlanetPopPane(gui.GalaxyMap)
At a high level, one problem with this code is that if GalaxyMap.lua is ever reloaded again a fresh version of PlanetPopPane won't get re-added to gui.GalaxyMap. That's a problem, because the resize_callbacks list is re-created every time we do a reload of GalaxyMap.lua -- and that means you can easily end up in a situation where the root tables in PlanetPopPane are defined for gui.GalaxyMap -- but all the window-size specific logic is left undefined. I *suspect* this may be what's going wrong in the broken version of the mod, but, I haven't quite been able to figure out where the hypothesized reload of GalaxyMap.lua is being triggered from.

Re: Adding new leftside panels from the mods folder?

Posted: Fri Apr 27, 2018 6:24 pm
by harpy eagle
sven wrote: If I switch harpy eagle's mod to use a version of this pattern, say by inserting PlanetPopPane(_ENV) into the top of the UI_File function call in @TopBarContents.lua; the mod appears to work fine. So... I guess that's the fix.
Aha, so you're piggy-backing the call to PlanetPopPane() onto one of the pre-existing calls inside GalaxyMap.lua.
sven wrote: At a high level, one problem with this code is that if GalaxyMap.lua is ever reloaded again a fresh version of PlanetPopPane won't get re-added to gui.GalaxyMap. That's a problem, because the resize_callbacks list is re-created every time we do a reload of GalaxyMap.lua -- and that means you can easily end up in a situation where the root tables in PlanetPopPane are defined for gui.GalaxyMap -- but all the window-size specific logic is left undefined. I *suspect* this may be what's going wrong in the broken version of the mod, but, I haven't quite been able to figure out where the hypothesized reload of GalaxyMap.lua is being triggered from.
Right, that makes sense. So what I had would fail if GalaxyMap.lua is reloaded from anywhere else (even though we don't quite know where that anywhere else is)...

I wonder if it would work to put a GalaxyMap.lua file containing only a call to PlanetPopPane(gui.GalaxyMap). Any time GalaxyMap gets reloaded it should then execute the mod version after, right?

Re: Adding new leftside panels from the mods folder?

Posted: Fri Apr 27, 2018 6:58 pm
by sven
harpy eagle wrote: Aha, so you're piggy-backing the call to PlanetPopPane() onto one of the pre-existing calls inside GalaxyMap.lua.
Yeah -- the whole call graph for GalaxyMap.lua is spread over dozens of different files -- hacking a hook into any one them should be sufficient. (And you'll probably need to mod at least one file in the call graph anyway, because you'll need an entry point for your new UI element.)
harpy eagle wrote:I wonder if it would work to put a GalaxyMap.lua file containing only a call to PlanetPopPane(gui.GalaxyMap). Any time GalaxyMap gets reloaded it should then execute the mod version after, right?
Maybe... but even if that did work, whatever else the GalaxyMap.lua reload was supposed to achieve probably won't be achieved, because you've just replaced most of the effect of executing GalaxyMap.lua with a null-op. So, even this did work, I believe it would also totally break the live-coding features for the rest of the GalaxyMap UI.