Mephisto
April 2nd, 2023, 13:27
It's amazing. It is nearly spot on, the example using the number of rolled dices ist perfect but still, just amazing.
Q: Can you explain this lua code? local sSign, sColor, sDieSides = vDie.type:match("^([%-%+]?)([dDrRgGbBpP])([%dF]+)");
A: Certainly! This code is using a Lua string pattern matching function to parse a string that represents a dice roll notation, such as "2d6" or "-1d20".
Here's what each part of the code means:
local sSign, sColor, sDieSides declares three local variables that will be used to store the values extracted from the string.
vDie.type is the string that we want to extract values from.
:match("^([%-%+]?)([dDrRgGbBpP])([%dF]+)") is a Lua string pattern matching function that attempts to match the string against a given pattern. The pattern is defined inside the parentheses.
"^([%-%+]?)([dDrRgGbBpP])([%dF]+)" is the string pattern that we're matching against. Here's what each part of the pattern means:
* ^ means that the match should start at the beginning of the string.
* ([%-%+]?)([dDrRgGbBpP]) is a pattern that matches a sign (+ or -), followed by a letter that represents the type of die being rolled (d, D, r, R, g, G, b, B, p, or P). The () brackets create capture groups that can be used to extract the matched values later on.
* ([%dF]+) is a pattern that matches one or more digits (0-9) or the letter "F". This represents the number of dice being rolled or a fixed value, such as "F" for a flat bonus. Again, the () brackets create a capture group for this value.
So when we run vDie.type:match("^([%-%+]?)([dDrRgGbBpP])([%dF]+)"), Lua will attempt to match the pattern against the vDie.type string. If the string matches the pattern, the match() function will return three values: the sign, the die type, and the number of dice or fixed value. These values are stored in the sSign, sColor, and sDieSides variables, respectively.
For example, if vDie.type is "-2d6", then the match() function will return "-" as the sign, "d" as the die type, and "2" as the number of dice. These values will be stored in the sSign, sColor, and sDieSides variables, respectively
Q: Can you explain this lua code? local sSign, sColor, sDieSides = vDie.type:match("^([%-%+]?)([dDrRgGbBpP])([%dF]+)");
A: Certainly! This code is using a Lua string pattern matching function to parse a string that represents a dice roll notation, such as "2d6" or "-1d20".
Here's what each part of the code means:
local sSign, sColor, sDieSides declares three local variables that will be used to store the values extracted from the string.
vDie.type is the string that we want to extract values from.
:match("^([%-%+]?)([dDrRgGbBpP])([%dF]+)") is a Lua string pattern matching function that attempts to match the string against a given pattern. The pattern is defined inside the parentheses.
"^([%-%+]?)([dDrRgGbBpP])([%dF]+)" is the string pattern that we're matching against. Here's what each part of the pattern means:
* ^ means that the match should start at the beginning of the string.
* ([%-%+]?)([dDrRgGbBpP]) is a pattern that matches a sign (+ or -), followed by a letter that represents the type of die being rolled (d, D, r, R, g, G, b, B, p, or P). The () brackets create capture groups that can be used to extract the matched values later on.
* ([%dF]+) is a pattern that matches one or more digits (0-9) or the letter "F". This represents the number of dice being rolled or a fixed value, such as "F" for a flat bonus. Again, the () brackets create a capture group for this value.
So when we run vDie.type:match("^([%-%+]?)([dDrRgGbBpP])([%dF]+)"), Lua will attempt to match the pattern against the vDie.type string. If the string matches the pattern, the match() function will return three values: the sign, the die type, and the number of dice or fixed value. These values are stored in the sSign, sColor, and sDieSides variables, respectively.
For example, if vDie.type is "-2d6", then the match() function will return "-" as the sign, "d" as the die type, and "2" as the number of dice. These values will be stored in the sSign, sColor, and sDieSides variables, respectively