Howdy, Stranger!

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

Using links to replace text and set variables?

edited June 2015 in Help! with 2.0
Sorry if this has been asked before - I couldn't find anything that quite fit the bill.

I'm fairly new to Twine, and I'm trying to use a link to replace some text depending on which of two links the player clicks, and at the same time to set a variable value.

The code I'm using is:
|TheGun>[(link: "Take the gun")[(set: $Gun to 1)(replace: ?TheGun)[You take the gun and put it in the pocket of your jacket.]] or (link: "refuse it")[(replace: ?TheGun)[You refuse to take the gun and hand it back.]]]

Which changes the text just fine, but doesn't seem to set the variable.

Any ideas

Thanks in advance

Comments

  • Your example is setting the $Gun variable, what makes you think it isn't?

    note: it is a good idea to initialize all your $variables at the start of your story before you use then, don't rely on the story format automatically doing it for you.

    So if I take your example and add a markup link to a new passage named Next:
    (set: $Gun to 0)
    
    |TheGun>[(link: "Take the gun")[(set: $Gun to 1)(replace: ?TheGun)[You take the gun and put it in the pocket of your jacket.]] or (link: "refuse it")[(replace: ?TheGun)[You refuse to take the gun and hand it back.]]]
    
    [[Next]]
    
    ... and within the new Next passage I add the following test:
    Gun equals $Gun
    
    .. and then test the above by either clicking on your "Take the gun" before clicking on the "Next" link, or clicking on your "refuse it" link before clicking on the "Next" link, I get the following two results.
    Gun equals 1

    Gun equals 0
  • Hi greyelf,

    thanks for your thoughts.

    The reason I thought the $Gun variable wasn't being set is that when I put some test code similar to yours just after the code to link and replace, it still showed 0 whatever choice was clicked. Sure enough when I test it in a different passage it gives the expected answer.

    Perhaps it's just me, but that feels rather counter-intuitive. My expectation is that because the (replace:) has generated an immediate result in the current passage, the (set:) should do so as well. Not sure if this is a 'feature' in harlowe (i.e. there is a good reason for it), or a bug.

    cheers

    GDL
  • Well, consider this (collapsing syntax used for readability):
    {|TheGun>[
      (link: "Take the gun")[
        (set: $Gun to 1)
        (replace: ?TheGun)[
           You take the gun and put it in the pocket of your jacket.
        ]
      ]
      or (link: "refuse it")[
        (replace: ?TheGun)[
          You refuse to take the gun and hand it back.
        ]
      ]
    ]}
    
    Test: $Gun
    
    Now, when the passage is rendered, both links are created, and then at the "Test" line, the contents of $Gun is printed. Now, the passage has finished rendering. If the player then clicks a link, the (set:) and (replace:) are run, but the "Test" line won't suddenly change to match the new value - it's just dead text at that point.

    (Of course, there's a strong case for having special passage structures that constantly mirror values of variables, and in the future I may implement those. For now, though, you'll have to stick with judicious use of the (live:) macro.)
  • You just need to use another tagged hook to output the changed value of the $Gun variable, like the output one below.
    (set: $Gun to 0)
    {
    |TheGun>[
    	(link: "Take the gun")[
    		(set: $Gun to 1)
    		(replace: ?TheGun)[You take the gun and put it in the pocket of your jacket.]
    		(replace: ?output)[$Gun]
    	]
    	or (link: "refuse it")[
    		(replace: ?TheGun)[You refuse to take the gun and hand it back.]
    	]
    ]
    }
    
    gun's current value: |output>[$Gun]
    
  • Thanks L, it makes total sense when you put it that way.

    Thanks greyelf - that works great.

    Cheers

    GDL
Sign In or Register to comment.