Llareian

Jack of All Trades, Master of None
Veteran
Joined
Jan 26, 2017
Messages
608
Reaction score
1,443
First Language
English
Primarily Uses
RMMZ
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
608
Reaction score
1,443
First Language
English
Primarily Uses
RMMZ
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
608
Reaction score
1,443
First Language
English
Primarily Uses
RMMZ
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,228
Reaction score
11,235
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
608
Reaction score
1,443
First Language
English
Primarily Uses
RMMZ
@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,228
Reaction score
11,235
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
608
Reaction score
1,443
First Language
English
Primarily Uses
RMMZ
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:

Latest Threads

Latest Posts

Latest Profile Posts

I'm finally done with my demo for the Cube Trail! All that needs to happen is playtesting of the four main characters for any balance changes that need to be done.
Finally finished a little game for the one map challenge!
Game Link
IMG_6319.png
imgur sure is getting weird, one day I lose gradually all my images, the other I get them back randomly and then again they disappear again.
Despite OPT2 praise it still has some strange stories in it. Lady Mikka is trying to work herself to death because of guilt. Guilt over what? ¯\_(ツ)_/¯ So instead of finding a NPC to have a heart to heart with her they decide the cure is a new kimono. So when she drops dead she'll at least be well dressed. I haven't even got to the strange part yet.
Did so much work on the game today. I wish I could post it all in this status update but there is a character limit of course haha. I thought about making a topic for updates, though... maybe.

Forum statistics

Threads
131,691
Messages
1,222,276
Members
173,441
Latest member
Purple-H
Top