Howdy, Stranger!

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

So I'm doing an Inventory...

edited February 2014 in Help! with 1.x
I'm extremely new to Twine, and I work with graphics usually, so please excuse my extreme newbness. :D

Anyway, I am using the setup found here:, with an added Inventory window of my own design that looks like this:
<<if $carrying['redKey']>>Red Key<<endif>>
<<if $carrying['blueKey']>>Blue Key<<endif>>
<<if $carrying['greenKey']>>Green Key<<endif>>
This is then set to display on all passages using
<<display "inventory">>
so you can always see the inventory
What I'd like to do now is three things that I can't seem to figure out.

1. Firstly, I'd like to make the inventory appear on all passages automatically, without having to put in the Display command all the time. I am wondering if perhaps there is a command I can put in the Start passage to make it auto-appear on all passages, or maybe a stylesheet thing I am unaware of?

2. Secondly, I'd like to put "your inventory is empty" in the Inventory window if I'm not carrying anything, but I can't figure out how. Basically, I need a "print this if nothing else is activated" command, but my brain can't figure it out.

3. Thirdly, right now the inventory displays empty lines if I am not carrying a specific item, which looks ugly. I'd like the line to simply be hidden instead, so the text doesn't have odd holes. I tried using the <<nobr> tags, but couldn't make it do what I wanted it to. What do?


  • 3) Do this:

    <<if $carrying['redKey']>>Red Key<br><<endif>>
    <<if $carrying['blueKey']>>Blue Key<br><<endif>>
    <<if $carrying['greenKey']>>Green Key<br><<endif>>
  • Fab! That works! That's the last one I needed, managed to figure the others out on my own in the meantime. Many thanks. :)
  • No worries! I'm curious as to how you solved the first two. In my game's inventory, it literally checks if every single item is not carried before printing the empty message, which seems a bit ungainly. Did you find a tidier method?

    Also, YAY ZIM  ;D
  • By all means! But I wouldn't call it tidy...I'm sure someone more proficient can come up with something much better.
    Anways, this is my inventory window now:
    It is turn <<print turns()>>. You have <<print $health>> hitpoints. <<print $keys>> keys on the ground.

    You are carrying <<print [$itemCount]>>
    <<if $itemCount lte 0>>Your inventory is empty.<br><<endif>>
    <<if $itemHave['redKey']>>Red Key<br><<endif>>
    <<if $itemHave['blueKey']>>Blue Key<br><<endif>>
    <<if $itemHave['greenKey']>>Green Key<br><<endif>>

    And an example item-pickup-passage to show what happens when I pick an item up.
    You pick up the red key.

    [[Return to the door|begin]]
    <<set $itemHave['redKey']=true>>
    <<set $itemCount+=1>>
    <<set $keys-=1>>
    Suggestions are extremely welcome. I am having a lot of fun making this work...not used to actually building actual working bits of game, only the art for them, so this is a nice change of pace. For the items, I'd love a way to build all the information about the item into the item itself somehow, so all I had to put in the pick-up-passage would be the name of the item, if you know what I mean. I'm sure there's some way to make a nice list of items and sort of pin statistics to them, but I can't figure it out yet.
  • Aha, good sir, luckily for you this topic has been discussed before:,679.0.html

    To reiterate, you can set statistics for objects by creating objects inside them, or basically giving the properties properties. For instance, if your object is $carrying with the properties/inventory items redKey and blueKey, then you'd have something like
    <<set $carrying = {
    redKey: {
    color: "red",
    material: "metal"
    blueKey: {
    color: "blue",
    material: "plastic"
    And to refer to it, you'd use
    <<print $carrying.redKey.color>>
    <<print $carrying.blueKey>>
    And I'm pretty sure that'd you'd still be able to use "<<if $carrying['blueKey']>>, etc." since it's still a property, but honestly don't quote me in that, I'm only just learning JS myself.  ::)

    Now, do mind that I'm just the parrot here, repeating L's and TheMadExile's excellent advice, so give all thanks to them, and if anyone sees that I'm wrong about something, please, please correct me! :)

    Good luck! ;)

  • That's awesome, Primrose, thanks! Can't wait to try it out! :D
  • Another thing: I like to keep my inventory and health things visible at all times, so I usually have the actual stats in a seperate passage, and then set that to <<display>> on all actual story passages. However, that seems to always be one step behind, so to speak. If the story-passage makes the player lose 5 hitpoints, for instance, that will only update in the health display once I proceed to next passage. Is there any way I am missing to make that change appear immediately?

    A very simplified example:
    :: STATS
    <<print $playerHealth>>

    :: STORY
    You are hit for five hitpoints!
    <<set $playerHealth -=5>>
    <<display 'stats'>>
  • I used almost your exact code in a test, and it worked fine... See attachment.

    You could put your stats (and inventory) in the story menu, then you don't have to display them in every passage. Just create a passage called StoryMenu, and make the contents: Health: <<print $playerHealth>> and whatever else you want.
  • Terrific idea, Strangelander. Didn't know you could do that. I have now renamed the Story to "STATS" and have filled it with hitpoints and inventory. T'is grand. And for some reason it updates perfectly, so I suppose that problem is solved as well.

    For my next hurdle, I want a Turn Counter, but I want it to reset each time you die - which Turns() doesn't do it seems. So I'm considering if I could make my own variable called "$turns" and have that go up one every time the player clicks a link or when Twine's own turncounter advances a step - but no idea how to do that. Anyone have an idea?
  • You could use setter-links.

    Or, you could put <<set $counter = $counter + 1>> or <<set $counter += 1>> (both do the same thing) at the top of each passage that you want to increase your counter.

    Also, you may find some interesting stuff in the pre-alpha of my generic turn-based RPG:,1474.0.html

    I've not yet added an inventory yet as they are a pain. But, I'll have to do it eventually. I plan to release new versions every so often in the Workshop.
  • Yeah, putting it on top of each passage would definitely be an option, it just seems a bit messy. I may be overly anal about that, granted...I loathe having repeated code in my projects.

    I think I'll go with Setter-links for now, though I'd definitely prefer something that advanced automatically.

    Thanks though, much appreciated! :)
  • Okay, my inventory is coming along. I've iterated on it quite a bit and have arrived at what seems to me like the simplest solution. Since I am lazy, I'd love to simply have to do this in actual story passages to add an item:
    You pick up the thing.
    <<set $thing +=1>>]
    which then triggers this in my StoryMenu, which acts as my inventory:
    <<if $thing gt 0>>Thing x<<$thing>><br><<endif>>
    resulting in having the inventory print "Thing x1". Nice and simple.

    This does a bunch of things for me that I really like: It lets me keep a nice list of all my items in one place. It lets me add more or less of the same item really easily, and even lets me print how many of the item I have. And if I spend the item, it is automatically removed from the list.

    Now for my next steps, where I hope you lovely people will give me yet another hand.  :D

    1. I am still not confident how I'd go about adding stats to items using this approach - for instance, if I wanted an item to have weight or damage ratio. And if at all possible, I'd like all the information about an item to be in one place, so it's not scattered all over.

    Told in my ludicrous scrap-code, I'd like something like this, just, you know, in a way that works:
    <<$thing.weight = 5>>
    <<$thing.damage =(1 to 5)>>
    <<$thing.color = blue>>
    <<if $thing gt 0>>Thing x<<$thing>><br><<endif>>
    2. Possibly related to the stats thing, I'd love for a way to keep count of how many items I am carrying. That would let me display something like "You have nothing in your inventory" when you're carrying nothing, and could probably be used for other things as well.

    I could do it like this:
    You pick up the thing.
    <<set $thing +=1 and $itemCount +=1>>]
    but I wonder if there's a cleaner way to do it that doesn't have to bloat my nice, simple pick-up routine from before.
  • Bluntie wrote:
    1. I am still not confident how I'd go about adding stats to items using this approach - for instance, if I wanted an item to have weight or damage ratio. And if at all possible, I'd like all the information about an item to be in one place, so it's not scattered all over.

    You didn't look at the code in the link I provided. You should.
    <<set $thing= {
    name: "Name",
    weight: 0,
    damage: 0,
    amount: 0,

    <<set $thing.amount += 1>>
    <<set $ = "Sword">>
    <<set $thing.weight = 5>>
    <<set $thing.damage = Math.floor((Math.random()*5)+1)>>
    <<if $thing.amount gt 0>>Thing x<<$thing.amount>><br><<endif>>

    Bluntie wrote:
    2. Possibly related to the stats thing, I'd love for a way to keep count of how many items I am carrying.

    You're getting into lists here. I don't have time to go into lists/arrays right now, and it's been done before here. You should look at the link Primrose provided up thread and this one:,724.0.html
  • Oho! No, I think I must have missed that, pardon. But thanks! :)
  • I have been looking at an inventory system myself recently; I have posted a framework at my blog:
Sign In or Register to comment.