Hi all,
I’m looking for a little help and feedback on a project. I feel like I’m pretty close to getting my current approach to work, but I’m also wondering if there is a better way.
Background:
I have a TW I made as a reference for running Pathfinder/D&D games. Across a wide variety of fields and tiddlers I have text matching the (^|\s)\d+(d(2|4|6|8|10|12|20))?(\+\d+)?($|\s)
regex ie. 1d4+2, 5d6, 6. Respectively, the sum of a 4 sided die result and 2, the sum of the results of five, 6 sided dice, and the value 6.
Ultimately, I would like to make it so that for any such string matching the regex in my wiki, clicking on the text returns a new die/dice roll matching that string. As an example, given the text “1d4+2”. Clicking on that text transforms it into something like “1d4+2 (5)”. Clicking again might give you “1d4+2 (3)”.
Current Status:
I have a javascript macro that accepts a string <<dice_roll roll_str:"1d4+2">>
and returns the appropriate random result, such as “2”.
I am using the button widget in a macro like this:
\define roller(input) <$button class="tc-btn-invisible" set=!!DieResult setTo=<<dice_roll roll_str:$input$>> >''$input$'' ({{!!DieResult}})</$button> \end
The resulting invocation looks like this: <<roller 1d8>>
The pros:
-
The invocation is short
-
Once the invocation is substituted for the initial text, it works, kinda.
The con: -
As I’ve currently implemented, “DieResult” is a field, and therefore global with respect to the tiddler. All of the rollers return the exact same result at the same time (see image).
What I’m looking for: -
How can I make the rollers independent from each other?
- It seems like having the button set a variable instead of a field would solve the issue but I can’t figure out how to do that.
- I did try to use a set widget inside the button hoping that each set would only be evaluated on the click of that actual button, but apparently things don’t work that way.
- If all else fails I can add a single “Die Result” box at the top of most of the pages, but that isn’t my preferred result.
-
Is there an established way to do something like the built in camelCase linking globally with strings matching this regex?
- The vast majority of my tiddlers are bulk imported in JSON format from external sources. I could do a find replace on those files before importing, but that will have it’s own issues (potential corruption and not all the fields are evaluated again before rendering). I use a small number of ‘templates’ and transclusion to render the information.
-
Just in general, was there a smarter way to go about this? Professionally I do CLI scientific coding (C/C++/FORTRAN/Python/R), so while I can do some robust software engineering, but the Wikitext/HTML5/CSS/javascript interplay is pretty foreign.
All the best,
Baj