-
Something like this might work if I understand your goal.
Code:
local sAttacks = "";
for _,nodeAttack in pairs(DB.getChildrenGlobal(nodeRecord, "attacks")) do
local nNumberOfAttacks = DB.getValue(nodeAttack, "numberOfAttacks", 1);
local sSep = "";
if sAttacks ~= "" then
sSep = ", ";
end
sAttacks = sAttacks .. sSep .. tostring(nNumberOfAttacks) .. DB.getValue(nodeAttack , "name") .. " (" .. DB.getValue(nodeAttack , "damage") .. ")";
end
DB.setValue(nodeRecord, "attacks.numberattacks_text", "string", sAttacks);
-
You guys ABSOLUTELY rock!
1. Yes, I have no CLUE when or IF I'd have figured out the sparse array thing. I am aware of the difference but it never occurred to me to switch to index - at least for a long time. Once I did this, the issue of failing to iterate properly went way. I switched to getChildCount() instead of # operator and that solved the odd numbers returned (Celestian, #ofAttacks as a field was different than numberOfAttkEntries (which isn't what I called it -so that's why it was confusion - #attk referred to overall how many entries so I knew when I was at the last one - will be clearer when done tonight or tomorrow and post the fixed version. Have to put boy late to bed.)
2. Celestian, yeah - I know I started trying a version of "self[numberattacks_text_label].setValue(sAttacks);" because of misunderstanding use of self[control].update(...) code that was already there - BUT I thought I DID try it straight - I must have done something wrong, because it worked when I tried it on a label field. Will redo the control to emulate look of the stringcolumnleft control before finishing.
Seriously, than you for solutions and forever the patience.
-
Debug printout:
Runtime Notice: Reloading ruleset
...
Runtime Notice: s'AttkStr=' | s'1 Bite (1d6), 2 Claws (1d6)'
I had to leave off on the control template to sleep ;) but the code showing a logging of the output *so far* is:
Code:
function onAttacksChanged(nodeRecord)
local aAttacks = DB.getChildren(nodeRecord, "attacks");
local nTypesOfAttacks = DB.getChildCount(nodeRecord, "attacks");
Debug.console("onAttacksChanged", nodeRecord, nTypesOfAttacks);
local sAttacks = "";
local count = 0;
for idx,attack in pairs(aAttacks) do
count = count + 1;
Debug.console(idx, "Attk:", attack.getNodeName(), attack);
local nNumberOfAttacks = DB.getValue(attack, "numberOfAttacks", 1);
if nNumberOfAttacks == 1 then
sAttacks = sAttacks .. tostring(nNumberOfAttacks) .. " " ..
DB.getValue(attack, "name");
else
sAttacks = sAttacks .. tostring(nNumberOfAttacks) .. " " ..
Plural.lookup(DB.getValue(attack, "name"));
end
sAttacks = sAttacks .. " (" .. DB.getValue(attack, "damage") .. ")";
Debug.console("IDX=", count, "#Attks", nTypesOfAttacks)
if count ~= nTypesOfAttacks then
sAttacks = sAttacks .. ", ";
end
end
Debug.console("AttkStr=", sAttacks);
numberattacks_text.setValue(sAttacks);
end
That said, any reason NOT to use a "control" vs calculating it once and storing in the NPC-DB?
I just didn't want to fill it up with fields that are nothing other than combinations of existing fields. But if there is a big cost to the control way, I would reconsider.
In this case (and of "AC Down [Up]") I do not need to ever do anything with the string other than show it to folks.
-
If all the control is ever going to do is show it, it's never going to be accessed by any code and the value is set by code within the same GUI window, then a control rather than a field, is OK.
Are you going to be displaying this data in then combat tracker fields?
-
Nope, merely a nicer formatted (space-saving) way of displaying the underlying fields that will be referenced.
If I were to use that underline click thing feature and use this string elsewhere I’d save it. I wasn’t sure it was efficient to keep calculating it each time it is loaded due to the concatenation (Leery of string concatenation in the real time apps I work with, habit) but figured it’s a thing to keep in sync if I make it a data field, separately editable.
So making it a derived field actually in the DB is best middle ground other than (IMO) cluttering up the stored npc. But if I need it anywhere else, then yes, I would change it.