PDA

View Full Version : tonumber? + editable fields



DNH
June 2nd, 2007, 09:17
is the lua tonumber function implemented in FG2? i have an element from the source xml which is a string, but which contains the likes of +0, -2 (and NA) and i want to use it in calculating a value. i had understood that lua will use a string that looks like a number as a number but i keep getting an error such as "attempt to perform arithmetic on global 'checkmodvalue' (a string value)". i have tried to use 'tonumber(checkmodvalue)' but then i get "attempt to perform arithmetic on a nil value".

the second issue concerns the direct editing of stringfields. i am implementing a windowlist of (AD&D2e) proficiencies and i can happily drag-and-drop all the various weapon profs from my module data. BUT i don't have (and don't want) a separate module entry for each weapon (the ones i have are for weapon groups and fighting styles). i want to be able to create a new entry through the radial menu and enter the weapon name and the number of prof slots used. i also need to be able to edit the number of prof slots used for any entry created through drag-and-drop. so far, all of this is eluding me and i can't see why.

any thoughts?

thanks.

Foen
June 2nd, 2007, 12:46
To change a text string (such as "3") to a number, just add zero to it:


local txt = "3";
local num = txt + 0;


As regards the more complex question, could you post a snippet of code so I can see what you are trying to do?

Cheers

Stuart
(Foen)

DNH
June 2nd, 2007, 13:26
thanks stuart, but i can't get this to work. here is some code on the number issue:


function update(source)

statscorenode = window.getDatabaseNode().getChild("statvalue");
checkmodnode = window.getDatabaseNode().getChild("checkmodifier");
slotsnode = window.getDatabaseNode().getChild("slots");

statscorevalue = statscorenode.getValue();
checkmodvaluetxt = checkmodnode.getValue();
checkmodvaluenum = checkmodvaluetxt + 0;
slotsvalue = slotsnode.getValue();

totalvalue = statscorevalue + checkmodvaluenum + math.floor(slotsvalue-1);

setValue(totalvalue);
end

i continue to get the error msg "attempt to perform arithmetic on global 'checkmodvaluetxt' (a string value)"

Foen
June 2nd, 2007, 13:45
How about trying:

checkmodvaluetxt = checkmodnode.getValue();
print("*" .. checkmodvaluetxt .. "*");
checkmodvaluenum = checkmodvaluetxt + 0;

And see what gets displayed on the console? You will have to type /console before opening the character sheet.

Cheers

Stuart
(Foen)

DNH
June 2nd, 2007, 14:46
[02.06.2007 14:44] Script Notice: **
[02.06.2007 14:44] Script Error: [string "charsheet_nwplistitem:total"]:1: attempt to perform arithmetic on global 'checkmodvaluetxt' (a string value)

is the result whenever i add new profs to the list. so a null string of "" plus zero remains a null string of ""?

Foen
June 2nd, 2007, 14:53
You can only add a zero to a string if the string is a number (not empty). Try the following code:



checkmodvaluetxt = checkmodnode.getValue();
if checkmodvaluetxt=="" then
checkmodvaluenum = 0;
else
checkmodvaluenum = checkmodvaluetxt + 0;
end

Cheers

Stuart
(Foen)

DNH
June 2nd, 2007, 16:08
yay! that works! that is, my total now calculates correctly from the off. now, if i apply a similar kind of logic to the statvalue field, perhaps it will display that too.

this is what i have and it ain't working properly:


function onInit()
statabbrnode = window.getDatabaseNode().getChild("ability");
statabbrvalue = statabbrnode.getValue();

abilities = window.windowlist.window.getDatabaseNode().getChil d("abilities");

for k, v in pairs(abilities.getChildren()) do
if v.getChild("abbr").getValue() == statabbrvalue then
statscore = v.getChild("score").getValue();
statvaluenode = window.getDatabaseNode().createChild("statvalue", "number");
statvaluenode.setValue(statscore);
end
end
end