Howdy, Stranger!

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

Variable as widget argument? (Sugarcube 2)

LuxLux
edited January 2017 in Help! with 1.x
So I don't know if this can be done: I'd like to make a widget that sets a variable to a particular value, and lets me choose which variable every time I use it. But I can't do:
<<widget mywidget>><<set $args[0] to "something complicated">><<endwidget>>
and then
<<mywidget $specificvar>>
Because that would just pass the current value of $specificvar to the widget, which obviously wouldn't do anything. Is there some way to work around this?

Comments

  • If I understand you correctly, the Sugarcube 2 <<switch>>-macro might solve your problem, e.g.
    <<widget mywidget>>
        <<set $varToSet to $args[0]>>
        <<switch $varToSet>>
            <<case "red" >>
                 <<set $red to 10>>
            <<case "blue" >>
                 <<set $blue to 10>>
        <</switch>>
    <<endwidget>>
    

    (not tested, just a suggestion)
  • Hmm, but that would still require adding an additional switch case for each possible variable I want to use it with, right?

    In that case, I could also just make several widgets with slightly different names, which is my current workaround...
  • First of all, you're not passing the variable properly. In SugarCube, there are a few ways to reference a variable in macros so that it may be modified, however, they all start with quoting the variable when you pass it in (just as you must with <<textbox>> for example). The reason you must quote the variable is found at the top of the macro library documentation (see: Passing a variable's name as an argument), to bypass automatic story variable substitution.

    Beyond that, you need to use the Stupid Print Trick™ to render a <<set>> with the name of the passed in variable on the left-hand side of the assignment expression.

    Try something like the following:
    <<widget "mywidget">><<print '<<set ' + $args[0] + ' to "something complicated">>'>><</widget>>
    
  • LuxLux
    edited January 2017
    I can't quite wrap my head around how that print trick works, but that did exactly what I needed. Thanks!

    EDIT: Actually I think I do now, the different types of quote marks confused me at first.
  • As an explanation. Many of SugarCube's macros and other sections of the parser are pervasively recursive, so the <<set>>, rendered by the invocation of the <<print>>, is itself parsed.

    For example, assuming an invocation of the widget like:
    <<mywidget "$foo">>
    
    Yields a <<set>>, which is parsed, like the following:
    <<set $foo to "something complicated">>
    
  • richVIE wrote: »
    <<widget mywidget>>
        <<set $varToSet to $args[0]>>
        <<switch $varToSet>>
            <<case "red" >>
                 <<set $red to 10>>
            <<case "blue" >>
                 <<set $blue to 10>>
        <</switch>>
    <<endwidget>>
    
    I believe we're done here, however, some commentary on your solution.
    1. If you're going to use a variable for what certainly seems to be a temporary use, then you should use an actual temporary variable—i.e. _varToSet instead of $varToSet—to keep the story history clean.
    2. An additional intermediate variable isn't necessary here in the first place—i.e. <<switch $args[0]>> is sufficient.
Sign In or Register to comment.