Tip for other noobs re: default values of $gameVariables

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!
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.
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".
So technically, the variable itself is still null.
If we're really getting technical, the value is "undefined", not "null" 😉

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.
Can you test that? Afaik, "if (object)" simply checks if it exists or not.. sadly I cant check it myself right now
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. 😮‍💨
Always learning something new from you guys! Thanks for taking time to explain things like this 😀
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 🙂
What was the actual error message when you tried it?

Original thread: https://forums.rpgmakerweb.com/threads/tip-for-other-noobs-re-default-values-of-gamevariables.110243/