Howdy, Stranger!

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

[Twine 2.1.1][Sugarcube 2.14.0] checking for specific object within array

I have recently switched to Sugarcube from Harlowe and after watching some videos on youtube and implementing the inventory system from the Twine Wiki, i have run into a problem.
I want to be able to check if i have a specific object in my inventory array, but what works for values does not work for objects.

I'm new to programming in general and the idea at the moment is to prevent the player from picking up the same items twice.
<<if $inventory.indexOf("$sword") == -1>>
<<linkreplace"Pick up the sword">>//You've picked up the sword!//<<addToInv $sword>><</linkreplace>><<else>>There used to be a sword here.
<</if>>

The object is successfully added to the inventory and i can print it and its values just fine, but when i return to the same passage where i got it, i can pick it up once more.

The code works as intended if it's just "sword" instead of "$sword".

I'm probably going about this all wrong, but i have not been able to find anyone else doing something similar so i turn to you. Does anyone know how to make this work?

Comments

  • edited March 2017
    Firstly, you are passing a string "$sword" not the actual reference to $sword!
    I would advise defining an ID for every item in your game and having a member function attached to every container that is to hold an item in your gane. The function could lookup items by their ID.

    I believe TheMadExile posted some code on that.

    About the sword reappearing:
    Are you using <<back>> anywhere in your code?
    That normaly cancels changes like picking up a sword (adding an item to an array) or changing values.
  • Thanks for your answer Disane, I think i might have found the code TheMadExile wrote, it seems pretty complex, but i'll look into it :smile:

    I don't use <<back>> anywhere, but i do use <<return>> to exit the inventory.

    btw... in harlowe you could easily hide footers by making it so that it wouldn't show up in passages that was tagged a certain way. Could you make a similar function in Sugarcube to hide the inventory or other functions in the sidebar by tagging passages?
  • Pizzalord wrote: »
    Could you make a similar function in Sugarcube to hide the inventory or other functions in the sidebar by tagging passages?
    SugarCube has a built in Passage Tag based Styling feature which is documented in the CSS section of its manual. You can use it to style both the story format's built-in HTML elements which you can learn about in the HTML section of the manual, as well as any custom HTML element you add to the story UI.
Sign In or Register to comment.