Hi,
I am currently busy creating a Character Sheet for a RPG.
The display part is in a $:/tags/Global tagged file, so I can reuse the code.
The Statistics data is currently in a Character Tiddler, in the fields, to test it. I may later switch to data tiddlers for that.
Now the problem is that I want a procedure or something to display the modifier, based on the field value.
For example:
0 or 1 should return a modifier of -3.
2 to 4 would be -2
5 to 9 would be -1, etc.
Now if I write another global procedure for this, using the compare operator, the currentTiddler is different for the first global procedure I think?
Also, it is a bit awkward to make a longish <% if %> chain.
So I was wondering is there an easier way to find the right value to display, given that all the data is known before hand?
The table below is the first global procedure.
The modifiers should appear in the third block.
<div style= "overflow-x: auto;">
<table>
<tr>
<th></th>
<th>Might</th>
<th>Talent</th>
<th>Speed</th>
<th>Health</th>
<th>Power</th>
<th>Spirit</th>
<th>Aura</th>
<th>Fp</th>
<th>Hp</th>
<th>Mp</th>
<th>Wp</th>
</tr>
<tr>
<td>Statistics</td>
<td>{{!!000Might}}</td>
<td>{{!!001Talent}}</td>
<td>{{!!002Speed}}</td>
<td>{{!!003Health}}</td>
<td>{{!!004Power}}</td>
<td>{{!!005Spirit}}</td>
<td>{{!!006Aura}}</td>
<td>{{!!007Fp}}</td>
<td>{{!!008Hp}}</td>
<td>{{!!009Mp}}</td>
<td>{{!!010Wp}}</td>
</tr>
<tr>
<td>Modifiers</td>
<td>{{!!000Might}}</td>
<td>{{!!001Talent}}</td>
<td>{{!!002Speed}}</td>
<td>{{!!003Health}}</td>
<td>{{!!004Power}}</td>
<td>{{!!005Spirit}}</td>
<td>{{!!006Aura}}</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</div>
Thanks in advance for any help.
buggyj
February 3, 2025, 4:27pm
2
something like
\define x(val) {{{[[-3]] [[-3]] [[-2]] [[-2]] [[-2]] [[-1]] [[-1]] [[-1]] [[-1]] +[zth{!!$val$}]}}}
<<x 000Might>>
Thanks, I did not know that operator.
But as the Stats can grow quite large, this isn’t a practical solution.
It is useful for something like the day of the week, with 7 entries.
This is a very clever approach! @TerryTigre , you’ll need to add = before any duplicate values, though; otherwise, they’ll be ignored, and zth won’t return the correct corresponding number.
\define x(val) {{{ [[-3]] =[[-3]] =[[-2]] =[[-2]] =[[-2]] =[[-1]] =[[-1]] =[[-1]] =[[-1]] +[zth{!!$val$}] }}}
If you’re using TW 5.3.0+, you could also use do this with functions rather than a macro:
\function mod.values() [[-3]] =[[-3]] =[[-2]] =[[-2]] =[[-2]] =[[-1]] =[[-1]] =[[-1]] =[[-1]]
\function .mod(field) [<currentTiddler>get<field>] :map[mod.values[]zth{!!title}]
<<.mod 000Might>>
This is obviously a little less concise than @buggyj ’s solution, since we can’t use $substitution$, but IMO functions have a few advantages:
less brittle than substitution macros (e.g., they won’t break if your parameter includes a $)
get fully evaluated before they’re substituted into your text, so your can use them as attributes/parameters in other widgets or procedures
return a single plain-text result by default, rather than the link(s) produced by a {{{ filtered transclusion }}}
Is there a mathematical pattern you can use to derive the correct modifier, or is it the kind of thing you have to look up in a table? If they’re arbitrarily assigned values, then @buggyj ’s approach is probably the best you can do.
It you can write a formula, though, it can be expressed in a function using math operators . For instance, I use the following function to display stat modifiers in D&D 5e:
\function .mod(stat) [<stat>subtract[10]divide[2]floor[]]
Here, the stat variable would represent the numerical ability score. My actual usage is a bit more complicated as it’s also accounting for stat-boosting items, but to simplify, I could use it like [.mod{!!ability-strength}], where the ability-strength field contains my raw strength score.
The table is below, but I could change it to just Quadratics, as it only changes for >256 in the current table.
These Main Stats all have modifiers. See table below.
table, th, td {border: 3px solid; color: blue; text-align: center;
}
Main Stat
Modifier
Stat Cards
Action Points
Damage Dice
0-1
-3
1
2
1
2-3
-2
2
3
D4
4-8
-1
3
4
D6
9-15
0
4
5
D8
16-24
1
5
6
D10
25-35
2
6
6
2D6
36-48
3
7
7
3D6
49-63
4
8
7
4D6
64-80
5
9
7
5D6
81-99
6
10
8
6D6
100-120
7
11
8
7D6
121-143
8
12
8
8D6
144-168
9
13
8
3D20
169-195
10
14
9
4D20
196-224
11
15
9
5D20
225-255
12
16
9
6D20
256-299
13
17
9
D50*3
300-399
14
18
10
D50*4
400-499
15
19
10
D50*5
500-599
16
20
10
D100*3
600-699
17
21
10
D100*4
700-799
18
22
11
D100*5
800-899
19
23
11
D100*6
900-999
20
24
11
D100*7
1000-1499
21
25
11
D100*8
1500+
22.max
26.max
12.max
D1000
So the Square root of Stat - 3 would give the modifier.
Try this out:
\function .mod(field) [<currentTiddler>get<field>power[0.5]subtract[3]floor[]]
<<.mod 000Might>>
I didn’t test it extensively against your chart; it’s possible round[] may be more appropriate than floor[].
Ok, I will test it, thanks. And if the char does not fit the results, I can change it, it is not set in stone yet (as you can see when the first numbers I gave did not match the chart…)
buggyj
February 3, 2025, 6:18pm
9
try this
\define y(val) {{{0 2 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 300 400 500 600 700 800 900 1000 1500 +[compare:integer:lteq{!!$val$}count[]subtract[4]]}}}
This solution worked even better as I could keep all the original table values.
This is a seriously powerful technique, and it will help a ton throughout all the rules.
Many thanks to you both!