2 quick questions about variables

So I notice that you have to define variables ("control variables") in events. 

1) Say in common event 3, I define variable 8 one way (let's say based on player agility).   Then in common event 4, I define variable 8 another way (let's say based on player defense).  Does this work?  Can I define the same variable in different ways in different events, and it will always just be defined by that particular event?

2) This brings me to my 2nd question.  Whenever I make a new event, do I ALWAYS have to re-define my variables?  What if I want to make something based on how many battles the player fought in the previous map, would I have to use a separate variable to track that, and then define my current event's variable based on that variable?
You can define a variable any time you like; it will just be overwritten if you set it again.

Variables are global to the game, so checking variable 1's value in event 5 on map 5 will be the same value as when you check it in event 12 on map 3.
Ah I see, so then as in my first example above, I can have multiple events define the same variable in different ways, and each time one of the events runs, it will use its own particular definition?  Thanks for the info.
All events in your game share the same set of variables. If you change the value of a variable, every event can access the new value. During test play, you can press F9 to look up the current values of all switches and variables.

Each variable can only hold one value at a time. If you are using variable 8 as both, the players defense and agility within different common events, you have to make sure the common events are not using the variable concurrently.

On the other hand, each map event has access to 4 self-switches, that are unique for each particular event.
Yes, but that might or might not cause confusion if you later work on your game.


You should always name your variables, and then you'll know that the variable was used when you look for a free variable later.


If you know that the variable will be used only temporary for different things in different events, then name it "temp" or something like that.


If you know that you'll need a variable to store data throughout the game, give it a name to remember what is stored inside (for example "chapter number" or so)


When using scripts, always check if those scripts need variables for configuration and if yes, reserve those variables by naming them


And remember, the variable will keep the data from the moment the control variable command was used - if you set a variable to for example the player AGI, and the player levels up/gains AGI, then the variable will still contain the old value until you use another control variable command. That might or might not be what you want.
Gotcha.  Thanks, all.

Original thread: https://forums.rpgmakerweb.com/threads/2-quick-questions-about-variables.40306/