Howdy, Stranger!

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

Sugarcube 2 - Replacing placeholder text based on conditions

I got my original problem fixed but have run into another one. Is it possible to do what I'm wanting to do below? That is, can I have the interactive text replaced with something if the crate has been opened using the methods I'm using, or am I going about it completely wrong?
<<silently>>
<<set $roomWidth = 16>>
<<set $roomLength = 12>>
<<set $crateOpen = "{{{>> }}}In the northwest corner, there is a small crate (you opened it earlier).">>
<<if $cave_room1_crate == "Open">><<replace "#crateText">>$crateOpen<</replace>><</if>>
<</silently>>
{{{>> }}}You find yourself in a small room. The dimensions of the room are <<print $roomWidth>> feet by <<print $roomLength>> feet. 

{{{>> }}}Exits are [[north|cave_room6]] and [[west|cave_room2]].

<span id="crateText">{{{>> }}}In the northwest corner of the room, there is a small <span id="crate"><<click "crate">><<replace "#crate">>crate<</replace>><<replace "#output">>Inside the crate, you find a <span id="item">small silver key</span>, a large <span id="keyItem">Yellow Key</span>, and <span id="gold">25 gold</span>.<</replace>><<set $playerScore +=25>><</click>><<set $cave_room1_crate = "Open">></span>;</span> next to it, a medium-sized <span id="chest"><<click "chest">><<replace "#chest>>chest<</replace>>.<</click>>.</span> <span id="torch">A <<click "torch">><<replace "#torch">><</replace>><<replace "#output">>You take the torch from the wall.<</replace>><</click>> hangs on the wall.</span>

<span id="output"></span>

I tried moving the "If" statement lower in the code (in case it wasn't working because I was attempting to perform a function before the tag was created), but that still threw the "no elements matched selector" error. Thoughts?

Comments

  • Your issue is one of timing.

    The #crateText span won't exist until after the Passage has been completely processed to generate it's HTML output, this is why you get the "no elements matched selector" error because it does not yet.

    I also noticed that the <<replace "#chest">> macro call in your example is missing a Quotation mark at the end of the element ID.

    Try the following:
    <<silently>>
    <<set $roomWidth = 16>>
    <<set $roomLength = 12>>
    <</silently>>
    {{{>> }}}You find yourself in a small room. The dimensions of the room are <<print $roomWidth>> feet by <<print $roomLength>> feet. 
    
    {{{>> }}}Exits are [[north|cave_room6]] and [[west|cave_room2]].
    
    <<if $cave_room1_crate == "Open">>\
    {{{>> }}}In the northwest corner, there is a small crate (you opened it earlier)\
    <<else>>\
    {{{>> }}}In the northwest corner of the room, there is a small <span id="crate"><<click "crate">><<replace "#crate">>crate<</replace>><<replace "#output">>Inside the crate, you find a <span id="item">small silver key</span>, a large <span id="keyItem">Yellow Key</span>, and <span id="gold">25 gold</span>.<</replace>><<set $playerScore +=25>><</click>><<set $cave_room1_crate = "Open">></span>\
    <</if>>\
    ; next to it, a medium-sized <span id="chest"><<click "chest">><<replace "#chest">>chest<</replace>>.<</click>>.</span> <span id="torch">A <<click "torch">><<replace "#torch">><</replace>><<replace "#output">>You take the torch from the wall.<</replace>><</click>> hangs on the wall.</span>
    
    <span id="output"></span>
    
  • That worked perfectly, but I added conditions for the chest and torch and now nothing works. I know it's something very simple I'm overlooking (hands down my biggest challenge as a programmer; I'm fairly certain I'm at least borderline dyslexic), but I just can't seem to figure it out.
    <<silently>>
    <<set $roomWidth = 16>>
    <<set $roomLength = 12>>
    <</silently>>
    {{{>> }}}You find yourself in a small room. The dimensions of the room are <<print $roomWidth>> feet by <<print $roomLength>> feet. 
    
    {{{>> }}}Exits are [[north|cave_room6]] and [[west|cave_room2]].
    
    <<if $cave_room1_crate == "Open">>\
    {{{>> }}}In the northwest corner, there is a small crate (you opened it earlier)\
    <<else>>\
    {{{>> }}}In the northwest corner of the room, there is a small <span id="crate"><<click "crate">><<replace "#crate">>crate<</replace>><<replace "#output1">>{{{>> }}}Inside the crate, you find a <span id="item">small silver key</span>, a large <span id="keyItem">Yellow Key</span>, and <span id="gold">25 gold</span><</replace>><<set $playerScore +=25>><<set $cave_room1_crate = "Open">><</click>></span>.\
    <</if>>\
    <<if $cave_room1_chest == "Open">>\
    ; next to it, a medium-sized chest (it has been opened).\
    <<else>>\
    ; next to it, a medium-sized <span id="chest"><<click "chest">><<replace "#chest">>chest<</replace>><<replace "#ouput1">>Inside the chest, you find a dusty <span id="keyItem">antique lute</span>, a small <span id="keyItem">scrap of paper</span>, and <span id="gold">10 gold</span><<set $playerGold += 10>>. <<addToInv "Clue #1", "Antique Lute">><<set $cave_room1_crate = "Open">><</replace>><</click>>.</span>\
    <</if>>\ 
    <<if $inventory.indexOf("Torch") >= 0>>\
    <<else>>\
    <span id="torch"> A <<click "torch">><<replace "#torch">><</replace>><<replace "#output1">>You take the torch from the wall.<</replace>><<addToInv "Torch">><</click>> hangs on the wall.</span>\
    <</if>>
    
    <span id="output1"></span>
    

    Thank you in advance for being so patient with a beginner such as myself.
  • I can't fully test your last example because I don't have the code for your custom <<addToInv>> macro, but I did notice that you spelt #ouput1 incorrect in your chest related code.

    Assuming your $inventory variable contains an Array you may want to use SugarCube's built-in <Array>.includes() method to test for the existence of "Torch", something like the following should do what you want.
    <<if not $inventory.includes("Torch")>>\
    <span id="torch"> A <<click "torch">><<replace "#torch">><</replace>><<replace "#output1">>You take the torch from the wall.<</replace>><<addToInv "Torch">><</click>> hangs on the wall.</span>\
    <</if>>
    
  • edited February 2017
    I'm using a modified version of F2Andy's Inventory code. The method I used to check for the item does scan an array, I believe (I'm still new with JavaScript); I'm coming from Python and have been looking for a way to check for an object within an array in JavaScript. I also didn't know that JavaScript also used "not" in If statements. I'll give all this a shot and report back. Thanks!
  • historic66 wrote: »
    I also didn't know that JavaScript also used "not" in If statements.
    Javascript doesn't, that not operator is part of SugarCube's implementation of Twine-script but it is the equivalent of Javascripts ! (exclamation point) operator.
    Both operators work, I just used not in the example because it is generally less confusing for non-programmers.
  • greyelf wrote: »
    historic66 wrote: »
    I also didn't know that JavaScript also used "not" in If statements.
    Javascript doesn't, that not operator is part of SugarCube's implementation of Twine-script but it is the equivalent of Javascripts ! (exclamation point) operator.
    Both operators work, I just used not in the example because it is generally less confusing for non-programmers.

    So could I also use <<if ! $inventory.includes("Torch")>>?

Sign In or Register to comment.