Howdy, Stranger!

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

Inventory

So I've looked at the inventory system people have used and its a bit confusing and I'm not sure if it will do what I want. Does anyone know a good method to make an inventory that can hold x number of weapons/items/armor and can be accessed by the player in order to use items or equip armor/weapons.

Comments

  • I think it does not get much simpler than this: http://www.glorioustrainwrecks.com/node/5034
  • I have found a way to implement an inventory in a even (slightly) simpler way!

    I love L's article that I recommended in my previous reply, and I use it. However, I think the Javascript syntax for removing an item from an array/inventory is pretty hard to remember. So I found an article where somebody wrote a simpler way to "splice" from a Javascript array.

    That article is How to Remove an Element from an Array.

    The relevant code from that article is:

    // Removes an element from an array.
    // String value: the value to search and remove.
    // return: an array with the removed element; false otherwise.
    Array.prototype.remove = function(value) {
    var idx = this.indexOf(value);
    if (idx != -1) {
    return this.splice(idx, 1); // The second parameter is the number of elements to remove.
    }
    return false;
    }
    Simply paste that code into a Twine passage and give that passage a [script] tag. Now, anywhere in your Twine game you can remove items from a Javascript array (such as one you've given the name of $inv, for example) like so:
    set $inv.remove('bar of soap')
    instead of like so:
    set $inv.splice($inv.indexOf("bar of soap"),1)
    Cool!

    *I also commented on L's article with this info.

    (For more background, read about script passages in the Twine Reference, and about Javascript arrays and about the Javascript indexOf "method" via Google. Lightbulbs will go on.)
Sign In or Register to comment.