Better way to add values to a $gameVariables array?

Discussion in 'Learning Javascript' started by Llareian, Nov 9, 2017.

  1. Llareian

    Llareian Jack of All Trades, Master of None Veteran

    Messages:
    603
    Likes Received:
    1,393
    Location:
    USA
    First Language:
    English
    Primarily Uses:
    RMMV
    I have variables that are used to store arrays of numbers.
    I want to be able to push new values onto those arrays (and splice them out when no longer needed).

    Currently I'm using the method below to push the value of variable 16 onto the array variable 15:
    var tempArray = $gameVariables.value(15) || [];
    tempArray.push($gameVariables.value(16));
    $gameVariables.setValue(15, tempArray);

    I'm wondering if there's a more direct way to do this (without a temporary variable).

    Thanks!
     
    Last edited: Nov 9, 2017
    #1
  2. Sarlecc

    Sarlecc Veteran Veteran

    Messages:
    454
    Likes Received:
    209
    First Language:
    English
    Primarily Uses:
    RMMV
    You can do this:
    Code:
    $gameVariables.setValue(1, []);
    $gameVariables.value(1).push(5);
    $gameVariables.value(1); // => [5]
     
    Last edited: Nov 9, 2017
    #2
    Indsh and Llareian like this.
  3. Llareian

    Llareian Jack of All Trades, Master of None Veteran

    Messages:
    603
    Likes Received:
    1,393
    Location:
    USA
    First Language:
    English
    Primarily Uses:
    RMMV
    Huh, thanks! I didn't realize you could push directly onto .value(). I guess I assumed .value() would pass by value, not reference. This is an area I can learn more about!
     
    #3
    Sarlecc likes this.
  4. Sarlecc

    Sarlecc Veteran Veteran

    Messages:
    454
    Likes Received:
    209
    First Language:
    English
    Primarily Uses:
    RMMV
    Since .value() in this case returns the array that gives you access to all the array functions. :)
     
    #4
    Llareian likes this.
  5. Llareian

    Llareian Jack of All Trades, Master of None Veteran

    Messages:
    603
    Likes Received:
    1,393
    Location:
    USA
    First Language:
    English
    Primarily Uses:
    RMMV
    This is the code I wound up with, in case of interest. This allows the array to be initialized if it hasn't been (since a non-array variable here would break the game) but also allows the array to already have existing values. And neatly avoids a temporary variable.

    if ( !$gameVariables.value(15) ) { $gameVariables.setValue(15, []) };
    $gameVariables.value(15).push( $gameVariables.value(16) );
     
    #5
  6. Sarlecc

    Sarlecc Veteran Veteran

    Messages:
    454
    Likes Received:
    209
    First Language:
    English
    Primarily Uses:
    RMMV
    Note though that if your variable got set to something other than an array your if statement will fail.
    This can be avoided by using typeof:
    Code:
    if ( typeof $gameVariables.value(15) !== 'Array' ) {
        $gameVariables.setValue(15, []);
    } else {
        $gameVariables.value(15).push( $gameVariables.value(16) );
    }
    
    This should avoid any potential errors you could run into with accidentally setting the variable to say a number for instance.

    Edit:
    $gameVariables.setValue(1, 5);
    !$gameVariables.value(1); // returns false
    !$gameVariables.value(2); // returns true
     
    #6
    Llareian likes this.
  7. Poryg

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

    Messages:
    3,340
    Likes Received:
    8,095
    Location:
    Czech Republic
    First Language:
    Czech
    Primarily Uses:
    RMMV
    You can always define your own variables too, although that doesn't solve the actual question :D
    For instance I use gameVariables only for eventing, when I need a variable for something, I define my own variables.
     
    #7
  8. Llareian

    Llareian Jack of All Trades, Master of None Veteran

    Messages:
    603
    Likes Received:
    1,393
    Location:
    USA
    First Language:
    English
    Primarily Uses:
    RMMV
    @Poryg do you mean using a plugin to define them, or in the script box? I always thought variables defined in the script box had local scope.
     
    #8
  9. Poryg

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

    Messages:
    3,340
    Likes Received:
    8,095
    Location:
    Czech Republic
    First Language:
    Czech
    Primarily Uses:
    RMMV
    You can use a plugin to define them, but that is impractical, so use a script box quite often.

    Allow me to demonstrate the difference between local and global variables though.

    Local variables are defined by var. Their scope of use is only inside a program or, if they are defined inside a function, inside that function. The reason why var defined in script boxes are local is because the script box is in fact a function consisting of an eval command (which allows it to execute whatever code you type in).
    However, if you define a variable without using var, then you defined a global variable. This variable can be stored inside a browser travelling from site to site, which can, if site designers aren't careful, cause bugs (although this happens pretty rarely, both due to browser evolution and the chance of you hitting upon a variable that the other site uses in a code but forgets to define), but in computer games they are completely harmless.
    For example
    x = 3 - I have defined a global variable x.
    Don't need a global variable anymore? No problem, simply
    x = "undefined"
     
    #9
    Llareian likes this.
  10. Llareian

    Llareian Jack of All Trades, Master of None Veteran

    Messages:
    603
    Likes Received:
    1,393
    Location:
    USA
    First Language:
    English
    Primarily Uses:
    RMMV
    Thanks! That makes more sense now.

    @Poryg the problem with this approach seems to be that the global variable is not saved with the game data. Is there a simple fix to this that I'm not aware of?
     
    Last edited: Nov 10, 2017
    #10

Share This Page