Variable Help

I am trying to set up a variable so that it contains a string.  Later, I want to be able to reference that variable.

I've uploaded a bit of code that works just fine and then a second bit of code that doesn't work.

Why does the game forget about the variable I just set up?

Invalid.png

Valid.png
The script box is build like a "sandbox" programming - closing a scriptbox deletes everything assoziated with it. So the next script box has no access to older variables, no matter what they are.

You can only store data in variables that had been defined outside the script command (by a plugin, or in the game variables)
The script box is build like a "sandbox" programming - closing a scriptbox deletes everything assoziated with it. So the next script box has no access to older variables, no matter what they are.

You can only store data in variables that had been defined outside the script command (by a plugin, or in the game variables)
What would be the correct way to do this then?  How do I associate a game variable with a string?  How do I then call said game variable using the $gameMessage function?
You could set one of MV's variables like this (MV itself will only let you store numbers in them, but in a script you are free to do otherwise):

$gameVariables.setValue(1, "text");This will store the string "text" in variable #1.In your message, you can then use \V[1] to retrieve that value.
You could set one of MV's variables like this (MV itself will only let you store numbers in them, but in a script you are free to do otherwise):



$gameVariables.setValue(1, "text");


This will store the string "text" in variable #1.


In your message, you can then use \V[1] to retrieve that value.

But what if one wanted to reference that variable in another script command instead for the purposes of conditional branches? What would the code for that look like?
That game variable are the ones contained in the editor, so think of them as something of a global scope. @Chronic Heart


This means


$gameVariables.setValue(1, "text");


Sets the game variable at id 1 in the editor to some string, which you can reference anywhere -- even in another script command for example.
That game variable are the ones contained in the editor, so think of them as something of a global scope. @Chronic Heart


This means




$gameVariables.setValue(1, "text");


Sets the game variable at id 1 in the editor to some string, which you can reference anywhere -- even in another script command for example.



Yeah, I figured that it would be in the global scope. I was just wondering what referencing a particular variable would look like in a script command. Maybe I'm just missing the obvious, but it seems to me that this line of code is a function for setting a value, not to check it. So how would I write an "if" statement based off of a variable? Would it look like: if ($gameVariables[0] == "text");? Or would it look like something else? 
if($gameVariable.value(1) == "text")


It would look something like that. That would refer to the gameVariable at ID 1. @Chronic Heart
Rpgmaker tends to not use the zero index.

Original thread: https://forums.rpgmakerweb.com/threads/variable-help.49378/