How to have one button do multiple changes (solved)

Hi,

I’m currently trying to generate some encounters. The problem I have run into is there is only 1 action statement for each button action.

So when I want to generate an en encounter the result of the first dice rolls need to be computed before the next actions can be randomized.

I tried to solve this by just generating a lot of randoms in advance, but this fails when trying to store the results.

Is there a way to solve this or do I need a two button system to generate then store the results.

Below is the current version, it first sets 16 random numbers then uses these numbers to generate mobs.


<$button actions='
<$action-setfield $tiddler="$:/temp/persistent-randomR1" text={{{ [random[1],[20]] }}}/>
<$action-setfield $tiddler="$:/temp/persistent-randomR2" text={{{ [random[1],[20]] }}}/>
<$action-setfield $tiddler="$:/temp/persistent-randomR3" text={{{ [random[1],[20]] }}}/>
<$action-setfield $tiddler="$:/temp/persistent-randomR4" text={{{ [random[1],[20]] }}}/>
<$action-setfield $tiddler="$:/temp/persistent-randomR5" text={{{ [random[1],[20]] }}}/>
<$action-setfield $tiddler="$:/temp/persistent-randomR6" text={{{ [random[1],[20]] }}}/>
<$action-setfield $tiddler="$:/temp/persistent-randomR7" text={{{ [random[1],[20]] }}}/>
<$action-setfield $tiddler="$:/temp/persistent-randomR8" text={{{ [random[1],[20]] }}}/>
<$action-setfield $tiddler="$:/temp/persistent-randomR9" text={{{ [random[1],[20]] }}}/>
<$action-setfield $tiddler="$:/temp/persistent-randomR10" text={{{ [random[1],[20]] }}}/>
<$action-setfield $tiddler="$:/temp/persistent-randomR11" text={{{ [random[1],[20]] }}}/>
<$action-setfield $tiddler="$:/temp/persistent-randomR12" text={{{ [random[1],[20]] }}}/>
<$action-setfield $tiddler="$:/temp/persistent-randomR13" text={{{ [random[1],[20]] }}}/>
<$action-setfield $tiddler="$:/temp/persistent-randomR14" text={{{ [random[1],[20]] }}}/>
<$action-setfield $tiddler="$:/temp/persistent-randomR15" text={{{ [random[1],[20]] }}}/>
<$action-setfield $tiddler="$:/temp/persistent-randomR16" text={{{ [random[1],[20]] }}}/>
' >
Roll for Encounters
</$button>

<!-- First decide the threat level of the first mob -->
<% if [[1]match{$:/temp/persistent-randomR1}] %>
 Very Weak
<% elseif [[20]match{$:/temp/persistent-randomR1}] %>
 Very Strong
<% elseif [[15]compare:number:lteq{$:/temp/persistent-randomR1}] %>
 Strong
<% elseif [[5]compare:number:gteq{$:/temp/persistent-randomR1}] %>
 Weak
<% else %>
Average
<% endif %>
&nbsp;
<!-- First decide the type of the first mob -->
<% if [[1]match{$:/temp/persistent-randomR2}] %>
 Boss
<% elseif [[20]match{$:/temp/persistent-randomR2}] %>
 Elite
<% elseif [[15]compare:number:lteq{$:/temp/persistent-randomR2}] %>
 Lesser
<% elseif [[5]compare:number:gteq{$:/temp/persistent-randomR2}] %>
Minion
<% else %>
Average
<% endif %>

This is not an actual solution to your question, but just a way to make your random number generator $button much more compact:

\define temp() $:/temp/persistent-randomR
\define act()
<$list filter="[range[16]]" variable=r>
<$action-setfield $tiddler={{{ [<temp>addsuffix[r]] }}} text={{{ [random[1],[20]] }}}/>
</$list>
\end
<$button actions="<<act>>">Roll for Encounters</$button>

enjoy,
-e

1 Like

Thank you for that part of the solution.

It looks like very easy things in average computer languages take a bit more work in Tiddlywiki, while some other things that are difficult in computer languages are build in tiddlywiki.

Specifically variable manipulation and subsequent storage seems to take a bit of work, as the base system updates most things as soon as something is changed. Thus arbitrarily changing something can have unintended consequences.

edit:
The given code doesn’t work properly, it makes 1 new tiddler, $:/temp/persistent-randomRr instead of 16. looks like the suffix uses r instead of the variable r.

changing r to <r> and <<r>> changed the created toddler to
$:/temp/persistent-randomR<r> and
$:/temp/persistent-randomR<<r>>

so it looks like addsuffix only accepts literals, not variables.

edit2:

once I changed it to addsuffix<r> it updated all 16

codeblocks are required or it doesn’t show the < r >

Oops! My bad. As you’ve already realized, that line should be:

<$action-setfield $tiddler={{{ [<temp>addsuffix<r>] }}} text={{{ [random[1],[20]] }}}/>

-e

Of course one button can trigger multiple actions, for example see the use of action create tiddler and the additional use of action navigate on tiddlywiki.com

However there are some caviets such as actions within the button are rendered when the button is displayed and stays the same until clicked.

Using the actions= parameter of the button widget will render the actions (outside the button) when the button is clicked.

Although we can assist you here, remember there are many functions built into a default tiddlywiki including a whole set of buttons on tiddlers, in the Page Controls, editor toolbar buttons and provided by plugins. It is helpful if you learn to research how tiddlywiki works via the open source already visible within a live tiddlywiki eg advanced search > Shadows > “button” will list many of the button tiddlers and reading their contents give you clues how to do it.

  • I often review what the default wiki or tiddlywiki does that is similar to what I want to do, then look at the wikitext behind, sometimes the javascript (eg filteroperators) to see how it is done, then experiment with, clone and customise to get what I want. Safely on an empty.html.
  • The export buttons, and instructions on building a custom export also show how quite complext templates can be used at the time the export (type) button is selected.

Thank you for explaining this.

I was getting lazy by hoping for a community solution, instead of trying to understand the inner workings of tiddlywiki.

Using an empty wiki to experiment never occurred to me, and using that doesn’t risk the main wiki.

I think I already found something that might work in this case, using the range statement earlier in this topic.

What if say 100 random numbers are generated in advance, say when opening the wiki, or pressing a start encounter button.

Then before the roll for encounter button is pressed, the encounter is displayed using these rolls, and that information is stored in fields when the roll for encounter button is pressed. And after storing the information, the next set of 100 numbers is generated with the same button press.

If it turns out more than 100 randoms are needed, which is very likely, then simply the amount of randoms generated can be increased.

A global counter can be added to keep track of which random numbers is the current one, and be used a a parameter for procedures/functons/macros used.