- 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:
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)"):
Hope this helps someone 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);
}
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);
}


