Howdy, Stranger!

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

(Harlowe) Combining two arrays?

Hello-

TL;DR, I'm trying to append one array to another.

The player has an inventory, saved as $inv. The player is holding a sword and shield. So it looks something like
(set: $inv to (a: "sword", "shield")

Some "places" have an "inventory", such as the refrigerator, which is $fridge.
(set: $fridge to (a: "sandwich", "drink")

It's easy to move a set item in and out of the fridge:
(set: $inv to $inv + (a: "drink"))
(set: $fridge to $fridge - (a: "drink"))

But since the player can put whatever the hell he wants in the fridge, there's an option to empty the whole thing into his inventory. I thought it would be something like:
(move: $fridge into $inv)

... but that overwrites whatever was previously in $inv. What can I do to combine $inv and $fridge?

Comments

  • edited October 2016
    What you want is concat (http://www.w3schools.com/jsref/jsref_concat_array.asp).
    (set: $fridge to (a: "sandwich", "drink"))
    $fridge
    (set: $inv to (a: "sword", "shield"))
    $inv
    (set: $inv to $inv.concat($fridge))
    $inv
    

    Pet peeve: I really dislike harlowe's way of hiding jscript interaction as it results in some wonky designs. Eg.
    (set: $inv to $inv.concat($fridge)) <-- works despite all odds because concat returns the new array
    (set: $fridge = ["sandwich", "drink"]) <-- does not work but is valid JS
    (set: $fridge to ["sandwich", "drink"]) <-- does not work either despite looking like it would
    (set: $fridge.push("head")) <-- does not work due to syntax
    (set: $fridge to $fridge.push("head")) <-- $fridge is now equal to 3 as push returns the array length
    

    Can make getting stuff done a bit complex as you end up mixing and matching syntaxes
Sign In or Register to comment.