PDA

View Full Version : Function sharing



statik37
January 14th, 2021, 17:55
I have a, probably, silly question.

If I have a template in a windowclass that references a lua file and I want to initate a function in that file from a different lua file in a different template in a different windowclass in the same file?

So for example



<windowclass name ="main">
<sample_1 name="firstTemplate">
</windowclass>

<windowclass name ="sub">
<sample_2 name="secondTemplate">
</windowclass>


If you go into the sample_1 template there is

<script file="scripts/primary.lua" />

Can I add a lua file to sample_2 that calls a function from primary.lua?

I tried window.functionName(), but it doesn't want to work. I know I'm missing something simple, I just can't see it.

Trenloe
January 14th, 2021, 18:22
You can call a script in another control using <GUI hierarchy>.<controlname>.<function>

This relies on navigating the GUI hierarchy, and also the GUI controls being in place. It therefore can be an unreliable approach - especially if the code is being called during onInit functions or on GUI entities that have multiple tabs.

Not knowing your full GUI hierarchy, I can't suggest an accurate hierarchy string. But, if those windowclasses are subwindows you will probably need something like main.subwindow.firstTemplate.functionName()

If you're doing a operation involving data, I'd strongly recommend you code directly against the database than the GUI.

statik37
January 14th, 2021, 19:38
OK, this shows a poor choice in naming from me. When you say main.subwindow, I assume you don't mean to refer to the windowclass I called main, but main as in main branch of the file. In otherwords if that first subwindow was named "first" instead of "main", then I would still use the keyword main.

Trenloe
January 14th, 2021, 20:40
OK, this shows a poor choice in naming from me. When you say main.subwindow, I assume you don't mean to refer to the windowclass I called main, but main as in main branch of the file. In otherwords if that first subwindow was named "first" instead of "main", then I would still use the keyword main.
GUI hierarchy uses either the name of a control or one of the reserved variable names. "main" is not a reserved variable name, so I was indeed referring to the naming used. But, as I said, I don't know your hierarchy, so what I posted was only a guess - which could be way off.

Details on script block scope, and reserved variables names, can be found in the Wiki here: https://fantasygroundsunity.atlassian.net/wiki/spaces/FGCP/pages/996644496/Ruleset+-+Scripting#Script-Block-Scope