How to make "complex" macro calculations?

150 views
Skip to first unread message

Alessandro Gianni

unread,
Mar 11, 2021, 7:03:35 AM3/11/21
to TiddlyWiki
Hi all. I'm in need of a macro (or something else?) that calculates a bunch of fields together in an expression. I'm trying to do with plugins like calc and playing around with variables and action widgets but nothing seems to work. In my last attempt I made a huge button full of actions that stored fields in a temp tiddler, with vars that recovered those fields to use in other action setfields... It was monstrous and still it didn't work.

I believe I need a macro - actually, probably a javascript one. But I don't know where to start; I don't know much about java and making macros in general. Can anyone point me to the best path I should follow, or help in any way?

The macro should:

from tiddler-1:
get field-a-1, field-a-2, field-b-1, field-b-2, field-n

from both tiddler-a and tiddler-b (which are stored as values in field-a-1 and field-b-1):
get field-x, so to get field-x-a and field-x-b to keep around for calcs.

these are all fields I manually set up.

then:

multiply field-x-a and field-a-2, and keep the result around (let's say var-a)
multiply field-x-b and field-b-2, the same (var-b)

add field-a-2 and field-b-2, to store as field-y in tiddler-1

multiply field-n and field-z, and get var-n

add together var-a, var-b and var-n, and store the result as field-z in tiddler-z


this is my last attempt (embed in a button). There's one more small calc, but it is just a sum that involves one field, so it's non relevant at the moment.
<$action-setfield $tiddler=":$/temp/HPCalc" $field="lev1" $value={{!!levello1}}/>
<$action-setfield $tiddler=":$/temp/HPCalc" $field="lev2" $value={{!!levello2}}/>
<$action-setfield $tiddler=":$/temp/HPCalc" $field="hd1" $value={{{ [title{!!class}get[hd]] }}}/>
<$action-setfield $tiddler=":$/temp/HPCalc" $field="hd2" $value={{{ [title{!!class}get[hd]] }}}/>
<$action-setfield $tiddler=":$/temp/HPCalc" $field="conmod" $value={{{ [<currentTiddler>get[modcon]] }}}/>

<$vars lev1={{{ [[$:/temp/HPCalc]get[lev1]] }}} lev2={{{ [[$:/temp/HPCalc]get[lev2]] }}} hd1={{{ [[$:/temp/HPCalc]get[hd1]] }}} hd2={{{ [[$:/temp/HPCalc]get[hd2]] }}}
conmod={{{ [[$:/temp/HPCalc]get[conmod]] }}}>

<$action-setfield $tiddler=":$/temp/HPCalc" $field="hps" $value={{{ [<hd1>multiply[2]subtract[2]] }}}/>
<$action-setfield $tiddler=":$/temp/HPCalc" $field="hp1" $value={{{ [<lev1>multiply<hd1>] }}}/>
<$action-setfield $tiddler=":$/temp/HPCalc" $field="hp2" $value={{{ [<lev2>multiply<hd2>] }}}/>
<$action-setfield $tiddler=":$/temp/HPCalc" $field="lev" $value={{{ [<lev1>add<lev2>] }}}/>

<$vars hps={{{ [[$:/temp/HPCalc]get[hps]] }}} hp1={{{ [[$:/temp/HPCalc]get[hp1]] }}} hp2={{{ [[$:/temp/HPCalc]get[hp2]] }}} lev={{{ [[$:/temp/HPCalc]get[lev]] }}}>

<$action-setfield $tiddler=":$/temp/HPCalc" $field="hpcon" $value={{{ [<conmod>multiply<lev>] }}}/>

<$vars hpcon={{{ [title[$:/temp/HPCalc]get[hpcon]] }}}>

<$action-setfield $tiddler=<<currentTiddler>> $field="hitpoints" $value={{{ [<hps>add<hp1>add<h2>add<hpcon>] }}}/>
<$action-setfield $tiddler=<<currentTiddler>> $field="level" $value={{{ [<lev>] }}}/>

</$vars>
</$vars>
</$vars>

if you played you know what, you know what this is about :P

Saq Imtiaz

unread,
Mar 11, 2021, 7:24:57 AM3/11/21
to TiddlyWiki
In the same block of action strings, you cannot set tiddler/field values and retrieve the updated values. Try saving the interim values as variables instead of in temp tiddlers.

PMario

unread,
Mar 11, 2021, 7:39:00 AM3/11/21
to TiddlyWiki
Hi,
-m

Saq Imtiaz

unread,
Mar 11, 2021, 8:09:58 AM3/11/21
to TiddlyWiki
Something like this:

<$vars lev1={{!!levello1}} lev2={{!!levello2}} hd1={{{ [title{!!class}get[hd]] }}} hd2={{{ [title{!!class}get[hd]] }}}
conmod={{{ [<currentTiddler>get[modcon]] }}}>

<$vars hps={{{ [<hd1>multiply[2]subtract[2]] }}} hp1={{{ [<lev1>multiply<hd1>] }}} hp2={{{ [<lev2>multiply<hd2>] }}} lev={{{ [<lev1>add<lev2>] }}}>

<$vars hpcon={{{ [<conmod>multiply<lev>] }}}>

<$vars hitpoints={{{ [<hps>add<hp1>add<h2>add<hpcon>] }}}>

<!-- further actions here -->

</$vars>
</$vars>
</$vars>
</$vars>

Only use action-setfield with the final values you wish to save.

Alessandro Gianni

unread,
Mar 11, 2021, 9:08:53 AM3/11/21
to TiddlyWiki
THIS. I don't know how to thank you. I actually tried something similar, but I probably try to calculate vars all in one line (like: <$vars a=2 b=3 c=a+b>) it obviously didn't work and I thought it just couldn't work with vars. Thanks again!

Alessandro Gianni

unread,
Mar 11, 2021, 9:08:55 AM3/11/21
to TiddlyWiki
This is an excellent plugin and I will use it a lot, thank you!

However, I don't understand how can I store a result calculated with this plugin inside a field.

Saq Imtiaz

unread,
Mar 11, 2021, 11:30:53 AM3/11/21
to TiddlyWiki
The key is that you cannot access the value of a variable, within the same vars widget that sets it.

So this:
 <$vars a=2 b=3 c=a+b>

Should be:

<$vars a=2 b=3>
<$vars c=a+b>

Alessandro Gianni

unread,
Mar 11, 2021, 1:50:58 PM3/11/21
to TiddlyWiki
While looking for solutions to my other problem (where you replied!), I found another solution to this by using =[field] =[field] +[sum[]] and +[product[]]. I didn't know how = and + worked, and I don't know if this is simpler - it has way less stratification of vars - but it works nonetheless.

<$action-setfield $tiddler=<<currentTiddler>> $field="level" $value={{{ =[<currentTiddler>get[level1]] =[<currentTiddler>get[level2]] +[sum[]] }}}/>

<$vars
hps={{{ [{!!class}get[hd]multiply[2]subtract[2]] }}}
hp1={{{ =[<currentTiddler>get[level1]] =[{!!class}get[hd]] +[product[]] }}}
hp2={{{ =[<currentTiddler>get[level2]] =[{!!class2}get[hd]] +[product[]] }}}
hocon={{{ =[<currentTiddler>get[modcon]] =[<currentTiddler>get[level]] +[product[]] }}}>

<$action-setfield $tiddler=<<currentTiddler>> $field="hitpoints" $value={{{ =[<hps>] =[<hp1>] =[<hp2>] =[<hpcon>] +[sum[]] }}}/>

</$vars>

Saq Imtiaz

unread,
Mar 11, 2021, 5:39:42 PM3/11/21
to TiddlyWiki
I just transposed your calculations exactly as they were, as I wasn't sure of the logic or which of the interim values you might need.

See https://tiddlywiki.com/#Filter%20Expression for an explanation on = and + prefixes for filter runs.

Alessandro Gianni

unread,
Mar 11, 2021, 5:53:06 PM3/11/21
to TiddlyWiki
Believe me, I've been looking at them for days, and only now it struck me how to use them. I'm still trying to understand if there's a formula to have only 2 action-setfield performing the whole operation (one for the first field and one for the second, that needs the first to compute). Thanks

Ste

unread,
Mar 19, 2021, 9:56:06 AM3/19/21
to TiddlyWiki
Evans formula plug in makes it a little less painful.
Reply all
Reply to author
Forward
0 new messages