Howdy, Stranger!

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

Two Questions About Creating an Inventory

Okay, I have two questions when it comes to arrays.  I am creating an array called $inv for inventory.  I have a link on the left sidebar to check inventory, and when you click it, it takes you to a room that has

<<print $inv >>

So two questions:

1. When you start the game, I want to display the message "You have nothing" when you click that check inventory link.  But once you pick up items, I want it to display the items in the array.  How do I create the <<if>><<else if>> statement so it displays one message if the array is empty and another message if the array has objects?

2. Currently, when I do <<print $inv >>, it gives the list like this:

milk,eggs,sugar

I want it to give the list with one object per line so it looks like this:

milk
eggs
sugar

Is there a way to set it to that?  Maybe by CSS?

Comments

  • And with another hour of tinkering, I think I found the solution.

    The inventory checking room now contains this one piece of code:

    <<if $inv is 0>>You have nothing.<<elseif>><<print $list = $inv.join('\n');>><<endif>>

    It prints "You have nothing" if you haven't picked up anything, and it prints the list in a column if you have.
  • If $inv will always be an array, and simply may be empty, then you're far better off checking its length, rather than by forcing an equality check against an integer (which requires coercion).  Also, you've used &lt;&lt;elseif&gt;&gt; where you should have used &lt;&lt;else&gt;&gt; here.  For example:

    <<if $inv.length is 0>>You have nothing.<<else>><<print $list = $inv.join("\n")>><<endif>>
    Additionally, unless you're using $list someplace else, there's no reason to assign to it simply to call join() on $inv.  You didn't mention $list before, so I'm unsure if you actually need it or just think you do.  For example:

    <<if $inv.length is 0>>You have nothing.<<else>><<print $inv.join("\n")>><<endif>>
  • You're right -- I don't need $list here. (It was part of another idea I was implementing, but I've since dropped it.)

    Checking length would be better, especially if a limit was set as to how many items the person could hold.  If, let's say, the limit for inventory was 5 items.  And when a 6th item was found in the game, the player could decide to drop an existing item (which would splice it from the array) or leave the new object.

    Though <<elseif>> is how I've always written this.  What is the difference between <<else>> and <<elseif>>?
  • cressidahubris wrote:

    Though <<elseif>> is how I've always written this.  What is the difference between <<else>> and <<elseif>>?


    The difference is that &lt;&lt;elseif&gt;&gt; takes a conditional expression (just like &lt;&lt;if&gt;&gt;), while &lt;&lt;else&gt;&gt; does not.  For example, this is how it is supposed to work:

    <<if conditional_expression_1 >>
    <<elseif conditional_expression_2 >>
    <<elseif conditional_expression_ >> /% Et cetera %/
    <<else>>
    <<endif>>
    That &lt;&lt;elseif&gt;&gt; works like &lt;&lt;else&gt;&gt; when called exactly as &lt;&lt;elseif&gt;&gt; (no expression or spaces), rather than throwing an error, is a bug in the vanilla story formats (SugarCube does not have this bug).  As an exercise, adding a single space after the macro name (i.e. &lt;&lt;elseif &gt;&gt;) causes it to work as intended, which will throw the bad condition error because you didn't specify one (i.e. else if what?).
  • Got it.  I am using Sugarcane, hence why there wasn't an error.
  • you might find this Twine Inventory post interesting/useful :)

    http://strugglingwithtwine.blogspot.co.uk/2014/03/handling-inventory.html
  • I found that page before too, and considered using it. But I think it's simpler (and less limiting) to use the straight-up Javascript explained at http://www.glorioustrainwrecks.com/node/5034 and add a simple script to take care of one unintuitive syntax issue.

    The only part I find non-intuitive is removing items from the array. But the following script makes the syntax for that intuitive too:

    // 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;
    }
    Add that code to a passage tagged [script] and then items can be removed from an array like so:

    set $inv.remove('toothbrush')
    In the preceding example, "$inv" is the array, and 'toothbrush' is a string that is removed from $inv.

Sign In or Register to comment.