Weird Array Pop() Behaviour

● ARCHIVED · READ-ONLY
Started by Rink27 9 posts View original ↗
  1. I've been working on a plugin I created and noticed something odd.
    The following is a representation of piece of my code:

    Code:
    var array = $gameVariables.value(x);
    var array2 = $gameVariables.value(y); // x and y were given the same values
    
    //EDIT: I meant x and y represent different numbers, but these game variables contain the same value.
    
    var id = array.pop();
    console.log("Array: "+$gameVariables.value(x));
    console.log("Array: "+$gameVariables.value(y));

    The arrays for BOTH game variables (x and y) are decreasing as I pop out values from "array".
    Firstly, I thought only the new variable "array" would be affected, but $gameVariables.value(x) is also being affected.
    Secondly, I don't understand why a different variable is also being affected.

    I tested giving $gameVariables.value(y) a different array value and it was unaffected though. Does anyone recognise this issue?
  2. That's not weird at all. When x and y are given the same values, it means that x = y.
    Therefore
    $gameVariables.value(x) is the same as
    $gameVariables.value(y)

    and therefore since you pop one, you pop the other one as well.

    As for why if you pop array, $gameVariables.value(x) also gets popped:

    There are two major things to talk about: reference and value assignment.
    Assignment happens when you either deal with immutable objects (numbers, letters) or with where you call the constructor function.
    So you can freely say x = 3 and x = 5. Numbers are immutable, in other words 5 is always 5 and cannot be changed.
    However, arrays are mutable. Therefore when you want to make a new one, you need to call a constructor function. There are multiple ways you create an array, one of them is via the new Array, but the most common is via these [] brackets.
    When you do x = an already existing array, you reference the already existing array and therefore what influences the first one, influences the second one as well.
  3. Poryg said:
    That's not weird at all. When x and y are given the same values, it means that x = y.
    Therefore
    $gameVariables.value(x) is the same as
    $gameVariables.value(y)

    and therefore since you pop one, you pop the other one as well.

    Thank you for the quick reply. I was wondering if it had to do with references - I'm familiar with the concept.
    Um, one mistake on my part. When I said x and y were the same, I meant, for example:
    $gameVariables.value(1) and $gameVariables.value(2) were both given the same values.
    My bad; I didn't make that clear at all.

    Is this behaviour still normal with my updated explanation?
  4. And how did you make the $gameVariables.value(1) and $gameVariables.value(2) arrays?
  5. Poryg said:
    And how did you make the $gameVariables.value(1) and $gameVariables.value(2) arrays?

    A prior piece of code performs:
    $gameVariables.setValue(1,theArray);
    $gameVariables.setValue(2,theArray);

    Edit: So I'm using the slice() method to clone the array, eliminating the need of the second variable ($gameVariables.value(2)) - I can preserve the array after popping it. Still curious where my understanding is flawed though with my updated explanation.
  6. And is theArray an already created array?
    Like is it this format:
    Code:
    var theArray = [1,2,3];
    $gameVariables.setValue(1, theArray);
    $gameVariables.setValue(2, theArray);

    if yes, it is reference, not assignment and therefore it would be normal.
  7. @Poryg

    The code goes like:
    Code:
    var theArray = [];
    
    // segment of code that pushes values into it
    
    $gameVariables.setValue(1,theArray);
    $gameVariables.setValue(2,theArray);

    So I guess it's a reference still - thanks. I also edited my previous comment stating that I'm using the slice() method to solve my issue. Thanks for your time.
  8. Yup, slice method returns a new array, so that is completely fine. And that is the difference between reference and assignment.
  9. [closed]IgnoreMe[/closed]