Variable If Else

I’ve been searching through topics and haven’t seen this scenario yet. I want to create a macro with a numeric variable input, and output a value based on that. Like:

\define adjustment(value)

if value < 10 output -5
if value >= 11 and < 15 output -2
etc

Is this possible in a TW macro?

Thanks in advance!

As you get to know filters you will find there are mathematics operators that can do this. In someways the question has a different approach depending on what you plan to do with the result(s).

The quick way to play with it is with the triple curly braces/ filtered transclusions.
{{{ filter }}}

<$let value=12>
{{{ [<value>compare:number:lt[10]then[-5]] :else[<value>compare:number:lt[15]then[-2]] }}}
</$let>

Once you are happy, in TiddlyWiki 5.3.x you can use functions

\function compute(value) [<value>compare:number:lt[10]then[-5]] :else[<value>compare:number:lt[15]then[-2]]

<<compute 2>>

But you can use such filters in many places now. So perhaps you can expand a little on what you want to do with the results.

Oh great! Thank you for that.
I may not get a chance to try that out until tomorrow, but will let you know how it goes.

I’m building a roleplaying ‘rulebook’ and will have random character generation tools etc. So there will be a LOT of number value evaluation that would output values according to the number coming in.

So I have a random number generation macro, that just outputs the number. But within that macro I want to pass the random number to another macro/function - so that it can append an adjustment. So if the number is below a set number, the second macro/function would return something like “-5”, if the random number is above a set value, the second macro/function would return like “+2” etc.

And the outcome of the first macro would become a set value within a Tiddler’s body.

So it sounds like functions would be perfect. To access those globally, what tag do I need for functions? I have the tag for macros, but haven’t played with functions yet. This is my first foray into ‘coding’ in TW.

I’ve been doing work with bundles and simple macros, but for this notebook, I’ll be needing to do more complex things.

Thanks again!

Currently the random number changes every time the Tiddler is opened, eventually I’d like to have a ‘Generate’ button, that would rebuild all the random numbers, then they would remain the same, but I’ll have to do more learning/researching into that. For now how it works is fine. :slight_smile:

Here is what I have so far:
https://open-dnd.tiddlyhost.com/

There are a range of discussions here and in the old google groups and a community in discord who have built similar games, or world reference wikis. In this random numbers, or dice roles is common.

It is best not to set text in the body if you want to store free form text in the body. Instead set a field and reference that in the body (or even better a view template)

The only way to set a value you evaluated in a tiddler is using some kind of trigger, to trigger an action like action setfield, the most common being a button.

The same tag(s) as we used for macros, or one of the new global tags. $:/tags/Global

Thank you for the answers! I’ll track down some examples of what other people have done, see how they did it. And thanks again for the feedback on setting values.

Ok I found a wiki with exactly what I need EXCEPT it receives a hardcoded value. Is there a way to pass the result of my macro to this other js macro? In the following the first two return results, but the last one returns NAN. Is there a way to pass a macro result as a parameter to another macro? So far I haven’t seen anything online that matches my scenario.

<<dnd.ability 8>>
<<abilityScoreGen “1” “6”>>
<<dnd.ability <<abilityScoreGen “1” “6”>> >>

Here is the output:

8 (−1)
5
NaN (−NaN) >>

Yes this is invalid; try;

<$macrocall $name="dnd.ability" param=<<abilityScoreGen “1” “6”>> />

You need to find what the name of the parameter is and change param accordingly.

  • Have a look in the definition of dnd.ability to find the first parameter name.

There is a list of D&D wikis, which should have “random” or “shuffle” macros, that you may be able to use.

Search for “random” and “shuffle”

I personally not involved in the D&D genre, but I do like those wikis and themes.

The Discord section: tw-for-gaming may be interesting for you. I think that’s the main “forum” for TW related D&D content.

have fun!
mario

1 Like

Thank you! That did it. I’ll improve what I have over time, but I have a prototype now.

Thank you for that list! The one I had found with an ability generator was the benwebber dnd plugin. I don’t think I saw those you listed in my search results. I’ll check those out - and the tw-for-gaming section!