PDA

View Full Version : Calling of function in a separate .lua script file



Nilliom
August 16th, 2021, 19:48
Hi

I have a question about scripting in FG.

I have defined a function in a lua file of the charsheet directory of the ruleset I'm working on.

I want to call that function onInit and onValueChanged of a attributecheckbonus "widget". But I have to use the same function on 2 other attributescheckbonus since 3 attributes are included in the calculation of the function.

How can I call that function, (lets call it calcMentalLimits) from 3 attributes on the onInit and onValueChanged without copy pasting the same code 3 places in the script block of the attributecheckbonus widget?

I already put the code in a script .lua file but don't know how to reference it correctly at the right location to be able to use the function without having a name of function (nil) message...

Thanks

jharp
August 16th, 2021, 20:00
Add a <script name="GameSystem" file="scripts/manager_gamesystem.lua" /> into base.xml.

Then you can use GameSystem.calcMentalLimits. You add that in the base.xml. If you are doing a lot of this type of work I personally suggest Ruleset Wizard. I use it exclusively.

Jason

Edit: My first option was incorrect. Turns out Ruleset Wizard had been helping me by creating a name mapping that allowed it to work.

Trenloe
August 16th, 2021, 20:33
If for example the LUA filename is named "external.lua" then you can call it with external.calcMentalLimits().
That won't work. You can't just refer to a LUA file in that way.


<script name="GameSystem" file="scripts/manager_gamesystem.lua" />

Then you can use GameSystem.calcMentalLimits. You add that in the base.xml.
Yes, this will work - these are known as Global Script packages.


@Nilliom - there are three ways to call LUA functions - in controls, window instances (window classes) or via Global Script Packages - these are described here: https://fantasygroundsunity.atlassian.net/wiki/spaces/FGCP/pages/996644496/Ruleset+-+Scripting#Using-Scripts

It sounds like you're wanting to call a function that is attached to a control. In which case you can use <GUI hierarchy path>.<control name>.<function name> - maybe something like window.attributecheckbonus.myFunction

The issue could be if you can't reliably traverse the GUI hierarchy from where your code is running.

Whereas it's possible to call the onInit and onValueChanged functions externally, it's not recommended as these are called at specific times in the control's life via a system generated event. If you want to run some code outside of these events then it's best to have these in custom functions within the control's LUA script and then call these externally and also from within the onInit/onValueChanged event handlers if they share the same code.

Nilliom
August 16th, 2021, 21:30
Thanks it works :)

I have some questions for you about Ruleset Wizard.

For me it would end up costing 73$ Canadian to buy that software, which I find to be a steep price so I would like to know what I would buy, especially for a beta software.

Can we use it to update an existing ruleset?
Is it easy to use?
Is it complete, that you can do everything needed in a ruleset?
Is it stable, does it crash often or scrap your work?

Thanks for your help :)

superteddy57
August 16th, 2021, 23:37
It would be best to post your questions related to that program in it's own thread:
https://www.fantasygrounds.com/forums/showthread.php?61387-Ruleset-Wizard-The-new-Ruleset-development-environment

That way you can get speedier answers and the developer also watches that thread.

damned
August 16th, 2021, 23:37
No IDE will build everything for you.
Is it complete? What does that mean? You can do everything within the tool but you must still write some code and you must still understand how FG rulesets work.

Updating an existing ruleset might be easy or it might be very complex.
If you want to change the font of the name field thats easy on the other hand the range of your changes could be nearly infinite.

I have had it crash twice and I lost the 5 or 10 mins of work I had been working on.
I have once had a file get corrupted but I dont know if it was RW or something else that caused it.

I have written several full rulesets inside the RW - I think its a great tool.