Thread: Decoding Strings for Variables
-
May 25th, 2016, 12:21 #1
Decoding Strings for Variables
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?
-
May 25th, 2016, 14:16 #2
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.
Code:-- 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
-
May 25th, 2016, 14:19 #3
Actually, that should be simplified to:
rRoll.aDice[1] = sDieType;
-
May 25th, 2016, 14:38 #4
...
-
May 26th, 2016, 14:56 #5
In case anyone else has a similar question in the future, here is the code I ended up with:
Code:-- 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
Attachment 14178
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks