Better way to add values to a $gameVariables array?

Llareian

Jack of All Trades, Master of None
Veteran
Joined
Jan 26, 2017
Messages
604
Reaction score
1,421
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:

Sarlecc

Veteran
Veteran
Joined
Sep 16, 2012
Messages
453
Reaction score
211
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:

Llareian

Jack of All Trades, Master of None
Veteran
Joined
Jan 26, 2017
Messages
604
Reaction score
1,421
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!
 

Sarlecc

Veteran
Veteran
Joined
Sep 16, 2012
Messages
453
Reaction score
211
First Language
English
Primarily Uses
RMMV
Since .value() in this case returns the array that gives you access to all the array functions. :)
 

Llareian

Jack of All Trades, Master of None
Veteran
Joined
Jan 26, 2017
Messages
604
Reaction score
1,421
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) );
 

Sarlecc

Veteran
Veteran
Joined
Sep 16, 2012
Messages
453
Reaction score
211
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
 

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,125
Reaction score
10,639
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.
 

Llareian

Jack of All Trades, Master of None
Veteran
Joined
Jan 26, 2017
Messages
604
Reaction score
1,421
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.
 

Poryg

Dark Lord of the Castle of Javascreeps
Veteran
Joined
Mar 23, 2017
Messages
4,125
Reaction score
10,639
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"
 

Llareian

Jack of All Trades, Master of None
Veteran
Joined
Jan 26, 2017
Messages
604
Reaction score
1,421
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:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,862
Messages
1,017,050
Members
137,571
Latest member
grr
Top