Weird Array Pop() Behaviour

Discussion in 'Javascript/Plugin Support' started by Rink27, Aug 26, 2018.

Thread Status:
Not open for further replies.
    Tags:
  1. Rink27

    Rink27 Veteran Veteran

    Messages:
    222
    Likes Received:
    12
    Location:
    Caribbean
    First Language:
    English
    Primarily Uses:
    RMMV
    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?
     
    Last edited: Aug 26, 2018
    #1
  2. Poryg

    Poryg Pixie of the Emvee kingdom, Ham of a Hamster Veteran

    Messages:
    3,777
    Likes Received:
    9,440
    Location:
    Czech Republic
    First Language:
    Czech
    Primarily Uses:
    RMMV
    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.
     
    #2
  3. Rink27

    Rink27 Veteran Veteran

    Messages:
    222
    Likes Received:
    12
    Location:
    Caribbean
    First Language:
    English
    Primarily Uses:
    RMMV
    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?
     
    #3
  4. Poryg

    Poryg Pixie of the Emvee kingdom, Ham of a Hamster Veteran

    Messages:
    3,777
    Likes Received:
    9,440
    Location:
    Czech Republic
    First Language:
    Czech
    Primarily Uses:
    RMMV
    And how did you make the $gameVariables.value(1) and $gameVariables.value(2) arrays?
     
    #4
  5. Rink27

    Rink27 Veteran Veteran

    Messages:
    222
    Likes Received:
    12
    Location:
    Caribbean
    First Language:
    English
    Primarily Uses:
    RMMV
    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.
     
    Last edited: Aug 26, 2018
    #5
  6. Poryg

    Poryg Pixie of the Emvee kingdom, Ham of a Hamster Veteran

    Messages:
    3,777
    Likes Received:
    9,440
    Location:
    Czech Republic
    First Language:
    Czech
    Primarily Uses:
    RMMV
    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.
     
    #6
  7. Rink27

    Rink27 Veteran Veteran

    Messages:
    222
    Likes Received:
    12
    Location:
    Caribbean
    First Language:
    English
    Primarily Uses:
    RMMV
    @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.
     
    #7
  8. Poryg

    Poryg Pixie of the Emvee kingdom, Ham of a Hamster Veteran

    Messages:
    3,777
    Likes Received:
    9,440
    Location:
    Czech Republic
    First Language:
    Czech
    Primarily Uses:
    RMMV
    Yup, slice method returns a new array, so that is completely fine. And that is the difference between reference and assignment.
     
    #8
    Rink27 likes this.
  9. Kes

    Kes Global Moderators Global Mod

    Messages:
    20,366
    Likes Received:
    10,405
    First Language:
    English
    Primarily Uses:
    RMVXA

    This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

     
    #9
Thread Status:
Not open for further replies.

Share This Page