Tip for other noobs re: default values of $gameVariables

Joined
Jun 22, 2019
Messages
3
Reaction score
2
First Language
English
Primarily Uses
RMMV
So I'm new to both rpgmaker and javascript (just started about a month ago), and just now jumped into some light scripting. I discovered something that probably is obvious to the more seasoned veterans, but maybe it will be of use to my fellow noobs out there.

Namely, that the default value for a variable in $gameVariables is set to 0 as opposed to null.

For context, my game has two exclusive parties, party A and party B. I was messing with some code (I eventually got it working) that would let the player switch between them. The catch was that I wanted to save the actor ids in each respective party (I plan to have many actors in this game) into variables, and add/remove based on those variables.

The problem I ran into was the assumption that game variables that had not been set would be null. See my incorrect first-pass code below:

Code:
var actorid_pos1 = $gameVariables.value(29);

if (actorid_pos1) { 
    $gameParty.removeActor(actorid_pos1);
}
So, with the above code, the condition of "if (actorid_pos1)" would eval to TRUE even if $gameVariables.value(29) had not been set yet. This is because it defaults to 0 (and thus not null).

This caused my game to error out. The condition would pass the 0 value to removeActor(), then the game would query the db for actor_id = 0, which would return a null, which then caused all kinds of errors down the line (as other plugins / game code tried to parse a null object)

The solution, simply, is below (change to "if (actorid_pos1 > 0)"):

Code:
var actorid_pos1 = $gameVariables.value(29);

if (actorid_pos1 > 0) { 
    $gameParty.removeActor(actorid_pos1);
}
Hope this helps someone out there!
 

fugahagen

Villager
Member
Joined
Oct 31, 2017
Messages
21
Reaction score
11
First Language
english
Primarily Uses
RMMV
I'm also noob and just figured out you can type stuff in the console when running the game. So if you want to check the value of a var or just see if one exists you type it in the console and it will return the value or say undefined. It helps when you wan't to see what your code is doing.
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
Actually, its not that the default value is 0 but rather when you use the .value function to get it, the function returns 0 if the variable is null or nonexistent

this is the code of the .value function

Code:
return this._data[variableId] || 0;
So technically, the variable itself is still null. But you'd need to take the variable directly from the _data array which isnt recommended.

I think this is more of because Game Variables are supposed to be used mainly thru the event commands and mostly as integers. If they dont make it return 0 when the variable is null, it can cause various errors when people would use it on event commands without "initializing" it to a value. Since RM is supposed to be usable even by kids, this design is "safer".
 

Aloe Guvner

Walrus
Veteran
Joined
Sep 28, 2017
Messages
1,628
Reaction score
1,115
First Language
English
Primarily Uses
RMMV
So technically, the variable itself is still null.
If we're really getting technical, the value is "undefined", not "null" :wink:

Code:
var actorid_pos1 = $gameVariables.value(29);


if (actorid_pos1) {

$gameParty.removeActor(actorid_pos1);

}
So, with the above code, the condition of "if (actorid_pos1)" would eval to TRUE even if $gameVariables.value(29) had not been set yet. This is because it defaults to 0 (and thus not null).
Are you sure that was the issue? Zero evaluates to false inside an "if" condition.
 
Last edited:

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
Can you test that? Afaik, "if (object)" simply checks if it exists or not.. sadly I cant check it myself right now
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
2,093
Reaction score
1,509
First Language
EN
Primarily Uses
RMMZ
Yep, !!0 is false, I use it a fair bit. Test case (console):
Code:
if (0) console.log('0 is truthy!');
Explanation: JavaScript uses type-coercion where appropriate; for boolean stuff (e.g. if statements) there are a number of Falsy values, including 0, undefined, and null. Non-falsy values coerce to true. :)

Like Aloe says, the issue was probably with something else. :kaophew:
 
Joined
Jun 22, 2019
Messages
3
Reaction score
2
First Language
English
Primarily Uses
RMMV
Thank you all for the replies; lots of good information.

Now I'm curious what my actual issue was... if I figure it out I'll update here :)
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,003
First Language
Tagalog
Primarily Uses
RMVXA
What was the actual error message when you tried it?
 

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

Latest Threads

Latest Posts

Latest Profile Posts

I should realize that error was produced by a outdated version of MZ so that's why it pop up like that
Ami
i can't wait to drink some ice after struggling with my illness in 9 days. 9 days is really bad for me,i can't focus with my shop and even can't do something with my project
How many hours have you got in mz so far?

A bit of a "sparkle" update to the lower portion of the world map. :LZSexcite:
attack on titan final season is airing tomorrow, I'm excited and scared at the same time!

Forum statistics

Threads
105,883
Messages
1,017,232
Members
137,607
Latest member
Maddo
Top