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
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.
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?