RPGMaker - Improving Data Scope.
RPGMAKER
- Sub lesson -
Data scope
At this point Iâm assuming you have an idea of how switches and variables work. You know that every treasure chest needs itâs own switch, and you might have even played with using the value of a variable to choose how events render on your map.
The only way to guarantee that two events arenât triggered at the same time is to use a unique switch for every event, right?
Well no, actually. We can create a set of map scope variables and switches.
Before I go on, let me define scope.
To grossly simplify programs are made of levels. There is an outer level that calls smaller parts, and in turn each of those parts call more parts. In most programming languages each level only has access to data it generates. If I created a variable on level 3, level 1 couldnât read it.
Global data is an exception. A global switch can be read and modified by any level.
Most of the data in RPGMaker is at a global scope.
This is why if two chests share switch 001, opening one of them will open both of them.
Another important thing to understand is persistence. Data is only stored as long as the level is active. This means that if the level 3 from before ended and was restarted the values written to its non global variable would have been lost.
For the purpose of ease I will be sometimes calling switches and variables the more generic term Data Structures.
Why do I care?
Suppose you had an event on map 1 that displayed a sign post with a number on it. The number displayed was a random number that resets every time the map loads.
You would need a variable to keep this temporary number in.
The obvious conclusion is to devote some variable, say v0051, and call it âSign numberâ.
Now imagine you want a similar sign on map 3. Would you use a new variable or reuse the old one?
The logical conclusion is to reuse the variable.
Suppose then that you want to add a peasant in map 2 who says one of 5 random things every time you speak to him.
You could make another variable, or you could reuse v0051 yet again.
All of these events occur on different maps. So randomizing the peasants response with the same variable wonât cause the a sign to randomly change.
The Basic solution:
Set aside a few data structures called something like âMap Variable 1, Map Variable 2,... & Map Switch 1, Map Switch 2, âŚâ
#SpacesAreForChumps
Then as you code you associate the generic data structures to one event per map.
You will have to keep a list so you donât get confused as to what value you assigned to what.
Just remember to only use these data structures for data that only needs to be kept as long as the map is loaded.
If you wanted a randomized sign number that changes only when the user speaks to the sign you could not use these generic data structures.
The Mutex Solution:
Suppose this isnât good enough for you. You have thirty villagers who all do random things. You know five of them will be acting at any given time but which ones are always random.
You could give each villager a random generic data structure.
Or you could use locks.
In this case you would need 10 data structures, five of which would be variables called Lock 1- 5 and the others can be any data type, but would be numbered 1-5 as well.
My games rarely have the data and locks in a neat order like this.
Whenever a villager wants to use a data type they cycle through a lock and find one thatâs free.
It then locks the lock and uses the data storage associated to the data number. When itâs done with the data it frees the lock and finishes.
You need to make sure that when the lock is set each event has a separate Id.
In the example below the eventâs Id is 1. Itâs essential that all the lock operations set or check for the eventâs given id. (Unless the lock is being turned off, then it should be set to 0.)
And thatâs only with two of the code for two locks.
Thatâs great but why should I bother?
Simply, the more variables you are using in the game the harder it is to keep track of what was used for what. The more you can use a few variables the less you will have to remember when you go to debug.
Just remember that if you reuse idâs on a map you will end up with conflicts.
See my code:
The code I used for this tutorial can be downloaded at https://github.com/kylehoffmann/RPGMaker_Tutorials
You will need to download all of the files and the folder RPGMaker MV Tutorial Base as well as map001.json and System.json from âTutorial - Improving Data Scopeâ.
Take map001.json and System.json, then replace the file of the same name in the folder  âRPGMaker MV Tutorial\dataâ to see my code in your RPGMaker.
Note: I added an event that wasnât covered in the demo. This is to make the player visible. You wonât need that event in a normal game.
Index













