Help with adding variables together?

So I’m attempting to make a character sheet that uses variables to calculate their stats.

I’ll add some pseudo code to try and convey what it is I want to do.

<$vars
strength="2"
endurance="3"
dexterity="1"
intelligence="[<int-logic>add<in-creative>add<int-social>]"
  int-logic="2"
  int-creative="3"
  int-social="1"
total="[<strength>add<endurance>add<dexterity>add<inteligence>]"
>

Character Level is <<total>>.

</$vars>

Is this possible, or should I do this a different way?

I read that the $let widget allows variables to reference eachother, but wasn’t sure how to use it, or if it would allow me to do what I’m aiming to.

The $let widget does allow reference to other variables that are defined in the same $let, but the order does matter. If you want to reference a variable, it has to be defined BEFORE it is referenced.

Also, to perform calculations using filter syntax, you will need to use the tripled curly braces (for filtered transclusion) instead of quotes (which are for literal text)

Something like this:

<$let strength="2" endurance="3" dexterity="1"
   int-logic="2" int-creative="3" int-social="1"
   intelligence={{{ [<int-logic>add<int-creative>add<int-social>] }}}
   total={{{ [<strength>add<endurance>add<dexterity>add<intelligence>] }}}
>
Character Level is <<total>>.
</$let>
1 Like

Oooh, Ok now I understand, thank you. I’ve been using triple curly brackets every once and awhile but never really knew what they were called or what their exact use was. I was using the text widget to print out the numbers before, but this works way better haha

A bit off topic but still related, I was tinkering with the ability to use fields to hold values for the variables, and wanted to know if something like this would be possible.

title: character 1
strength: 2
title: character 2
strength: 4
<$let strength={{{ [prefix[character]get:field[strength]add{!!strength}] }}}>
Character strength is <<strength>>
</$let>

I’m trying to have the filter add all of the strength field values of tiddlers that are prefixed with character.

The reason for this is keeping track of the same character in a series, but their different stats in different stories, like how a person grows in life and changes, just for reference.


Edit: I think I’ll just take the easier route and set each variable as the strength field value of the tiddlers and then add em all up that way :sweat_smile:

Give this a try:

<$let strength_count={{{ [prefix[character]has[strength]count[]] }}}
      total_strength={{{ [prefix[character]get[strength]sum[]] }}}
      avg_strength={{{ [<total_strength>divide<strength_count>] }}}>
Total character strength is <<total_strength>><br>
Average character strength is <<avg_strength>>
</$let>
1 Like

Ah ok, I was thinking count would just give the number of tiddlers with that field, but was mistaken :sweat_smile:

Edit: If you wouldn’t mind explaining, why do you use get when using sum, but has when using count?

  • {{{ [prefix[character]has[strength]count[]] }}}
    is the number of tiddlers whose title starts with “character” and has a non-blank strength field. The value of each strength field doesn’t matter (as long as it is non-blank).
  • {{{ [prefix[character]get[strength]sum[]] }}}
    is the sum of the non-blank strength field values for all tiddlers whose title starts with “character”. We need to use get[strength] to actually fetch the field values so we can add them up to calculate the total value.

@EricShulman, So using the has operator is better if a tiddler might have an empty field? I noticed you can use get for both, but only use has in conjunction with count.
I’m reading the documentation for it at TiddlyWiki.com but the wording is a little confusing for me, as far as figuring out the differences and ideal use-cases.

has[strength]count[] and get[strength]count[] will both look for non-blank fields, so for this particular use-case you could use either.

However, if you wanted to check for the existence of the strength field, even if it is blank, then you need to use has:field[strength]count[] (note that the field suffix is a literal keyword, not a reference to a field name).

Another difference: [prefix[character]has[strength]] results in a list of tiddler titles, while [prefix[character]get[strength]] results in a list of strength values.