Using If Statements and Link Replacers

Twine Version: Harlowe

Hi guys, I’m very new to Twine and kinda stuck right now with using if statements and links together.

I’ve been using link replacers to extend my story in passages through choices (and i want the other choices to disappear after choosing one link). Not sure if this is the best way to do this, but it’s been working.

But now I’m trying to use If statements to check if a variable has been acquired or not, for example if the player has “Dog Toy” or not.

Something like this:
(if:$dogtoy >= 1)[Give dog toy to dog.]
(if:$dogtoy < 1)[You don’t have a dog toy.]

I want to use the if statements and links together, to make it so that only if the player has the variable the link is shown OR if the player does not have the variable the link is still shown but when you click it, an alternative message is shown (like, “you don’t have a dog toy”).
Everything I’ve tried so far hasn’t worked (I have no idea what I’m doing), so any help is appreciated! Below is the link replacers I use.

|links>[
(link: “Pet the dog.”)[(replace: ?links)[You pet the dog!]]
(link: “Pet the cat.”)[(replace: ?links)[You pet the cat!]]
(link: “Pet the turtle.”)[(replace: ?links)[You pet the turtle!]]
(link: “Pet the horse.”)[(replace: ?links)[You pet the horse!]]
]

Thanks!

Welcome Steam boat!

First thing, next time you volunteer code, either to describe a problem or offer an answer, it’d be better if you could use the preformatted text button, it looks like </> just above the box where one composes messages. Not doing so change certain characters, especially the quotes or double quotes, while copying and pasting.

About your problem, you may nestle hooks. For instance:

(set:$dog to true)(set:$cat to false)(set:$turtle to true)(set:$horse to true)
{|links>[
(if:$dog)[(link: "Pet the dog.")[(replace: ?links)[You pet the dog!]]<br>]
(if: $cat)[(link: "Pet the cat.")[(replace: ?links)[You pet the cat!]]<br>]
(if: $turtle)[(link: "Pet the turtle.")[(replace: ?links)[You pet the turtle!]]<br>]
(if:$horse)[(link: "Pet the horse.")[(replace: ?links)[You pet the horse!]]<br>]
]
<br>Thanks!}

Of course these (set:) macros can be declared prior the passage. I’ve added brackets to your code in order to not allow an empty line to appear if the variable is set to false, though you could wish to let the reader know about the missing options and keep the empty line by simply removing both brackets as well as the <br> tags.

As a side note, unless you plan at least one of the four animals to always be there by default, you will have to resolve the risk no animal is there to pet!

1 Like

Thanks souppi!
Your solution is working really well!
Sorry for the dumb question, but for this I’m a little confused.

Of course these (set:) macros can be declared prior the passage. I’ve added brackets to your code in order to not allow an empty line to appear if the variable is set to false, though you could wish to let the reader know about the missing options and keep the empty line by simply removing both brackets as well as the <br> tags.

What brackets do I remove if for example, $cat is false, I still select “Pet the Cat.”, it instead shows “You can’t pet the cat.”, and then re-shows the previous options.

Thanks again!

My bad, I meant curly brackets! Not square brackets. English isn’t my primary language, so I’m sometimes less precise than I would have liked. The curly brackets kill some characters like continuing white spaces and line breaks.

1 Like

Thanks so much, everything is working perfectly now!

If you want to do more with the replace: ?stuff, you can put it anywhere and nest multiples. This is what I use for skill checks, which has another inside each for success/failure. For example, when you click “Persuade” it does two things:
(link-reveal:"''Persuade:''")[(replace: ?Intimidate)[ ].
The link-reveal opens the Persuade option (which has it’s own text and future options with Success and Failure), then the replace command with empty brackets deletes the Intimidate option.

Then I repeat the process for the success and failure options. This allows me to have plenty of options within a passage that are mutually exclusive, but don’t require going to a whole new passage.

Important Note: the (replace: ?stuff) command has to be inside the link-reveal command brackets [] or it will mess the whole thing up.

Side note: I use the span around breaks when inserting a lot of code to avoid problems where the web browser reads multiple breaks as a new paragraph and inserts spacing between each line, which looks terrible.
Also, the skill check link-reveal with the DC22 has nothing to do with the rest of the code. I have that in so readers can choose to make a roll before actually knowing what the DC is.

{
<span><br></span>|Persuade>[(link-reveal:"''Persuade:''")[(replace: ?Intimidate)[ ]<p>You attempt to Persuade
<span><br></span>(css: "font-size: 80%;")[(link-reveal: "Skill Check: Persuade")[<br>''DC22'']]

|==
(css: "font-size: 80%;")|success1>[(link-reveal:"''Success:''")[(replace: ?fail1)[](show:?success1)]]
    =|||=
(css: "font-size: 80%;")|fail1>[(link-reveal:"Failure")[(replace: ?success1)[](show:?fail1)]]
 =====||
  |==|
|success1)[<br>''Success:'' You succeed!]
|fail1)[<br>''Failure:'' You fail!]
]]


<span><br></span>|Intimidate>[(link-reveal:"''Intimidate:''")[(replace: ?Persuade)[ ]<p>You attempt to intimidate
<span><br></span>(css: "font-size: 80%;")[(link-reveal: "Skill Check: Intimidate")[<br>''DC22'']]

|==
(css: "font-size: 80%;")|success2>[(link-reveal:"''Success:''")[(replace: ?fail2)[](show:?success2)]]
    =|||=
(css: "font-size: 80%;")|fail2>[(link-reveal:"Failure")[(replace: ?success2)[](show:?fail2)]]
 =====||
  |==|
|success2)[<br>''Success:'' You succeed!]
|fail2)[<br>''Failure:'' You fail!]
]]
1 Like

I’m curious why you’re wrapping your <br> elements inside a <span> element.