Since I've come across a significant number of people lately who aren't aware of some of these things, I thought I'd write a post covering just how useful the console can be to us in game development. And I know there's probably some confusion around the change from "var" in MV to "let" and "const" in MZ, so I figured I'd include an explanation of exactly what they do and what the difference is.
So first things first, var. As most of you will know, var declares a variable. If you don't assign it a value on declaration, it defaults to having the value "undefined"; this is known as "hoisting". For example:
var myVar;
If I then do console.log(myVar) I'll get "undefined".
myVar = 5;
console.log(myVar) will now return 5.
var is a function-scoped keyword, which is to say that a variable declared using var is available anywhere in the defining function. This also means that if you define a variable inside an if statement, for loop etc. you can use it outside of the statement after declaring it. This isn't usually an issue, but there are cases where that can cause some interesting problems if you forget you've already declared a particular variable.
So that brings us to let and const, the new variable declaration methods added in ES6.
let myVar;
This, on the surface, does the same thing as var did, but let is *block*-scoped, meaning the variable is only available to the block that created it. If you define it in an if statement, or for loop, or other nested block, it will cause an error if you try to print it to the console after that block. let variables are also mutable, and can have other values assigned to them. (it's still hoisted to have a default value of undefined)
const is also block-scoped, but is value-immutable, meaning it cannot be assigned a new value after declaring it. However, if its value is an object, you can still change properties on that object.
const myVar = { name: "Trihan", occupation: "Badass scripter", workethic: "Questionable at best" };
If I then try to do myVar = 5, I'll get an error because myVar was already declared as a const. But I can do myVar.workethic = "Slightly better than it used to be" and that'll change just fine.
So which should you use? My rule of thumb is now to use const for every variable unless I need to be able to change its value, in which case I use let. I never use var now. Your mileage, of course, may end up varying!
Now that we've covered the subtle-yet-often-important differences between these three, let's talk about the console. Some of you reading this may not even have any idea this is a thing; I've spoken to more than a few people in the past week who didn't even know it was there!
In RPG Maker MV or MZ, while you're playtesting, hit F8. You'll be met with something that looks like this.
Doesn't look particularly impressive, but you can use this little dev tools powerhouse to test *any* Javascript code your dark, twisted little heart desires. It can be used for debugging, for trying out a line before you add it to a notetag, for checking a property on an object...
For example, let's say I've added a new weapon with ID 25 which uses a notetag provided by a plugin to provide a variable amount of attack power dependent on a formula, and I need to make sure equipping it gives me the right value. I mean, sure, I could make an event that gives me that weapon, go into the menu, check my attack before equipping it, equip it and then check the value after, but I could also do...
Now I know exactly what effect equipping that weapon will have, and I didn't even have to do anything in-game to do it!
You can also use it to check more complex calculations before you use them in a formula:
And the console has object autocomplete: if you reference an object and type a ., all of the functions and properties available to that object will be displayed:
This provides a limited capability for finding out the internal variables and script call functions which you can perform manually for plugin functionality in the case where you need to do it in code. For example, if you're using the BTB battle system and type this, you'll see bravePoints as a function in the list, which you can call to return the number of BP your battler has. This can then be referenced in code in notetags to have certain effects happen dependent on what the BP is.
But even better than that, if you don't remember how a particular function works, you can type it without the calling parentheses and the console will output the function itself (since functions are just objects like anything else). For example, if I want to check the implementation of the removeState() function:
I can see from this that the function already calls isStateAffected before doing anything, so I know that there's no need for me to check for a state being present before I call removeState somewhere.
In some cases, like this one, it won't show the entirety of the function's code. So if I do want to delve more deeply into it, or need to trace the hierarchy of function rewrites (for plugin purposes) I can click on or near the "f" there and...
it'll take me into the sources tab and navigate to the function declaration, where I can see it in its entirety. You can see from the top bar that this is located in rmmz_objects.js, which is one of the default files. Note that you can traverse the source manually as well by expanding the js folder on the left. This is really useful for figuring out precisely which plugin has created the currently "active" version of a particular function. (Thanks to @Restart for reminding me I forgot to include this originally)
The possibilities are endless! I use the console daily, for so many things, and I'd be utterly lost without it. For those who didn't know about it, enjoy your newfound toy! For those who already did, hopefully you still learned something useful from this. 🙂
So first things first, var. As most of you will know, var declares a variable. If you don't assign it a value on declaration, it defaults to having the value "undefined"; this is known as "hoisting". For example:
var myVar;
If I then do console.log(myVar) I'll get "undefined".
myVar = 5;
console.log(myVar) will now return 5.
var is a function-scoped keyword, which is to say that a variable declared using var is available anywhere in the defining function. This also means that if you define a variable inside an if statement, for loop etc. you can use it outside of the statement after declaring it. This isn't usually an issue, but there are cases where that can cause some interesting problems if you forget you've already declared a particular variable.
So that brings us to let and const, the new variable declaration methods added in ES6.
let myVar;
This, on the surface, does the same thing as var did, but let is *block*-scoped, meaning the variable is only available to the block that created it. If you define it in an if statement, or for loop, or other nested block, it will cause an error if you try to print it to the console after that block. let variables are also mutable, and can have other values assigned to them. (it's still hoisted to have a default value of undefined)
const is also block-scoped, but is value-immutable, meaning it cannot be assigned a new value after declaring it. However, if its value is an object, you can still change properties on that object.
const myVar = { name: "Trihan", occupation: "Badass scripter", workethic: "Questionable at best" };
If I then try to do myVar = 5, I'll get an error because myVar was already declared as a const. But I can do myVar.workethic = "Slightly better than it used to be" and that'll change just fine.
So which should you use? My rule of thumb is now to use const for every variable unless I need to be able to change its value, in which case I use let. I never use var now. Your mileage, of course, may end up varying!
Now that we've covered the subtle-yet-often-important differences between these three, let's talk about the console. Some of you reading this may not even have any idea this is a thing; I've spoken to more than a few people in the past week who didn't even know it was there!
In RPG Maker MV or MZ, while you're playtesting, hit F8. You'll be met with something that looks like this.
Doesn't look particularly impressive, but you can use this little dev tools powerhouse to test *any* Javascript code your dark, twisted little heart desires. It can be used for debugging, for trying out a line before you add it to a notetag, for checking a property on an object...
For example, let's say I've added a new weapon with ID 25 which uses a notetag provided by a plugin to provide a variable amount of attack power dependent on a formula, and I need to make sure equipping it gives me the right value. I mean, sure, I could make an event that gives me that weapon, go into the menu, check my attack before equipping it, equip it and then check the value after, but I could also do...
Now I know exactly what effect equipping that weapon will have, and I didn't even have to do anything in-game to do it!
You can also use it to check more complex calculations before you use them in a formula:
And the console has object autocomplete: if you reference an object and type a ., all of the functions and properties available to that object will be displayed:
This provides a limited capability for finding out the internal variables and script call functions which you can perform manually for plugin functionality in the case where you need to do it in code. For example, if you're using the BTB battle system and type this, you'll see bravePoints as a function in the list, which you can call to return the number of BP your battler has. This can then be referenced in code in notetags to have certain effects happen dependent on what the BP is.
But even better than that, if you don't remember how a particular function works, you can type it without the calling parentheses and the console will output the function itself (since functions are just objects like anything else). For example, if I want to check the implementation of the removeState() function:
I can see from this that the function already calls isStateAffected before doing anything, so I know that there's no need for me to check for a state being present before I call removeState somewhere.
In some cases, like this one, it won't show the entirety of the function's code. So if I do want to delve more deeply into it, or need to trace the hierarchy of function rewrites (for plugin purposes) I can click on or near the "f" there and...
it'll take me into the sources tab and navigate to the function declaration, where I can see it in its entirety. You can see from the top bar that this is located in rmmz_objects.js, which is one of the default files. Note that you can traverse the source manually as well by expanding the js folder on the left. This is really useful for figuring out precisely which plugin has created the currently "active" version of a particular function. (Thanks to @Restart for reminding me I forgot to include this originally)
The possibilities are endless! I use the console daily, for so many things, and I'd be utterly lost without it. For those who didn't know about it, enjoy your newfound toy! For those who already did, hopefully you still learned something useful from this. 🙂