0 votes
by (120 points)
I use sugarcube 2 and I am making  a code  to simulate story telling dice, dice that roll success and advantages that generate more than simple "pass / fail" outcomes.

But I'm getting a "Error: cannot find a closing tag for macro <<for>>"  for code that clearly has a <</for>> macro in it D: What am I doing wrong here?

<<set $herodice to ["Miss", "X", "X", "+", "+", "X+", "++", "XX"]>>

<<set($swift to 2)>>

<<for _h 0; _h =$swift; _h++>>\
    <<set _roll to $herodice.random()>>\
    <<print "You roll: _roll">>
        <<if _roll is "X">>\
            <<set _success to +1>>\
        <<elseif _roll is "+">>\
            <<set _boost to +1>>\
        <<elseif _roll is "X+">>\
            <<set _success to +1>>\
            <<set _boost to +1>>\
        <<elseif _roll is "++">>\
            <<set _boost to +2>>\
        <<elseif _roll is "XX">>\
            <<set _success to 2>>\
        <</if>\
<</for>>

 

The purpose of the code is to simulate story telling dice, dice that roll success and advantages that generate more than simple "pass / fail" outcomes.

1 Answer

+1 vote
by (44.7k points)
edited by

The error is caused by the "<</if>\" missing a second ">".

Another problem is that your <<for>> line isn't quite right.  This line:

<<for _h 0; _h =$swift; _h++>>

should be like this instead:

<<for _h = 0; _h < $swift; _h++>>

That assumes that you want 2 dice rolls if $swift is set to 2.

Also, a <<switch>> macro would be better here than a bunch of "<<if>>....<<elseif>>" lines:

<<nobr>>
	<<set $herodice to ["Miss", "X", "X", "+", "+", "X+", "++", "XX"]>>
	<<set $swift = 2>>
	<<set _success = 0>>
	<<set _boost = 0>>
	<<for _h = 0; _h < $swift; _h++>>
		<<set _roll to $herodice.random()>>
		You roll: _roll<br>
		<<switch _roll>>
			<<case "X">>
				<<set _success += 1>>
			<<case "XX">>
				<<set _success += 2>>
			<<case "+">>
				<<set _boost += 1>>
			<<case "++">>
				<<set _boost += 2>>
			<<case "X+">>
				<<set _success += 1>>
				<<set _boost += 1>>
		<</switch>>
	<</for>>
<</nobr>>

That also uses <<nobr>> so you don't need "\" everywhere (the "<br>" forces a linebreak), initializes _success and _boost to 0, and adds to their values instead of just setting them.  Also, you don't need the <<print>> macro, since SugarCube handles displaying "naked variables" automatically (as long as they aren't too complex).

Hope that helps!  :-)

by (120 points)
My mind is blown D:

Thank you! This helps so much!
by (23.6k points)
edited by

Or you could skip the <<if>>/<<switch>> entirely:

<<set $herodice to [[0,0,"Miss"], [1,0,"X"], [1,0,"X"], [0,1,"+"], [0,1,"+"], [1,1,"X+"], [2,0,"XX"], [0,2,"++"]]>>

<<set $swift to 2>>
<<set _success = 0>>
<<set _boost = 0>>

<<nobr>>
<<for _h 0; _h =$swift; _h++>>
    <<set _roll to $herodice.random()>>
    <<print "You roll: _roll[2]">>
    <<set _success += _roll[0]>>
    <<set _boost += _roll[1]>>
<</for>>
<</nobr>>

 

...