PDA

View Full Version : How to determine when a function is fired



KlatuB
September 22nd, 2018, 20:01
I'm writing my first extension for FG, what it will do is calculate the remaining XP needed to go to the next level.

The issue I'm having is that when I change the value of the current XP or Next Level XP fields the extension doesn't update my XP remaining field.

Questions

1. Did I code something wrong (no error when loading in FG)
2. Am I using the wrong function to do the update.
3. I have tried both Debug.console and Debug.chat to try to troubleshoot but neither will show any messages, how to find out if the function is being called

Trenloe
September 22nd, 2018, 20:56
Which event are you referring to?

Loading up this extension, when I press the magnifying glass button (control = calcXpNeeded) the onButtonPress() event runs and I get a console error: Script Error: [string "calcXpNeeded"]:1: bad argument #4 to 'setValue' (number expected, got string)

KlatuB
September 22nd, 2018, 21:15
Trenloe,

Sorry about that, I uploaded the wrong version, the function I'm having problems with is the onSourceUpdate() in the XP Needed field definition.

Trenloe
September 22nd, 2018, 21:37
onSourceUpdate is not a predefined event. You'll need to setup specific event handlers to trigger onSourceUpdate.

Put the following in the <script> block before the onSourceUpdate function:


function onInit()
local nodeWin = window.getDatabaseNode();
DB.addHandler(DB.getPath(nodeWin, "exp"), "onUpdate", onSourceUpdate);
DB.addHandler(DB.getPath(nodeWin, "expneeded"), "onUpdate", onSourceUpdate);

onSourceUpdate();
end

You might also want to make the control <readonly /> so that it can't be updated through the interface.

KlatuB
September 22nd, 2018, 22:46
Trenloe,

Thanks, that is what I was looking for.

I had posted a similar question on the 5E forum, and Damned helped clarify the error you first got when you tried the extension. He also made a few suggestions

1. Make it calculate any time you change the level, XP or Next Level values.
2. Make it calculate when you open that window as well.
3. You might also look at automatically setting the Next level values every time the Level changes.

Originally I was trying to due the update via a button, but I liked suggestion #1 more so that is what is does now.

I would like to try and do suggestions 2 & 3. I'm assuming that for suggestion 2, I would put the same onInit and onSourceUpdate code of the "charsheet_classes" windowclass. When I tried this (as is) I get a console error: Script Error: [string "charsheet_classes"]:1: attempt to index global 'window' (a nil value)

How do I get the DatabaseNode when opening the window.

Trenloe
September 22nd, 2018, 23:03
Suggestion 2 is included in the code I provided. It runs onSourceUpdate as part of the onInit function when the window is opened.

KlatuB
September 23rd, 2018, 06:12
Thanks for clarifying that Trenloe, I was thinking the onInit was only dealing with the field it was defined on.

As for suggestion 3, I would like to add the calc code, to code that calculates the total class level (if that is the best place to put it), do you know where I should look for that code.

Trenloe
September 23rd, 2018, 09:45
Thanks for clarifying that Trenloe, I was thinking the onInit was only dealing with the field it was defined on.
onInit within that fields codes is only triggered when that field is initialized. But that field is initialized when the window is first opened, so it will run, setup the DB handlers for the fields it needs to monitor for changes and also with calling onSourceUpdate() it will do an initial calculation based off the other XP fields when the window is opened.


As for suggestion 3, I would like to add the calc code, to code that calculates the total class level (if that is the best place to put it), do you know where I should look for that code.
Look at the code in campaign\scripts\char_manager.lua - function addClassRef. There is code in there that calculates the total level - starts at the "-- Calculate total level" comment and also where "nodeList" is created earlier in the function. You can extract these few lines of code to use to calculate the total level.

KlatuB
September 23rd, 2018, 18:18
Trenloe,

Thanks for all the help, I have everything working the way I want it. I will be posting the finished extension over on the 5e forum shortly.

Trenloe
September 23rd, 2018, 18:31
Trenloe,

Thanks for all the help, I have everything working the way I want it. I will be posting the finished extension over on the 5e forum shortly.
No worries. Pleased you got it working they way you want. :)