Lack of understanding global variables

Hello everybody!


I need some help understanding global varaibles scope in Rpg Maker MV. I'll enunciate the problem:


So, I have a global variable in my plugin file that I've set up:


var enableMHUD = false;




And I've tried to access it via the game interpreter:

Spoiler
Capture.PNG



But an error has occured:

Spoiler
Err.PNG



I'm new in scripting in Rpg Maker MV, I was more efficient with RGSS but since they replace it by JS, I'm lost 🙁 . That's why I need your help young (or old whatever) scripter!


Thanking you in advance 😉




PS: My plugin file if it's necessary: StatusOnMap.js
Is the plugin file enabled in the plugin manager of the game? 
In your plugin you define:


var $enableMHUD = false


and then you try to change/check


enableMHUD
// you instead want to change
$enableMHUD


Those aren't the same variables. This might be the cause of error.


There is no need to put a $ sign in front of the variable in javascript to declare it global. In fact If I were you i'd just remove the $ sign in the global declaration and be done with it.


It might also be that your plugin commands aren't properly defined. But I see no error screaming at me there.


As a reminder, you also probably want to check equality with "===" instead of "==" to enforce strict type comparison.


Because by using "==" you'll get weird results such as


"5" == 5 //will return true
"5" === 5 //will return false




I used this site to learn javascript basics, it might help you: http://www.w3schools.com/js/js_variables.asp


Edit: I hadn't event thought about the plugin not being enabled, lol. Still my point stands, you are trying to check a variable that doesn't exist.


If you want to check your variable: press f8 and then type in the console:

Code:
console.log($enableMHUD) //should return false
console.log(enableMHUD) //should return undefined
hahahahhahaha.PNG


LMAO,
I feeeeel very stupid, you're very perspicacious !!!


Thank you for that answer but now I feel very stupid 😀


Edit : But that's was not the problem!!!
In your plugin you define:



var $enableMHUD = false


and then you try to change/check



enableMHUD
// you instead want to change
$enableMHUD


Those aren't the same variables. This might be the cause of error.


There is no need to put a $ sign in front of the variable in javascript to declare it global. In fact If I were you i'd just remove the $ sign in the global declaration and be done with it.


It might also be that your plugin commands aren't properly defined. But I see no error screaming at me there.


As a reminder, you also probably want to check equality with "===" instead of "==" to enforce strict type comparison.


Because by using "==" you'll get weird results such as



"5" == 5 //will return true
"5" === 5 //will return false




I used this site to learn javascript basics, it might help you: http://www.w3schools.com/js/js_variables.asp

Thanksss a lot!!!!!


I don't know why I put this dollar thing in that variable!
This thread can be close!


Thanks a lot Astfgl66 !!!!

Original thread: https://forums.rpgmakerweb.com/threads/lack-of-understanding-global-variables.64036/