Howdy, Stranger!

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

A simple inventory system

I am using Harlowe or Twine 2. I need a simple inventory system. A var that holds a count of items the player picks up. So I tried this:
(set: $inv to 0)
and at the time the object is taken $inv = $inv + 1 but it doesn't work. Why? I'm just incrementing a simple variable.

Comments

  • Can you put a sample of the ACTUAL code up?
  • (set: $inv to 0)
    (set: $haveBook to false)
    (set: $havetable to false)
    You are in a small room There is a [book]<book| on the <table|.
    (click: ?book)[heavy tome with an age-worn leather cover]]
    (click: ?table)[(replace: ?table)[massive oak dining table]]
    (if: not $haveBook)[(link: "Take Book")[(set: $haveBook to true)($inv=$inv +1)]]
    (if: not $havetable)[(link: "Take Table")[You can't take the table]]
    You can go North or West
  • edited June 2015
    you have an extra ]
    but it works

    I made a tiny change for personal tastes, but it works nonetheless.
    (set: $inv to 0)
    (set: $haveBook to false)
    (set: $havetable to false)
    You are in a small room There is a [book]<book| on the [table]<table|.
    (click: ?book)[heavy tome with an age-worn leather cover]
    (click: ?table)[(replace: ?table)[massive oak dining table]]
    (if: not $haveBook)[(link: "Take Book")[(set: $haveBook to true, $inv=$inv +1)]]
    (if: not $havetable)[(link: "Take Table")[You can't take the table]]
    You can go [[North]] or [[West]]
    $inv
    

    Add an $inv to West, and you'll see what I mean. You just have to leave the room because your variable already rendered once.
  • Well it shows the Inventory count of the $inv variable alright but the variable still doesn't get incremented. :(
  • edited June 2015
    it does. Did you add $inv to West?
    Add an $inv to West, and you'll see what I mean. You just have to leave the room because your variable already rendered once.
  • For consistence sake I would suggest using the to keyword everywhere you are doing an assignment:
    eg. change
    (if: not $haveBook)[(link: "Take Book")[(set: $haveBook to true, $inv=$inv +1)]]
    
    ...to
    (if: not $haveBook)[(link: "Take Book")[(set: $haveBook to true, $inv to $inv +1)]]
    
  • OK here is what I have written and here is the play by play. Sage I have implemented your suggestions as you can see I hope You will be able follow the code and the play I have commented showing what I chose in the play. But even if this worked, what would happen after I took the book and chose North instead where ther is no JS to increment $inv?

    Start

    (set: $haveBook to false)

    There is a [book]<book| on the <table|.
    (click: ?book)[(replace: ?book)[heavy tome with an age-worn leather cover]]
    (click: ?table)[(replace: ?table)[massive oak dining table]]

    (if: not $haveBook)[(link: "Take Book")[(set: $haveBook to true)]]
    You can go North ot West

    North

    you are in narrow dark passage. You can go
    end

    West

    You're in another small room.
    You can go to end
    ($inv = $inv + 1)

    End

    You have reached the end. You may close your browser
    $inv

    PLAY

    Start

    There is a book on the table.



    Take Book <!-- I took the book and chose west to end quickly-->
    You can go North ot West

    West

    You're in another small room.
    You can go to end
    (0 = 0 + 1)
    <!--I chose end-->

    End

    You have reached the end. You may close your browser
    0
  • You won't need to put inv plus anything in west or north or anywhere else because it has already been incremented when they took the book.

    That's why I am saying to put $inv in west. Don't put anything else there that modifies inv. Just put it there as is, so that you can verify to yourself that it has increased.

    So: inv is zero
    They take book and simultaneously inv goes up
    They walk around
    They walk around
    They have no idea what inv is
    They walk around
    But this entire time inv has been one all along

    The only reason I said put $inv in another room is just for you to look at it and verify it went up. But even if you didn't look at it, it went up anyway.

    In the example you wrote earlier it showed you it was zero because it had already been rendered by the browser. You have to go somewhere else and ask it what it is.

    Or not. But even if you don't ask, it still goes up.
  • I don't understand. As you can see INV was not incremented when I queried at the spot marked End it still said 0(zero)
  • edited June 2015
    UPDATED: OK I put the original code back in and it worked also. I did separate $inv=$inv+1 to $inv = $inv + 1 before I ran it. I don't know if that made a difference; I don't think so but... In the first code I include it in **[(set: $haveBook to true, $inv=$inv +1)]** so I guess the set: was there and I had a multiple set:

    Tell me something? I can't find set: as a statement in either JS or jQuery. Is it just unique to Twine or Harlow ?
    ====================================================

    OK I figured it out! I did this in start after I took the book.
    (set: $inv = $inv +1)
    when I queried $ at the point or room (I like to think of them as rooms) it gave me the correct count of 1
    Thanks,
    Paul
  • note: if you want to include code in your comment then use the C button above the comment text box, this will insert a code block into the comment into which you place your code.

    As you seemed to of worked out, you need to us the (set:) macro if you want to either assign a value to a $variable, or increment a $variable.
    assignment:
    (set: $variable to "value")
    
    increment:
    (set: $variable to $variable + 1)
    (set: $variable to it + 1)
    
Sign In or Register to comment.