Howdy, Stranger!

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

How to create a working Inventory for a complete newbie?

edited September 2015 in Help! with 2.0
Hi guys. I'm working on my first Twine game, and given I have zero experience with all this Javascript stuff, I have no clue how to actually make a working inventory. I scoured the forums for about two days straight and still haven't found a way to add or remove items and have kind of currency for my game.

What kind of coding would I need to figure out to implement this?

This is what I have thus far in my side bar.
(if: $inv's length is 0)[<b>You aren't carrying anything!</b>]
(else:)[You're carrying:$inv]

Working in Harlowe, btw.

Comments

  • If you look at the TwineScript operators section of the Harlowe documentation you will see that if you add two arrays together using the plus character the resulting array will contain the items of both arrays the other arrays. The following example uses that information to add an item to a $inventory array.
    Start with an empty array
    (set: $inventory to (array:))
    
    Adding a "Sword" to the inventory:
    (set: $inventory to it + (array: "Sword"))
    You have: $inventory
    
    Adding a "Shield" to the inventory:
    (set: $inventory to it + (array: "Shield"))
    You have: $inventory
    
    Remove the Sword from the inventory:
    (set: $inventory to it - (array: "Sword"))
    You have: $inventory
    
  • edited September 2015
    Alright, I think I understand that much, thanks!

    But then, how do I get these changes to reflect properly in the sidebar? It's now showing like so:
    [Youre carrying:0]
    

    It says this even though I have the if-statement that should tell you that you aren't carrying anything.

    And lastly, how do I keep the story contained to the right? The text moves under the sidebar once past it.
  • If you're using greyelf's code, did you change the sidebar code to $inventory rather than $inv
    (if: $inventory's length is 0)[<b>You aren't carrying anything!</b>]
    (else:)[You're carrying:$inventory]
    

    The fact that it's printing 0 suggests the variable hasn't been initialised.
  • prof_yaffle, that worked! Thanks a lot! It's so obvious now that you pointed it out. Duh -_-'
Sign In or Register to comment.