Howdy, Stranger!

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

Dynamic Linking

edited March 19 in Workshop
Pre-face: I'm torn choosing between workshop and chit-chat. Is there any rules regarding on what is for what?
Pre-face 2: This is also not a question. Just a brain dump and acts like a note for me. People are free to jump in with better ideas and solutions though.

I'm trying to make a sort of dynamic link passage. This passage will change it's link details to whatever $variable it is passed to. I've tried using the following:
[[Link Text|$variable]]
[[Link Text|<<$variable>>]]
[[Link Text|<<print $variable]]
The all didn't work and created new passages which is not what I do not want it to do.

I poked around the Twinery wiki and found the link documentation via sitemap. There's an example there:
<<set $actor to "Ginger">>
<<if $location[$actor] is passage()>>
<<print "[[Talk to " + $actor + "|Talk_" + $actor + "][$chat to '" + $actor + "']]">>
<<endif>>
And I thought, maybe it might work. Then it did. The below code now makes the link in the passage act dynamically.
<<print "[[Link Text|" + $variable + "]]">>
It still made a passage but when I played the passage with the dynamic link, it took me where I want to go. I deleted the created passage and it still works. I just don't know how to remove the flow chart line, but I guess that's ok for now.


Other notes:
Twine Version: 2.1.0
Current Story Format: SugarCube 2.16

Other note to me: You big dummy read the title of the paragraph of the documentation next time.

Comments

  • As per sugarcube's documentation, the format
    [[link text|$variable]]
    

    Should work, without needing to do the print trick. It will create a passage that you'll need to delete on Twine 2.x, however.

    When you tried this, did it just not work at all?
  • edited March 19
    Chapel wrote: »
    As per sugarcube's documentation, the format
    [[link text|$variable]]
    

    Should work, without needing to do the print trick. It will create a passage that you'll need to delete on Twine 2.x, however.

    When you tried this, did it just not work at all?
    I did not work at all when I was doing it.

    I just tried this just now, created a new story for it and checked the story format. It now works as you suggested.

    Edit: The current story format of the first post is sugarcube 2.12.
    (I thought twine changed it to 2.16...)
  • edited March 19
    mega01man wrote: »
    I did not work at all when I was doing it.
    Then something was wrong somewhere, as using a variable for the link component has always worked in SugarCube v2—and most versions of v1 for that matter.

    I couldn't say what your issue was. At a guess, however, you probably weren't defining your variable properly, misspelled it, or didn't delete the passage automatically created by Twine 2. I'll further guess that the latter is probably the culprit.

    To explain why the latter could cause issues. As noted within the documentation, the link component may either be plain text or any valid TwineScript expression. SugarCube checks them in that order as well, meaning it checks to see if a passage by the name exists before it attempts to evaluate the link as code. Thus, if you have a passage named $variable, SugarCube assumes that the link is supposed to go to that passage. Basically, it prioritizes treating the link component as a passage over as code.

    The reason why the Stupid Print Trick™ worked in this case, is because it forced the variable to be evaluated as part of the <<print>> rather than as part of link markup itself. In other words, the following code:
    <<print "[[Link Text|" + $variable + "]]">>
    
    Produces the following link: (assume $variable has the value "Over There")
    [[Link Text|Over There]]
    
  • thank you for answering @TheMadExile . I think the problem is my version of twine 2.1 keeps reverting back the story's format that's why
    [[Link Text|$variable]]
    
    did not work.

    I guess I'm stuck using the Stupid trick at least for now.


  • I just explained that the version of SugarCube has, literally, nothing to do with why the link did not work as expected and, furthermore, outlined the most likely reasons why it didn't. Let me be clear, it wouldn't matter if Twine 2.1 reverted you to SugarCube v2.0.0—it can't, but it wouldn't matter if it could—the version is irrelevant.

    Beyond that, rather than using the Stupid Print Trick™, just use the separate argument version of the <<link>> macro. For example:
    <<link "Link Text" $variable>><</link>>
    
  • I just explained that the version of SugarCube has, literally, nothing to do with why the link did not work as expected and, furthermore, outlined the most likely reasons why it didn't. Let me be clear, it wouldn't matter if Twine 2.1 reverted you to SugarCube v2.0.0—it can't, but it wouldn't matter if it could—the version is irrelevant.

    Beyond that, rather than using the Stupid Print Trick™, just use the separate argument version of the <<link>> macro. For example:
    <<link "Link Text" $variable>><</link>>
    
    Ok.

    And thank you for the suggestion! I haven't thought of that. It sounds that the
    <<print ...>>
    
    is bad practice? Why is it?
Sign In or Register to comment.