PDA

View Full Version : Function Issue



bloodylemming
December 23rd, 2021, 21:21
I'm trying to move calculations out of the objects and into functions, but I'm having a bit of trouble.


Here, I'm trying to pass three values into the function, the character id, a value being manipulated and the name of the object whose value I need to set.

Here's what I have in the function


function calculateAbilityMod(nodeChar, strMod, numCurrent)

local nResult =0;

if numCurrent < 10 then
nResult = math.ceil((nScore - 10) / 2);
elseif numCurrent > 10 then
nResult = math.floor((nScore - 10) / 2);
else
nResult = 0;
end

nodeChar.getChild(strMod).setValue(nResult);

return true;

end

Here's what I have in the triggers:


function onValueChanged()
--values passed> nodeChar, strMod, numCurrent
local nodeChar = window.getDatabaseNode();
local strMod = "numStrengthMod";
local numCurrent = nodeChar.getChild("numStrengthCurrent").getValue();

charManager.calculateAbilityMod(nodeChar, strMod, numCurrent);
end

I've confirmed the variables are being passed into the function properly, but it's not setting the value in the target.
I don't think I'm handling the strMod value properly... I need this value so the function knows which stat modifier to update...

Trenloe
December 23rd, 2021, 21:23
Please provide the XML of the control anchored to the numStrengthMod DB field.

bloodylemming
December 23rd, 2021, 21:31
Please provide the XML of the control anchored to the numStrengthMod DB field.

There is no code added to numStrengthMod. That's the target I want to set the value of. Or are you talking about the XML setting up that number field?

damned
December 23rd, 2021, 22:46
above this line


nodeChar.getChild(strMod).setValue(nResult);

add


Debug.chat(nodeChar, strMod, nResult);

I suspect that you should be using nodeChar where you have used strMod

bloodylemming
December 23rd, 2021, 23:51
above this line


nodeChar.getChild(strMod).setValue(nResult);

add


Debug.chat(nodeChar, strMod, nResult);

I suspect that you should be using nodeChar where you have used strMod


Here's what I got with the debug. Everything looks right.....

s'nodeChar: ' | databasenode = { charsheet.id-00001 }
s'strMod: ' | s'numStrengthMod'
s'numCurtrent: ' | #17

Trenloe
December 24th, 2021, 00:07
Or are you talking about the XML setting up that number field?
Yes, the XML for the configuration of the number field.

bloodylemming
December 24th, 2021, 01:11
Yes, the XML for the configuration of the number field.

It's automatically generated, as I'm using Ruleset Wizard to do the layout, but here's the entry from the XML.


</number>
<number name="numStrengthMod">
<bounds>131,43,35,35</bounds>
<readonly />
<font>reference-b</font>
</number>

damned
December 24th, 2021, 01:22
Here's what I got with the debug. Everything looks right.....

s'nodeChar: ' | databasenode = { charsheet.id-00001 }
s'strMod: ' | s'numStrengthMod'
s'numCurtrent: ' | #17


Show what the data in the database looks like please.
This is in a charsheet - so find the db.xml and find <charsheet><id-00001> and copy everything from the <id-000001> to the </id-00001> tag and share here.

bloodylemming
December 24th, 2021, 01:46
Show what the data in the database looks like please.
This is in a charsheet - so find the db.xml and find <charsheet><id-00001> and copy everything from the <id-000001> to the </id-00001> tag and share here.


<id-00001>
<abilitylist />
<Checkbox1 type="number">0</Checkbox1>
<chkCharisma type="number">0</chkCharisma>
<chkConstitution type="number">0</chkConstitution>
<chkDexterity type="number">0</chkDexterity>
<chkIntelligence type="number">0</chkIntelligence>
<chkStrength type="number">0</chkStrength>
<chkWisdom type="number">0</chkWisdom>
<numCharismaCurrent type="number">10</numCharismaCurrent>
<numCharismaMod type="number">0</numCharismaMod>
<numCharismaScore type="number">10</numCharismaScore>
<numConstitutionCurrent type="number">16</numConstitutionCurrent>
<numConstitutionMod type="number">3</numConstitutionMod>
<numConstitutionScore type="number">16</numConstitutionScore>
<numDexterityCurrent type="number">14</numDexterityCurrent>
<numDexterityMod type="number">2</numDexterityMod>
<numDexterityScore type="number">14</numDexterityScore>
<numExperience type="number">0</numExperience>
<numFamiliarBonus type="number">0</numFamiliarBonus>
<numHeight type="number">0</numHeight>
<numIntelligenceCurrent type="number">8</numIntelligenceCurrent>
<numIntelligenceMod type="number">-1</numIntelligenceMod>
<numIntelligenceScore type="number">8</numIntelligenceScore>
<numLevel type="number">1</numLevel>
<numNextLevel type="number">0</numNextLevel>
<numNonProficiencyBonus type="number">0</numNonProficiencyBonus>
<numProficiencyBonus type="number">1</numProficiencyBonus>
<numSpeed type="number">0</numSpeed>
<numSpellcastingBonus type="number">0</numSpellcastingBonus>
<numStrengthCurrent type="number">17</numStrengthCurrent>
<numStrengthMod type="number">4</numStrengthMod>
<numStrengthScore type="number">18</numStrengthScore>
<numWeight type="number">0</numWeight>
<numWisdomCurrent type="number">7</numWisdomCurrent>
<numWisdomMod type="number">-1</numWisdomMod>
<numWisdomScore type="number">7</numWisdomScore>
<token type="token"></token>
<txtWounds type="formattedtext">
<p />
</txtWounds>
</id-00001>

damned
December 24th, 2021, 02:07
It looks like its working to me.

17-10/2 = 4

bloodylemming
December 24th, 2021, 02:57
It looks like its working to me.

17-10/2 = 4

Yea, that's the expected result, but I'm getting these errors.


[12/23/2021 8:56:03 PM] [ERROR] Script execution error: [string "numStrengthCurrent"]:3: attempt to call global 'onValueChange' (a nil value)
[12/23/2021 8:56:03 PM] [ERROR] Script execution error: [string "numStrengthCurrent"]:8: attempt to call global 'onValueChange' (a nil value)
[12/23/2021 8:56:16 PM] [ERROR] Script execution error: [string "campaign/scripts/charManager.lua"]:12: attempt to perform arithmetic on global 'nScore' (a nil value)

damned
December 24th, 2021, 03:16
you might not be showing us enough code.

some things to check first

the function should be onValueChanged() not onValueChange()
in your function calculateAbilityMod()
you reference nScore but you havent set nScore up yet

bloodylemming
December 24th, 2021, 03:47
you might not be showing us enough code.

some things to check first

the function should be onValueChanged() not onValueChange()
in your function calculateAbilityMod()
you reference nScore but you havent set nScore up yet

There's a very good reason nScore hasn't been initialized. It's because I'm a moron. It's supposed to be numCurrent... Stupid copy/paste...

Anywho, the function is working, but I'm still getting errors when I first open the character sheet.

[12/23/2021 9:39:51 PM] [ERROR] Script execution error: [string "numStrengthCurrent"]:3: attempt to call global 'onValueChange' (a nil value)
[12/23/2021 9:39:51 PM] [ERROR] Script execution error: [string "numStrengthCurrent"]:8: attempt to call global 'onValueChange' (a nil value)

This is all that's in numStrengthCurrent, which throws no errors when I actually run it, just when the character sheet first opens...


function onInit()
onValueChange();
end


function onFirstLayout()
onValueChange();
end


function onValueChanged()
--values passed> nodeChar, strMod, numCurrent
local nodeChar = window.getDatabaseNode();
local strMod = "numStrengthMod";
local numCurrent = nodeChar.getChild("numStrengthCurrent").getValue();

charManager.calculateAbilityMod(nodeChar, strMod, numCurrent);
end

When I comment out the onInit and onFirstLayout, it throws no errors. I have no idea why it would do that, unless it expects a value to be in that field before it's constructed.

damned
December 24th, 2021, 04:02
onValueChange() isnt a thing
onValueChanged() is the correct function

bloodylemming
December 24th, 2021, 04:38
onValueChange() isnt a thing
onValueChanged() is the correct function

That's very true. Changed those values, and no related errors being thrown, however, the die roll indicator (the d20 image) doesn't show up now. I can still double-click the object and it rolls the die, but no graphic... Comment those two out and the graphic displays. Weird...

damned
December 24th, 2021, 04:52
Well that is another issue...

bloodylemming
December 24th, 2021, 05:56
Well that is another issue...

One I guess I'll live with. Thanks for all the help. Hopefully I understand functions better...

Trenloe
December 24th, 2021, 16:37
... the die roll indicator (the d20 image) doesn't show up now. I can still double-click the object and it rolls the die, but no graphic...
I don't use Ruleset Wizard, so I don't know how it would display the dice graphic. However, in the XML you provided in post #7 there is nothing that indicates to show the dice graphic. In the usual CoreRPG based rulesets this would be set in XML with the <rollable /> XML tag.

bloodylemming
December 24th, 2021, 22:16
I don't use Ruleset Wizard, so I don't know how it would display the dice graphic. However, in the XML you provided in post #7 there is nothing that indicates to show the dice graphic. In the usual CoreRPG based rulesets this would be set in XML with the <rollable /> XML tag.

It's not much of an issue, at this point. The field is rollable, so I'll figure that out later. So many other walls to bang my head against. !)