is it possible to create a global var in a script event command?

Hey guys,
Simple question: basically what im asking in the title. "is it possible to create a global var in a script event command?"
Currently when i make a var in a script event command, it seems to be gone after the script is gone. Is there a way to make it stick around even after the script ended? Or is this only possible in a plugin?
A global var?
All Eventing variables are global.
If you mean a script variable, using a script event command, then why not assign the value to an actual Eventing Variable?
Here goes for switches and for variables:

$gameSwitches.setValue(num, true/false);

$gameVariables.setValue(var, value);


Example of use:

$gameSwitches.setValue(7, true);
The 7th switch turns ON



$gameVariables.setValue(9, 180);
The 9th variable (variable ID = 9) takes the value 180.
What exactly is the context? We can't really help find a definite solution unless we know why you need this.
The script command of the events is sandboxed. all commands inside it are intentionally deleted when the script command finished executing.
The script command of the events is sandboxed. all commands inside it are intentionally deleted when the script command finished executing.
As I thought.
Thus he can probably retain an integer value, by assigning it to an actual Eventing variable, using Control Variables Event Command right?
For a boolean he can use Switches respectively,
and for a string, let's say we set a dummy actor in the database with id = 7, to store a string as its name.
Then we can use:
$gameActors.actor(10).setName("Nick")

Right?
Actually the variables are not limited to integer use. You can put whatever you want in it (I even put objects in it actually), if you're careful enough in their use.
So this works too :
$gameVariables.setValue(9, "Nick");
Actually the variables are not limited to integer use. You can put whatever you want in it (I even put objects in it actually), if you're careful enough in their use.
So this works too :
$gameVariables.setValue(9, "Nick");

Can you actually store whatever you want inside the variables?😮
Oh! Of course. Makes sense. It's javascript after all. It sets the type on the fly.
But still it can become easily a mess like that. Plus, why use strings for instance? You can do most of the stuff with just integers and if you need a string you can use the name of an actor so you won't mess things up in the future. But Variable name can protect you from that too I suppose.
You got a point.
@Dreadshadow To some people, having host of dummy actors messes things even more, than having special block of variables they have set to use "script only" and thus know they can hold any type.
Damn I wasnt clear enough. I actually meant variables you create in the actual script like "var super = 55-30". But I think andar answered my question by saying that those "var super = 55-30" get removed after the script is gone with no exceptions?
As we answered, you can use any of the $gameVariables you like in the actual script (since they are global). Then the value won't disappear between scripts call.
Instead of "var super = 55-30", just declare it that way :
$gameVariables.setValue(x, 55-30);
and call it that way :
$gameVariables.value(x);
With "x" being the id number of your variable.
Technically, there is a way (there's always a way, just depends on whether you like it or not).

Variables are declared in function scope (var) or block scope (let, const). The 'script' Event Command executes as its own function, so any variables declared inside of the function are never available outside of the function.

The trick is to use something which is already declared in the global scope. Javascript has a special object called "window" which is always there. It's not just in the global scope, it is the global scope. Any variable declared in the global scope is actually added as a property of the window object (yes, it hurts the head to think about). I would suggest reading more on this and some experimentation.

Experiment: Try it in the browser that you are using to view this page. In Chrome and many other browsers, press F12 to open the Console (or search for how to open the Console in your browser).
Type this into the Console:
Code:
let myVar = 5; //global scope
console.log("The value is " + window.myVar); //myVar is a property of window automatically

In RPG Maker: So, in one event, you could put this as a script command:
Code:
window.myVar = 5;

And another event could access that, like this:
Code:
$gameMessage.add(myVar);
@TWings Ahh, i forgot to mention that Im aware of using variables and switches like that, but i was trying to not use those, because honestly i find it annoying to organize those.

@Aloe Guvner That experiment with f12 in firefox, seems not work with "window". I get "SyntaxError: redeclaration of let myVar".
Also while using
Code:
window.whatever = 5
, which works btw! I also found out by accident that when i would just do
Code:
whatever = 5
it would become global too. But when i do
Code:
var whatever = 5
, it wont be global. Which is strange because i thought i read somewhere that when you start with "var", it becomes a global variable, and if you leave it out the variable wont be global. Also im using var, because im still at 5.2.
Using "var" before the variable makes it temporary for the script or function it's created in. Does it really work as a global if you leave var off?
Using "var" before the variable makes it temporary for the script or function it's created in. Does it really work as a global if you leave var off?
If global by my definition means that the variable still works outside the script, and inside other events, then yes.
The browsers sometimes vary in their implementation of the developer console (Chrome won't be exactly the same as Firefox, etc.), so your results might be different for things like that. It looks like in both the Chrome and Firefox developer console you can declare global variables without the "var" keyword. Don't get used to it though, because that's not how Javascript is supposed to work.

I did test the RPG Maker example I gave, and that should work fine for your purpose.
You can still use a simple variable to assign a value in, like $gameVariables.setValue(var, value); and it will be global.
That is the point of Control Variables after all. Access them from any Event.
  • var is the id of the Variable
  • value is what you want to put in it.
Doesn't that solve your issue? I mean using this, you CAN assign values, and you can draw such values with simple Control Variable Commands.

P.S.
Yes, @Andar actually answered your question. 🙂
@Dreadshadow oh dont worry, the issue I had was resolved by Aloe Guvner, with his ''window.myVar'', which lead me to find out that apparently just not adding a "var" to your variable, will also make any variable global(apparently). But the reason i dont really like using the "standard" variables and switches is because theyre hard to reorganize once you got a alot of them going on imo.
Actually everyone, I've just found out that once i restart my game, the variables i made without var - even the window.variables, seem to dissapear again. Is there a way to make them permanent? or is the only true way to make them global; by putting them in a plugin, or using the ''$gameVariables.setValue(var, value);'' script
If you want your variable values to persist even after your user saves and closes the game, then you will want to use $gameVariables as those values get written to the save file. If you want to have those values persist then you need to store them in $gameVariables, or modify how writing to save files and loading from save files works to include additional variables. Alternatively, you could also use Yanfly's Utility Common Events to have a common event called on load game which can then determine the appropriate values for your variables based on other properties and variables written to save files.

Another thing to keep in mind: while it was brought up that $gameVariables values can hold anything such as strings, attempting to use floating point numbers (decimals) will get truncated when MV stores the values. Thus if you made a variable called ExpMultiplier and set it to 1.5 by script, it will become 1 as MV is forcing numbers to be integers. The Control Variables command in the editor will prevent typing of a decimal as well. To get around this, you can either store the value as 15 and do the necessary division (15 / 10 = 1.5) or store 1.5 as a string and run an eval command. Not something you asked specifically about, but good to know as I stumbled across this the hard way.
If you want to add your own variables to the save data, it is possible.

However, it seems like a different question than this thread was opened for. Although it might sound annoying, it would be best to create a new thread asking specifically about how to add more data to the save files (probably in the 'Learning Javascript' section).

The reason is for people who want to search the forum for answers, it's best to have 1 topic per thread, so the title is relevant and people can find it later.

Original thread: https://forums.rpgmakerweb.com/threads/is-it-possible-to-create-a-global-var-in-a-script-event-command.94338/