-
July 15th, 2020, 19:17 #1
String patterns (find/replace dice string values)
hey guys! i've been working on something for 5e here for the past couple of weeks that is really coming together, and i believe the last thing i need is to be able to replace some things.
so i have a window. the window has 6 windowlists (traits, actions, reactions, legendary actions, lair actions, innate spells).
i have, through trial and error, found how to find and replace the attack bonus of an action that is built up in the action windowlist when i drop an npc on the window. problem is that it does this for the damage as well.
i ultimately want to try and achieve at least 3 things here, but just need a to understand what the pattern i need is and why that pattern works as it does.
1 - replace the attack bonus
2 - replace the damage bonus
3 - replace the DC stated in the text.
this is my parseComponents() function, largely taken from the npc_skills.lua from 5e. the "actionSource" is the "Action" list windowinstance that is created and is being edited by this function. the parseComponents() function is currently called upon by an onDrop() function that creates the windowinstance of the action and then calls on the parseComponents() function to edit after.
Code:function parseComponents(actionSource) aComponents = {}; if DB.getValue(getDatabaseNode(), "dexbasedattacks", 0) == 0 then AttackBonus = tostring(proficiency.getValue() + DB.getValue(getDatabaseNode(), "abilities.strength.bonus", 0)); else AttackBonus = tostring(proficiency.getValue() + DB.getValue(getDatabaseNode(), "abilities.dexterity.bonus", 0)); end -- Get the comma-separated strings local aClauses, aClauseStats = StringManager.split(DB.getValue(actionSource, "desc", ""), ",;\r", true); Debug.console("parseComponents(), aClauses, aClauseStats", aClauses, aClauseStats); -- Check each comma-separated string for a potential skill roll or auto-complete opportunity for i = 1, #aClauses do local nStarts, nEnds, sLabel, sSign, sMod = string.find(aClauses[i], "([%w%s\(\)]*[%w\(\)]+:)%s*([%+%-–]?)(%d*)"); Debug.console("parseComponents(), nStarts, nEnds, sLabel, sSign, sMod", nStarts, nEnds, sLabel, sSign, sMod); if nStarts then -- Calculate modifier based on mod value and sign value, if any local nAllowRoll = 0; local nMod = 0; if sMod ~= "" then local ReCheck = StringManager.multireplace(DB.getValue(actionSource, "desc", ""), sMod, AttackBonus); DB.setValue(actionSource, "desc", "string", ReCheck); Debug.console("ReCheck", ReCheck); nAllowRoll = 1; nMod = tonumber(sMod) or 0; if sSign == "-" or sSign == "–" then nMod = 0 - nMod; end end -- Insert the possible skill into the skill list table.insert(aComponents, {nStart = aClauseStats[i].startpos, nLabelEnd = aClauseStats[i].startpos + nEnds, nEnd = aClauseStats[i].endpos, sLabel = sLabel, nMod = nMod, nAllowRoll = nAllowRoll }); end end bParsed = true; end
Last edited by pr6i6e6st; July 15th, 2020 at 19:24.
-
July 15th, 2020, 20:30 #2
seems like i might be able to do this if for the "if sMod ~= "" then" section i replace it with
Code:if sMod ~= "" then if sLabel == "Melee Weapon Attack:" then local ReCheck = StringManager.multireplace(aClauses[i], sMod, AttackBonus); local ReCheck2 = StringManager.multireplace(DB.getValue(actionSource, "desc", ""), aClauses[i], ReCheck); DB.setValue(actionSource, "desc", "string", ReCheck2); Debug.console("ReCheck, ReCheck2, aClauses[i]", ReCheck, ReCheck2, aClauses[i]); end end
-
July 15th, 2020, 20:57 #3
- Join Date
- May 2007
- Posts
- 84
One thing to note is that based on your regex expression, sLabel will be equal to " Melee Weapon Attack:" (that is, with a leading space), so you might want to change to just look for "Attack:" is contained in the string
-
July 15th, 2020, 21:56 #4
I didn’t appear to have that issue, though it may be useful for accommodating the different types of attacks after. What is the expression I’m looking for to find a diceroll expression?
And will the miltireplace function only work on single characters? I can’t seem to get it to replace the entire component.
-
July 15th, 2020, 22:46 #5
replacing the ReCheck2 line with the following yeilds slightly better results. that's a start, but i'm worried of it interfering with other bonus's that might appear in the action text?
Code:local ReCheck2 = StringManager.multireplace(DB.getValue(actionSource, "desc", ""), sSign .. sMod, sSign .. AttackBonus);
-
July 16th, 2020, 07:14 #6
so this following line is pulling out "Hit:", "9", "(1d10 + 4", "nil" from a string component that reads "Hit: 9 (1d10 + 4) bludgeoning damage"
Code:local nDamStarts, nDamEnds, sDamLabel, sDamAverage, sDieRoll, sDamBonus = string.find(aClauses[i], "([%w%s\(\)]*[%w\(\)]+:)%s*(%d*)%s*([(%d*)]+[d][(%d*)]+%s?[%+%-–]?%s[%d*)]?)");
-
July 16th, 2020, 07:16 #7
Supreme Deity
- Join Date
- Mar 2007
- Posts
- 20,561
Sometimes I find that breaking down the expression matching into steps can work better than doing a monster regular expression. Of course, sometimes it's just as much work; but it helps me to work through the problem easier.
Regards,
JPG
-
July 16th, 2020, 17:39 #8
that helped, thank you. i managed to break it all down and now have all the information i need i think. i get a return of a dice expression without the brackets, i get the bonus if there is one and the sign that comes with it, they're separated. now to replace numbers and rebuild the string.
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks