0 votes
by (2.7k points)

Here a simplified version of the code (the actual version has 16 of such buttons, each with a different variable as their name).

<<button $Variable>>Content<</button>>

It is important that some are negative and some are positive. The buttons are arranged in a 16X16 square, and are meant to line up (the minus signs stop this happening.) Each variable is a random number between -3 and 2. How to add a "+" sign to each number that is positive or zero?

1 Answer

0 votes
by (159k points)
selected by
 
Best answer

Please use the Question Tags to state the name and full version number of the story format you are using, as answers can vary based on this information. Based on the syntax used in your example I will assume you are using SugarCube 2.21.0 (or later)

You could us that story format's <<widget>> macro to create a custom <<pad>> macro which pads each value with the correct character. The following example goes within your story's widget tagged special passage.

<<widget "pad">>
	\<<if $args[0] lt 0>>
		\<<print "&minus;" + Math.abs($args[0])>>
	\<<else>>	
		\<<print "&#43;" + $args[0]>>
	\<</if>>
\<</widget>>

... it uses HTML character escaping (like that found here) to represent the plus and minus characters, and for consistency sake it used the Javascript Math.abs() function to remove the natural minus sign from negative numbers.

You can use the custom <<pad>> macro like so.

<<set $valueA to -3>>
<<set $valueB to 0>>
<<set $valueC to 2>>

<<pad $valueA>>
<<pad $valueB>>
<<pad $valueC>>

warning: The above <<pad>> macro example does not contain any error catching for conditions such as when no value is passed to the macro, of if a String is passed instead of a number.

...