How do I…
I’m making a calculator app with extra buttons for RPG dice (d4, d6, d8, d10, d12, d20, d100) and some custom preset buttons for certain weapons and spells. Got it working like a normal calculator, and when adding dice results together like “d6+d6” it will work as intended, choosing a random result for each d6 and adding them together.
The problem I’m facing is that I want to be able to enter something like “2d6” instead of “d6+d6” to do the same thing, but I can’t figure out an elegant way to do this with events that doesn’t require me to make events checking for every unique string from 1 to 100 for each of the dice types (700+ manually created events).
Right now I have events checking for the names of each die in the expression string (when I say “expression” I mean in the mathematical sense, not gdevelop expressions) and replacing them with random numbers in the corresponding range, then using a simple little javascript code block to evaluate the expression and send the result out to a scene variable, which the next events use to display the result.
What is the wanted result
I want a mathematical expression written like “2d8” or “2*d8” to functionally do the same thing as “d8+d8” without having to tediously manually create hundreds of events that check for and replace unique strings before the calculation event.
I want to be able to type, with the calculator buttons, mathematical equations like “1d10+2d8+6” and have events translate that string into “d10+d8+d8+6” so that I can have my already-working events splice the random numbers in to change the string into its final form of something like “5+1+8+6” before it is sent to the javascript code block for calculation.
What is the current result
Currently, only dice name strings are being replaced by a random number, within the main expression string (expected, but insufficient).
This means that something like “1d4” will get translated into “11”, “12”, “13”, or “14” before getting sent into the javascript code block to be calculated.
Also, strings like “2*d4” will only have that single “d4” string be replaced by a random number from 1 to 4, which then gets multiplied by 2, when what I actually want is for the d4 to be rolled twice and added together.





