MZ var, let, const and our Lord and Saviour the console.

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.
1622345459247.png


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...

1622345638699.png


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:

1622345718473.png


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:

1622345774288.png


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:

1622345972190.png


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...

1622348431392.png


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. 🙂
one addition: if you click on the first line of that printout of 'removestate', it will take you to the sources tab and show you where in the code it actually is located.
one addition: if you click on the first line of that printout of 'removestate', it will take you to the sources tab and show you where in the code it actually is located.
I was actually going to mention that but forgot, thanks!
This is a very nice description of the console's utility. Definitely good to recommend newcomers to.

I definitely don't fall into the camp of declaring every variable as const, but that's a matter of style 🙂
I'm not a big fan in the first place of declaring variables to save myself copy-pasting, so I only make them when the intent is to manipulate their values.
This is a very nice description of the console's utility. Definitely good to recommend newcomers to.

I definitely don't fall into the camp of declaring every variable as const, but that's a matter of style 🙂
I'm not a big fan in the first place of declaring variables to save myself copy-pasting, so I only make them when the intent is to manipulate their values.
...so how do you work with data that you aren't changing the value of? Do you always just type in the full object references to what you're using?
Yeah. (more often copy it once and paste it in each time).

It's just the habit I got into - when I first started taking classes, my teacher discouraged us from making a variable that represented something we weren't manipulating because it can lead to silliness (you've seen some of the beginner stuff on here, where they're doing, like, one if check or for loop, so declaring variables literally doubles the size of their code).

And then it was just the habit that I had as I got into more complex things. If I'm referencing different members of a given object, I'll make a local constant to be able to see that more clearly, but most of the time it's just more typing than I need to do for no actual purpose ¯\_(ツ)_/¯
Yeah. (more often copy it once and paste it in each time).

It's just the habit I got into - when I first started taking classes, my teacher discouraged us from making a variable that represented something we weren't manipulating because it can lead to silliness (you've seen some of the beginner stuff on here, where they're doing, like, one if check or for loop, so declaring variables literally doubles the size of their code).

And then it was just the habit that I had as I got into more complex things. If I'm referencing different members of a given object, I'll make a local constant to be able to see that more clearly, but most of the time it's just more typing than I need to do for no actual purpose ¯\_(ツ)_/¯
That's fair. It's mostly force of habit for me in the other direction, but more often I only create consts to streamline my code in cases where I need to reference the same object multiple times in a block.

Original thread: https://forums.rpgmakerweb.com/threads/var-let-const-and-our-lord-and-saviour-the-console.136968/