Howdy, Stranger!

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

Can i 'replace' a link with another link?

I'm trying to make something where students can match a word and its definition. I have it so that they can click on a word (= a link) and this makes the word appear somewhere else. The idea is that after that they will be able click on the right definition. But I want to give them the option to 'take back' the word, because maybe the want to start with another one or made a mistake.

The problem is finding a way to make the word appear in its initial place. I'm not sure how i can tell Twine which span (the words are in spans) to target.

This is my code so far:

<<set $word = [ "First word", "Second word" ]>>

<span id="choice"><<print " ">></span>
<<nobr>>
<<link "take back">>
<<replace "#choice">>
<<print " ">>
<</replace>>
<<replace "#word0">>
<<link "$word[0]">>
<<replace "#word0">>
<<print " ">>
<</replace>>
<<replace "#choice">>
<<print "$word[0]">>
<</replace>>
<</link>>
<</replace>>
<</link>>
<</nobr>>

<div id="word">
<span id="word0"><<link "$word[0]">><<replace "#word0">><<print " ">><</replace>><<replace "#choice">><<print "$word[0]">><</replace>><</link>><<set $temp = 0>></span>
<span id="word1"><<link "$word[1]">><<replace "#word1">><<print " ">><</replace>><<replace "#choice">><<print "$word[1]">><</replace>><</link>><<set $temp = 1>></span>

Can you help me?

I thought i could solve the problem by adding if statements. I tried saying that <<if $temp === 0>> the link would replace the word0 span with the original word (word0) and if $temp === 1 it would replace the word1 span with word1 etc, but that produced an error (child tag <</link>> was found outside of a call to its parents macro <<link>>.

I also tried substitution the 0 in $word[0] with $temp. That worked, but i can't replace the 0 in #word0 by a variable.

Comments

  • I'm using the latest version of sugarcube by the way.
  • Perhaps i should rephrase my question.

    I want to make a lot of links (the words). The player clicks one of them and it makes another link appear (its set up to be matched to a description of the word). There is also a third link (kind of a reset button). If the player clicks that, the new link (set up) disappears again AND it makes the original link (word among all the other words) reappear.

    The problem is that i know how to make A link reappear, but not how to target a specific span so that i can make THE ORIGINAL link reappear out of all of the possible links the player could have clicked.
  • edited May 2017
    If I understand correctly, you want something like this:
    <<for _i to 0; _i lt $word.length; _i++>>
    	<<if $word[_i] != $choice>>
    		<<capture _i>>
    		[[$word[_i]|passagename][$choice = $word[_i]]]
    		<</capture>>
    	<</if>>
    <</for>>
    
    <<if $choice != "">>
    [[$choice]] - [[Take Back|passagename][$choice = ""]]
    <</if>>
    
Sign In or Register to comment.