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
(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
but it works
I made a tiny change for personal tastes, but it works nonetheless.
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.
eg. change ...to
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
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.
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
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.