Howdy, Stranger!

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

Simple way to add inventory

Can someone please explain to me a simple way of adding an inventory? It doesn't have have to be super robust. Just a simple inventory where you can store items that will act as keys in certain junctures, whether or not they are keys at all.

Comments

  • It really depends on how detailed you want to be. It's possible to do a rudimentary inventory system with nothing but Twine. For example, say this is Doom. Lets add the keys to the players inventory.
    <<set $redkey to 1>>
    <<set $yellowkey to 1>>
    <<set $bluekey to 1>>
    Then to enter a blue door:
    <<if $bluekey>>
    [[Go through the blue door|Bluedoor]]
    <<else>>
    You need the blue key.
    <<endif>>
    You can also use this if structure to display keys in a 'inventory screen.'
    <if $bluekey>>
    You have the blue key.
    <<endif>>
    <br>
    <if $redkey>>
    You have the red key.
    <<endif>>
    <br>
    <if $yellowkey>>
    You have the yellow key.
    <<endif>>
    <br>
    This is the way to do it if you are NOT familiar with arrays or Javascript and the amount of items you want isn't unmanageable.
  • The most basic array-based inventory, where your items are simple strings ("key", "umbrella", "rope", that kind of thing) would be:
    <<set $inventory to []>>
    in Start, and then:

    to add an item:
    <<set $inventory.push("item")>>
    to remove an item:
    <<set $inventory.splice($inventory.indexOf("item"), 1)>>
    to check if you have an item:
    <<if $inventory.indexOf("item") != -1>>
    (true if you have that item)

    you can just use <<print $inventory>> to get a really basic list of items, or you can do something more involved like
    ::inventory (in a passage titled "inventory")
    <<set $_i to 0>><ul><<inventory_>></ul>

    ::inventory_ (in a passage titled "inventory_")
    <<if $_i lt $inventory.length>>\
    <li><<print $inventory[$_i]>></li>\
    <<set $_i to $_i + 1>><<inventory_>>
    <<endif>>
    ...which requires Twine 1.4 (or 1.4.1) to work, and will give you an &lt;&lt;inventory&gt;&gt; that will print out a list of as many items as you have, as if you'd used the
    * twine
    * list
    * format
    for them. This thread contains some talk about more complex inventories too, which you might find helpful.

    And like CoraBlue said -- it really depends on how involved you want your inventory to be. No need to make something really complex when you just want to track a few items, since you start mucking around with javascript and arrays pretty quickly.
  • Great thread!

    Thanks! It looks like we now have a good answer from the other thread! :D

    xax wrote:
    You can just use <<print $inventory>> to get a really basic list of items, or you can do something more involved like
    ::inventory (in a passage titled "inventory")
    <<set $_i to 0>><ul><<inventory_>></ul>

    ::inventory_ (in a passage titled "inventory_")
    <<if $_i lt $inventory.length>>\
    <li><<print $inventory[$_i]>></li>\
    <<set $_i to $_i + 1>><<inventory_>>
    <<endif>>
    ...which requires Twine 1.4 (or 1.4.1) to work, and will give you an &lt;&lt;inventory&gt;&gt; that will print out a list of as many items as you have, as if you'd used the

    * twine
    * list
    * format
    for them.


    So, this is how loops are done in Twine? Cool.

    However, while <<print $inventory>> prints "iron key,brass key" as it should, I get the following error from <<inventory>> in the passage where it's used:

    <<print>> bad expression: $_i is not defined
    <<print>> bad expression: $_i is not defined

    Attached is my game file.

    Thanks again!
  • Could be that bug with the $ symbol.  for some reason it's not being correctly replaced with "state.history[0].variables" when the arguments are parsed.  It worked fine in the previous release.  Until its fixed in 1.41, remember to put a space before $ whereever it is used.  [ $_i]

    An old way to do it would be to print a list.
    <<print $list = $inventory.join($newline+"*");>>
     

    Assuming $inventory was an array of strings and $newline was "\n" or equivalent char-code to get the list rendered by the wikifier.  (If you didn't have a patched version that fixed the old newline bug)

    Simpler than that new fangled macro gobbledygook yea?  ;)
  • oh yeah, Hank is totally correct -- the &lt;li&gt;&lt;&lt;print $inventory[$_i]&gt;&gt;&lt;/li&gt;\ line should be &lt;li&gt;&lt;&lt;print $inventory [$_i]&gt;&gt;&lt;/li&gt;\ to get around that 1.4 bug. (Sorry, I wrote the code and didn't actually bother to test it, whoops.)

    And... oh wow, I hadn't even thought of using print like that, even though I knew it wikified its contents. Back before 1.4 I tried doing the same thing, to make a list holding any number of items that wasn't all messed up, (that issue of each list item belonging to a separate list, because of each one being wikified separately,) and while I did end up with something that worked... it was a mess. Nice to know it's ultimately a one-liner in twine code :V
  • Yeah, it was that bug. Got it to work. :)

    Hank, I couldn't get your macro to work (other than just the exact same as <<print $inventory>>).

    I put the missing 'r' in "inventory" then it printed '0' for the $newline variable. So, I took that out. No more '0' but it still didn't print as a list, and didn't before taking the $newline variable out, either. So, I dunno.  :-\

    With the $newline in there, it prints "iron key0*brass key" and without, "iron key*brass key."

    See attached.


    PS: I also tired setting $newline to <br>. That put a new line between the two array values, but still no list, strangely. Also, it only adds the $newline+"*" after the first value, so the first value wouldn't be part of the list anyway.
  • Ah that is true about the first item pre-append an asterisk of course.  as i said, $newline needs to have '\n' as its value.  since the sn problem is fixed in 1.4 you can just use '\n';
    <<print "*"+$inventory.join('\n'+"*")>>;

    Heck you could make it's use even shorter and reusable as a function.
    ::tweeList [script]
    window.tweeList= function(tweear){return "*"+tweear.join('\n'+"*")};
    use it like so
    <print tweeList( $inventory ) >>
  • Would it be a possibility to add an in-house drag and drop key feature for a key like system in Twine? The majority of us are not coders and even if you are we can probably agree that it stifles the creative process apart from having more control over what you're doing. It would be nice just to be able to say player found rope, and that gets added to the inventory. Then when you get to a certain intersection you have the ability to rappel down, as opposed to something like "if only you had a rope."
    This system would be greatly appreciated in-house.
  • Neyowolf, while I agree coding can hamper creative types, the scenario you just described is easily doable with variables as CoraBlue described, which are the most basic form of "coding" possible, if one can even use that term for simple if statements. What I think we can also agree is that Twine is as easy as creation software tools get. I just don't think the "code" in the following two passages is too terribly stifling:


    Room

    You're in a room. You find a rope. You pick it up.

    <<set $rope = "true">>


    Cliff

    <<if $rope eq "true">>

    You rappel down the side.

    <<else>>

    If you only had a rope!

    <<endif>>
  • So you just put those functions in a twine window page and it will work?
  • Neyowolf wrote:

    So you just put those functions in a twine window page and it will work?


    Yes. Attached is an example.

    Here's a good page for beginners as well: http://www.auntiepixelante.com/twine/
  • Thank you, that was dead-on simple. Do you know of a function that will center an ASCII map?
  • Did <center> not work?

    Post a TWS file for more help.
  • No it didn't work. What's a TWS?
  • TWS is the extension type of Twine files. So when you click File > Open in Twine to open your project, you're opening a TWS file. He's asking to take a look at your Twine project in order to help you out. :)
  • Is there a line of code that will un-squish?
  • My ASCII map is still getting smashed
  • Problem solved. For ASCII to function to correctly you have to add both Pre tags and a double triple curly bracket like this,

    <pre>{{{ASCII Here}}}</pre>
Sign In or Register to comment.