Issue with $text widget, and calculations from its results, inside a procedure

Hello all,

I have the created a procedure to calculate a bonus/malus from a skill stored as a key in a spellbook dictionary tiddler. The logic is a bit cumbersome:

  • skills have Easy, Average, Hard or Very Hard difficulties, stored for each of them in their difficulty field
  • the skillbook only references the name of its skills as keys, and the amount of cp (Character points) invested in them as values, in the form skill name:cp invested
  • Depending on their diffculty, the bonus/malus varies depending on them, but not linearly or geometrically

This bonus/malus follows the table below (Attribute is irrelevant for my current needs, I only need the bonus/males value, like “-3”, “-2”, “0”, “1”, “4”, etc.):

image

In any case, I reproduced the logic of the table in the procedure, but my <$text> widgets fail to return the expected calculation result, and seem to return a blank; any condition that uses a plain number (negatives in this case) works perfectly fine.

\procedure rel-lvl-value(skillbook, skill)
<% if [<skillbook>!has:index[<skill>]]
	(<<skill>> not in <<skillbook>>)
<% else %>
	<$let cp={{{ [<skillbook>getindex<skill>] }}} difficulty={{{ [<skill>get[difficulty]] }}}>
		<% if [<difficulty>match[VH]] %>
			<% if [<cp>compare:eq[1]] %>
				-3
			<% elseif [<cp>compare:eq[2]] %>
				-2
			<% else %>
				<$text text={{{ [[<cp>]divide[4]trunc[]substract[2]] }}} />
			<% endif %>
		<% elseif [[<skill>get[difficulty]]match[H] %>
			<% if [<cp>compare:eq[1]] %>
				-2
			<% elseif [<cp>compare:eq[2]] %>
				-1
			<% else %>
				<$text text={{{ [[<cp>]divide[4]trunc[]substract[1]] }}} />
			<% endif %>
		<% elseif [[<skill>get[difficulty]]match[A] %>
			<% if [<cp>compare:eq[1]] %>
				-1
			<% elseif [<cp>compare:eq[2]] %>
				0
			<% else %>
				<$text text={{{ [[<cp>]divide[4]trunc[]] }}} />
			<% endif %>
		<% elseif [[<skill>get[difficulty]]match[E] %>
			<% if [<cp>compare:eq[1]] %>
				0
			<% elseif [<cp>compare:eq[2]] %>
				1
			<% else %>
				<$text text={{{ [[<cp>]divide[4]trunc[]add[1]] }}} />
			<% endif %>
		<% else %>
			(Error: <<skill>> difficulty field doesn't match VH, H, A or E)
	</$let>
<% endif %>
\end

I’d think I have a syntax issue in the text parameter of the <$text> widgets, but I can’t figure it out becaause it has worked in other cases (although they where macros and not procedures, perhaps). Is the mistake I’m making obvious to anybody?

Remove the extra square brackets around <cp>, like this:
<$text text={{{ [<cp>divide[4]trunc[]substract[2]] }}} />

-e

This is how I had written it originally, but it doesn’t work either…

You have a lot of extra/missing brackets; I imagine you copy-pasted a couple of initial errors. In addition to the ones Eric pointed out:

  • <% if [<skillbook>!has:index[<skill>]] → <% if [<skillbook>!has:index<skill>] %> (Also note the missing %>, which I suspect was the single largest issue here.)

  • <% elseif [[<skill>get[difficulty]]match[H] %> → <% elseif [<skill>get[difficulty]match[H]] %>

  • <% elseif [[<skill>get[difficulty]]match[A] %> → <% elseif [<skill>get[difficulty]match[A]] %>

  • <% elseif [[<skill>get[difficulty]]match[E] %> → <% elseif [<skill>get[difficulty]match[E]] %>

And since you’ve defined <<difficulty>> as {{{ [<skill>get[difficulty]] }}}, I’d go ahead and replace each instance of <skill>get[difficulty] with <difficulty>.

1 Like

There’s also a typo in the subtract filter operator, notice there’s only a single s

<$text text={{{ [<cp>divide[4]trunc[]subtract[2]] }}} />

I guess it’s a French’ thing, I made the same mistake a few times… :wink:

Fred

I’m ashamed, I did indeed copy/paste a wrong source :stuck_out_tongue_winking_eye: Thanks for pointing it out.

Wow! This is what eluded me all this time, good catch! I would have NEVER found it without you, I have been conviced that it was “substract” for perhaps 25 years of otherwise fluent english - learn everyday they say! Merci mille fois :slight_smile:

2 Likes

I fixed all that as follows:

\procedure rel-lvl(skillbook, skill)
<% if [<skillbook>!has:index<skill>] %>
	(<<skill>> not in <<skillbook>>)
<% else %>
	<$let cp={{{ [<skillbook>getindex<skill>] }}} difficulty={{{ [<skill>get[difficulty]] }}} attribute={{{ [<skill>get[attribute]] }}}>
		<% if [<difficulty>match[VH]] %>
			<% if [<cp>compare:eq[1]] %>
				<<attribute>>-3
			<% elseif [<cp>compare:eq[2]] %>
				<<attribute>>-2
			<% elseif [<cp>compare:eq[3]] %>
				<<attribute>>-2
			<% elseif [<cp>compare:eq[4]] %>
				<<attribute>>-1
			<% elseif [<cp>compare:lt[8]] %>
				<<attribute>>-1
			<% else %>
				<<attribute>>+<$text text={{{ [<cp>divide[4]trunc[]subtract[2]] }}} />
			<% endif %>
		<% elseif [<difficulty>match[H]] %>
			<% if [<cp>compare:eq[1]] %>
				<<attribute>>-2
			<% elseif [<cp>compare:eq[2]] %>
				<<attribute>>-1
			<% elseif [<cp>compare:eq[3]] %>
				<<attribute>>-1
			<% else %>
				<<attribute>>+<$text text={{{ [<cp>divide[4]trunc[]subtract[1]] }}} />
			<% endif %>
		<% elseif [<difficulty>match[A]] %>
			<% if [<cp>compare:eq[1]] %>
				<<attribute>>-1
			<% elseif [<cp>compare:eq[2]] %>
				<<attribute>>+0
			<% else %>
				<<attribute>>+<$text text={{{ [<cp>divide[4]trunc[]] }}} />
			<% endif %>
		<% elseif [<difficulty>match[E]] %>
			<% if [<cp>compare:eq[1]] %>
				<<attribute>>+0
			<% elseif [<cp>compare:eq[2]] %>
				<<attribute>>+1
			<% else %>
				<<attribute>>+<$text text={{{ [<cp>divide[4]trunc[]add[1]] }}} />
			<% endif %>
		<% else %>
			(Error: <<skill>> difficulty field doesn't match VH, H, A or E)
	</$let>
<% endif %>
\end

And I now only have one case that behaves oddly:
For a VH skill, when <cp> is between 5 and 7 (inclusive), it doesn’t enter the <% elseif [<cp>compare:lt[8]] %>, and goes on directly in the <% else %>… (I had to add this condition to avoid have a “+” when the result is actually negative)

The compare filter operator has two parts to the suffix: type and mode, like this:

compare:type:mode[value]

For your purposes, the type can be either number (the default) or integer.

BUT… you have completely omitted this type suffix… which is not a valid use of the operator.

You need to write:
[<cp>compare:number:eq[1]]
or
[<cp>compare::eq[1]]
or
[<cp>compare:integer:eq[1]]

Also, instead of using [<cp>compare::eq[...]], you could just use the simpler [<cp>match[...]] syntax to achieve the same result. Of course, you will still need to use [<cp>compare::lt[8]] for the “less than” filter.

-e

1 Like

Got it. I definitely need to sleep, this whole thing is a testament to my lack of focus, aside from the word “subtract”, I could have avoided all those mistakes… Thanks!

4 posts were split to a new topic: Simple Table Handling