0 votes
by (750 points)

Should be simple.

 

I am trying to get a variable $Name(displays a name of a NPC) to show up in a passage link.

 

Example:

[[Go to $Name]]

2 Answers

+1 vote
by (159k points)

Your question is slightly unclear, you state you want the value of the $Name variable to appear in the Link Text (the text displayed on the page), but your code example has that value in both the Link Text as well as in the Target Passage Name (the Passage shown if the link is selected).

eg. If the $Name variable equals "John" then you want the Link Text to read Go to John, and if the link is selected you want the Go to John Passage to be shown.

1. If you only want the Link Text to include the value of the $Name variable.

<<link `"Go to " + $Name` "Target Passage Name">><</link>>

2. If you want both the Link Text and the Target Passage Name to be the same.

<<link `"Go to " + $Name` `"Go to " + $Name`>><</link>>

note: Both of the above examples used the backquote expression described in the Macro Arguments documentation.

0 votes
by (68.6k points)
edited by

[EDIT]  Due to automatic passage creation in Twine 2, I suggest you use greyelf's answer.

While you may use a variable by itself within the link markup, if it's all you need, you need to use an expression when you want to combine some text and a variable.

If the link text and passage name will be the same, then you'll want to do something like the following:

[["Go to " + $Name]]

If they'll be different, then you'll want to do something like the following:

→ If the "Go to …" text is the link text
[["Go to " + $Name|Passage Name]]
[["Go to " + $Name->Passage Name]]
[[Passage Name<-"Go to " + $Name]]

→ If the "Go to …" text is the passage name
[[Link Text|"Go to " + $Name]]
[[Link Text->"Go to " + $Name]]
[["Go to " + $Name<-Link Text]]

SEE: The Markup > Links documentation.

PS: You may also use the <<link>> macro as shown by greyelf's answer.

by (159k points)

@Loliconman

You need to delete the automatically created "Go to " + $Name passage if you use any of the above mentioned markup based links that reference such a Target Passage.

Due to a limitation of the current Passage Map this will result in it incorrectly showing that you have a broken link even though you don't. This is one of the reasons I used the <<link>> macro instead, another is strange behaviour mentioned below.

 

@TheMadExile

If the automatically created "Go to " + $Name passage isn't deleted before using either Test or Play, or before running the HTML file generated by the Publish to File option then any of your example links that reference that "Go to " + $Name passage will actually show it and the first link example will literally have a link text of "Go to " + $Name

After the "Go to " + $Name passage is deleted all the relevant link examples behave correctly.

by (68.6k points)
edited by

Ah yes.  I don't use Twine 2 much anymore, so I forget about the automatic passage creation misfeature.  Mea culpa.

 

If the automatically created "Go to " + $Name passage isn't deleted before using either Test or Play, or before running the HTML file generated by the Publish to File option then any of your example links that reference that "Go to " + $Name passage will actually show it and the first link example will literally have a link text of "Go to " + $Name

 

After the "Go to " + $Name passage is deleted all the relevant link examples behave correctly.

That's exactly what should be happening in this case.  The text supplied to the link markup may be either plain text or a valid TwineScript expression.  The plain text case is tested first, so if a passage exists by the name '"Go to" + $Name', then the link will go to it.  Unfortunate, but entirely correct and proper.

The real issues here are that the automatic link creation feature:

  1. Is dead stupid.
  2. Doesn't ask the in-use story format for help in determining what should and should not be created as a passage.
  3. Cannot be cancelled during specific passage edits or turned off—either at the project or global level.
...