Howdy, Stranger!

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

Max value for a variable

Heey again,

I feel like I'm a regular in this forum now...

Anyway, I'm wondering if it's possible to establish a maximum value to a variable.
I'm using Twine 2 and Sugarcube 2.18.

My idea is to create a variable to describe a relationship status between two characters. I was thinking of something from 0 to 10, where 0 was the worse the relationship could be at, and 10 the best. And I was thinking throughout the story, depending on the choices made, to simply use something like <<set $relationship ++ >>. But I didn't want the value to ever surpass the 10. How do I do that without having to always go check if the variable is at 10 yet??

I don't know if I was really clear, but I'd appreciate any help :)


  • edited June 2017
    Easiest way? Use an <<if>> macro to prevent the number from going over a certain value:
    <<set $relationship++>>
    <<if $relationship gte 10>>
        <<set $relationship to 10>>

    Some more advanced methods.

    1. Make a widget (goes in a widget-tagged passage).
    <<widget 'relationshipUp'>>\
            <<if $args[0]>>
                <<set _incr to $args[0]>>
                <<set _incr to 1>>
            <<set $relationship += _incr>>
            <<if $relationship gte 10>>
                <<set $relationship to 10>>

    /% increases $relationship by 1 %/
    <<relationshipUp 3>>
    /% increases $relationship by 3 %/

    2. Use the ? operator, only works really well with adding 1 point at a time.
    <<set $relationship to ($relationship gte 10) ? 10 : $relationship++>>

    3. Use a special passage like PassageFooter or PassageDone, etc. YMMV, as the variable will be over 10 for some of the passage's code.
    ::some passage
    <<set $relationship++>>
    <<if $relationship gte 10>>
        <<set $relationship to 10>>

    There's a few other ways too, like the <number>.clamp() method and the Math.clamp() method, but I think one of these is probably easiest to implement.
  • Hey Chapel! Thanks for your help. Obviously the <<if>> option is the easiest, it's the one I had imagined before, but I had no idea abut the other ones, so thanks :) Do you mind just explaining, (newbie here :/), what you meant by a widget-tagged passage? I don't understand where exactly I would put the code... thanks again
  • edited June 2017
    Jajajewels wrote: »
    ...what you meant by a widget-tagged passage? I don't understand where exactly I would put the code...

    You can add tags to passages in the editor. Adding the tag 'widget' will register the passage as widget definitions. Widgets are like macros, but are defined in TwineScript (instead of JavaScript). To add a tag, click on the +Tag option, type in the tag, and click Add. I posted images for reference.

  • Ooops, sorry for the dumb question. I didn't realize that's how you applied widgets (I'd heard of them, but I haven't started using them. Thank you!!
  • edited June 2017
    @Jajajewels: The WARNING within the <<widget>> macro's documentation mentions the need to use a widget passage tag when defining widgets, reading the manual can be helpful.
  • Personally, I'd simply use Math.clamp() during the assignment—there's nothing particularly difficult about its use. For example, using it with the addition operator:
    <<set $relationship to Math.clamp($relationship + 1, 0, 10)>>
    Alternatively, with the prefix increment operator: (do not use the postfix increment operator here)
    <<set $relationship to Math.clamp(++$relationship, 0, 10)>>
Sign In or Register to comment.