Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Referencing id elements from within the macro.

Hi, I'm attempting to abstract the following out into a widget:

<<set $PC_AGE = 18>>
<span id="pc-age-stat"><<print $PC_AGE>></span>
<<click "[+]">>
<<if $PC_AGE != $PC_MAX_AGE>>
<<set $PC_AGE++>>
<<replace "#pc-age-stat">>
<<print $PC_AGE>>
<</replace>>
<</if>>
<</click>>
<<click "[-]">>
<<if $PC_AGE != $PC_MIN_AGE>>
<<set $PC_AGE-->>
<<replace "#pc-age-stat">>
<<print $PC_AGE>>
<</replace>>
<</if>>
<</click>>

Here is what I've managed to come up with:

<<widget numberselect>>
<span id=$args[1]><<print $args[0]>></span>\
<<click "[+]">>\
<<if $args[0] != $args[2]>>\
<<set $args[0]++>>\
<<replace "$args[1]">>\
<<print $args[0]>>\
<</replace>>\
<</if>>\
<</click>>\
<<click "[-]">>\
<<if $args[0] != $args[3]>>\
<<set $args[0]-->>\
<<replace "$args[1]">>\
<<print $args[0]>>\
<</replace>>\
<</if>>\
<</click>>\
<</widget>>

If you try and run this, you'll get the correct output: 25 [+] [-], with the plus and minus clickable. However, clicking on the plus and minus throws and error:

Error: Uncaught Error: cannot execute macro <<replace>>: Syntax error, unrecognzed expression $args[1].

I've tried a few things:
<<replace "#$args[1]">>, <<replace "#" + $args[1]>>, etc -- I've even changed it so that the input would be the "#id". However, none of these have worked. Is there a way to make this work?

Comments

  • edited February 2016
    You don't state which version of SugarCube you are using, so I am going to assume it is 1.0.34 which comes built-in to Twine 2.
    You also didn't include an example of the usage of the numberselect macro, base on your widget code I am going to assume it is something like the following:
    <<set $PC_AGE = 18>>
    
    <<numberselect "$PC_AGE" "pc-age-stat" 20 10>>
    

    Converting your first example into a widget is a little more complex than you thought, because you want to insert the id of an element, the name of a variable and the min/max values into the click handlers.
    The following is one way to do what you asked, it uses <<print>> macros to create the click macro handlers:
    <<widget "numberselect">>
    \
    <<print '<span id="' + $args[1] + '"><<print $args[0]>></span>'>>\
    \
    <<print '<<click "[+]">><<if ' + $args[0] + ' != ' + $args[2] + '>><<set ' + $args[0] + '++>><<replace "#' + $args[1] + '">><<print ' + $args[0] + '>><</replace>><</if>><</click>>'>>\
    \
    <<print '<<click "[-]">><<if ' + $args[0] + ' != ' + $args[3] + '>><<set ' + $args[0] + '-->><<replace "#' + $args[1] + '">><<print ' + $args[0] + '>><</replace>><</if>><</click>>'>>\
    \
    <</widget>>
    
  • edited February 2016
    Thank you, that was the question, and answer, I was looking for. Apologies for not proof-read before hand.
Sign In or Register to comment.