Howdy, Stranger!

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

(Harlowe) After changing a datamap's value, I can no longer remove it from an array

Here's an example of what's going on:
(set: $inventory to (a: ))
(set: $item_sword to (datamap: "name", "sword"))

(set: $inventory to it + (a: $item_sword))
(set: $item_sword's name to "broadsword")
(set: $inventory to it - (a: $item_sword))

(if: $inventory contains $item_sword)[sword in inventory]
(else:)[sword not in inventory]

As you can see I've added the sword to the inventory array, changed its name, and then tried to remove it from the inventory. But the final if-statement always tells me that the sword is still in the inventory.

The same thing does not happen when adding an item to the inventory, if I change the item's name and then add it to inventory that works fine. The issue happens specifically when you add the item to the array, then change its name, then try to remove it.

Anyone have any idea what's going on??

Comments

  • edited August 2016
    The issue is that after you change the object's name from "sword" to "broadsword" the object referenced by the $item_sword variable is no longer the same object referenced in the first element of the array contained within the $inventory variable.

    I will try to demonstrate what is happening using the following example:
    {(set: $inventory to (a: ))
    
    (set: $item_sword to (datamap: "name", "sword"))
    } **Initial**
    //item_sword:// (print: $item_sword) {
    
    (set: $inventory to it + (a: $item_sword))
    }//inventory length:// (print: $inventory's length)
    //inventory:// (print: $inventory's 1st)
    <hr>
    **Change to broadsword**
    note: Because item_sword and inventory's 1st are currently pointing to same object, changing one will change the other.
    (set: $item_sword's name to "broadsword")
    //item_sword:// (print: $item_sword)
    //inventory length:// (print: $inventory's length)
    //inventory:// (print: $inventory's 1st)
    <hr>
    **Change to dagger**
    note: At this point item_sword and inventory's 1st are **no longer** pointing to same object, so change one does **not** effect the other.
    (set: $item_sword's name to "dagger")
    //item_sword:// (print: $item_sword)
    //inventory length:// (print: $inventory's length)
    //inventory:// (print: $inventory's 1st)
    
    If you run the above code you will see that both $item_sword and $inventory's 1st change to "broadsword" but only $item_sword changes to "dagger", which indicates that $item_sword and $inventory's 1st are no longer referencing the same object after the first name change.

    This is why your (set: $inventory to it - (a: $item_sword)) code is not working, because you are trying to remove an object (reference) that no longer exists in the $inventory array.
Sign In or Register to comment.