Howdy, Stranger!

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

Print variables inside links (SugarCube)

Hi there,

I'm using Twine 2 with SugarCube 1.0.29 and I'm trying to print variables along with some text on a few specific links. Is this possible?

If I write $friend_1 it works, but if I change it to You choose $friend_1 instead of $friend_2 it does not recognise "$friend_1" and "$friend_2" as variables and outputs the entire sentence literally. I've also tried "<<print $friend_1>>" inside a link but it still doesn't render it as a variable.

Is there a way around this?

Thank you ;)

Comments

  • Wiki link components are treated as either plain text or TwineScript expressions. Try something like the following:
    [["You choose " + $friend_1 + " instead of " + $friend_2]]
    
  • Wiki link components are treated as either plain text or TwineScript expressions. Try something like the following:
    [["You choose " + $friend_1 + " instead of " + $friend_2]]
    

    @TheMadExile Thanks! Same outcome though, it printed the link verbatim (quotation marks and plus signs included)?
  • That's should be impossible if your project is actually set to use SugarCube (you might want to check that). Are you copy-pasting from a word processor or something?
  • SugarCube it is! It works now, I was setting the vars and using them on the same passage:
    /* Test */
    
    <<set $friend_1 to "Mike">> <<set $friend_2 to "John">>
    
    [["You choose " + $friend_1 + " instead of " + $friend_2]]
    

    I set them on the previous one and that did the job, thank you!

    Could I trouble you with one last question? If I wanted to print user input variables in uppercase in those links, would that be possible? In HTML I can simply use a "<span>", but it doesn't seem to be the case here?

    Thank you for sharing your valuable time, @TheMadExile ;)
  • As long as they're set before you use them it should be fine. So, what you have there should work. Even if they weren't defined, you'd have gotten a broken link (along the lines of: You choose undefined instead of undefined) instead of what you described. Whatever. As long as it's working, I suppose.

    As far as uppercasing the $variables, unfortunately no, you cannot use markup inside the link (again, they're either plain text or a TwineScript expression). You can, however, make use of the string method toLocaleUpperCase(). For example:
    [["You choose " + $friend_1.toLocaleUpperCase() + " instead of " + $friend_2.toLocaleUpperCase()]]
    

    If you wanted to uppercase the link text only, leaving the passage as-is, then it would be like the following:
    [["You choose " + $friend_1.toLocaleUpperCase() + " instead of " + $friend_2.toLocaleUpperCase()|"You choose " + $friend_1 + " instead of " + $friend_2]]
    
  • The ".toLocaleUpperCase()" string method does exactly what I need!

    Thank you for your time, @TheMadExile , and thank you for SugarCube! :)
Sign In or Register to comment.