PDA

View Full Version : “Breaking up” lua files?



Varsuuk
August 8th, 2019, 21:34
I have, for example. A char_main.lua associated with the record_main.xml or whatever. It has handlers for everything all in one.

I would prefer some times to separate conceptually maybe all the saving throw handlers from maybe where it calculates/updates encumbrance or whatever just randomly picking examples.

What is the recommended practice for any who have done something like that? Do you have a char_main.lua which calls other base.xml names scripts like ManagerSaves.lua etc like using proxy concept? Ex in Charmain:onLevelUpdateSaves(arg) it calls inside that method ManagerSaves.onLevelUpdateSaves(arg) ?

I don’t care about actually separating via encapsulation as much as seeing “less” at a time and more focused (helps me think) so if I could have, I’d be happy to just have in the top of the managerchar.lua file some “include ManagerSaves.lua” constrict? But I seem to recall no includes. But I’ve been away from my pet project for 3.5 months and I have actual memory problems in RL :(

Trenloe
August 8th, 2019, 21:35
https://www.fantasygrounds.com/wiki/index.php/Developer_Guide_-_Rulesets_-_Scripting#Global_Script_Packages

Varsuuk
August 9th, 2019, 00:47
Yeah, this was my memory: Window/Control/Global - but thanks cos the link is good to review.

There is at the top of the xml:


<root>
<windowclass name="charsheet_main">
<script file="campaign/scripts/char_main.lua" />


So that is the Windows-connected (one instance per window) of the (char_main.lua) lua script.

Then at the controls:


<number_savingthrow name="savingthrowbase"
source="defenses.saves.default.base">
<anchored>
<top parent="label_savingthrow" anchor="bottom" offset="10" />
<right parent="frame_classlevel" anchor="right" offset="-12" />
</anchored>

is derived from:
<template name="number_savingthrow">
<number>
<frame name="frame_charsheet_savingthrow" offset="7,5,7,5" />
<anchored width="32" height="20" >
<top parent="**MISSING ANCHOR**" anchor="bottom" offset="5" />
<right parent="savingthrowbase" />
</anchored>
<nokeyedit />
<rollable />
<script file="campaign/scripts/char_saves_sw.lua" />
</number>
</template>


So EACH Saving throw control element has its own copy of the lua script (char_saves_sw.lua)


So, the code that I want to be called, I can put in char_main.lua but I wanted to not clutter it up too much with too many disparate "areas of responsibility", I would like to delegate the work to files of related code. So, as I said in my original question - I'd be happy to have include statements if they were allowed. They are not :(

So, I was wondering if there were tricky ways. From reading the doc page, I do not SEE any "tricks" that might be cleaner than:


Call the Window script: char_main.lua that script delegates/sends a reference to the controls in question to a method in another global script that ONLY handles stuff related to X (e.g., Saving Throws)

Again - just trying to have smaller LUA files with more associated code concentrations. If it doesn't work or have issues, sure, I can toss the kitchen sink into char_main.lua.

(Now sitting at an airline counter per after 2nd canceled flight. Yay... at least have my laptop to work on)

Trenloe
August 9th, 2019, 02:20
You can't include additional scripts within XML or controls. You can layer scripts (based off layered rulesets) but this gets very complex and I would *not* recommend it.

So you have the two options you mention - the window level script (one instance of the script per window) or the global script package if you want to remove code from the control script. The thing to remember about Global packages is that there is only one instance of them, so you have to pass in either the main DB node or the window instance you're working on (best practice is to operate against DB nodes, not GUI controls/windows) and then do generic scripting accessing FG APIs on those objects.