PDA

View Full Version : Decoding Strings for Variables



leozelig
May 25th, 2016, 12:21
I am trying to pull a die type from a simple string, but I don't understand the parsing code very well. For my NPCs, they will have an 'action die' string in the following format:

1d20
2d24
etc.

I am trying to ignore the first number, and just pull the "d##" for building the attack roll. The variable can be a string, but I will be converting it to a die. Does anyone know how to code that?

leozelig
May 25th, 2016, 14:16
I think I figured it out. I stole some code from the convertStringToDice function in the StringManager and carved out what I didn't need. The "actiondice" string will be something like "1d16" or "2d24", and I have chosen to ignore the die count for now.


-- Get Action Die for NPCs
if sActorType == "npc" then
local sNPCActionDie = DB.getValue(nodeActor, "actiondice");
local aRulesetDice = Interface.getDice();
for v in sNPCActionDie:gmatch("([%da-zA-Z]+)") do
local sDieCount, sDieNotation, sDieType = v:match("^(%d*)([a-zA-Z])([%dF]+)");
if sDieType then
sDieNotation = sDieNotation:lower();
sDieType = sDieNotation .. sDieType;
if StringManager.contains (aRulesetDice, sDieType) or (sDieNotation == "d") then
local sDie = sDieType;


rRoll.aDice[1] = sDie;
end
end
end
end

leozelig
May 25th, 2016, 14:19
Actually, that should be simplified to:

rRoll.aDice[1] = sDieType;

damned
May 25th, 2016, 14:38
...

leozelig
May 26th, 2016, 14:56
In case anyone else has a similar question in the future, here is the code I ended up with:


-- Get action die for NPCs
if sActorType == "npc" or sActorType == "ct" then
local sNPCActionDie = DB.getValue(nodeActor, "actiondice");
local aRulesetDice = Interface.getDice();
local sDieCount, sDieNotation, sDieType = sNPCActionDie:match("^(%d*)([a-zA-Z])([%dF]+)");
if sDieType then
sDieNotation = sDieNotation:lower();
sDieType = sDieNotation .. sDieType;
if StringManager.contains (aRulesetDice, sDieType) or (sDieNotation == "d") then
rRoll.aDice[1] = sDieType;
end
end
end


Here is a screenshot of the result:

14178