Condensing Variables

This short tutorial is a method I use in my RPG for containing a lot of game data in a single string. I don't know if it uses less memory or not, but I find it to be more convenient than pages full of variables. This tutorial does require that you have a bit of GML knowledge. In any case, here's what to do:

Setup
For this example, let's say you are making an RPG. In your RPG you have created a level up system and want to make it so that when the player has gained enough experience to go up a level, his stats are increased. The player can gain a maximum of 10 levels in your game, and for each level you want specific stats to be raised. You have the following attributes you are working with: Level //The Player's current level HP //The player's Hit Points MP //The player's Magic Points ATT //The player's Attack DEF //The player's Defence ACC //The player's Accuracy EVA //The player's Evasion What you will need to do is store the amounts that each stat will be increased by in a string, so the first thing you do is create your string variable. You call it "LevelUp". Now that you have your string variable decided, you have to decide how the data will be arranged inside it. You know that at higher levels the HP and MP will be increased by hundreds, and you know that the other four stats will be increased by tens. So you'll need the string to have 14 digits (3 for HP, 3 for MP, 2 for ATT, 2 for DEF, 2 for ACC, 2 for EVA).

LevelUp = "00000000000000";

Next, you decide that you will use a event to set the LevelUp string when a level is gained.

(Level) {   1: LevelUp = "00000000000000"; ; case 2: LevelUp = "02501502010502"; break; case 3: LevelUp = "05003002021004"; break; case 4: LevelUp = "07504504021506"; break; case 5: LevelUp = "10006004022008"; break; case 6: LevelUp = "12507506032510"; break; case 7: LevelUp = "15009006033012"; break; case 8: LevelUp = "17510508033514"; break; case 9: LevelUp = "20012008044016"; break; case 10: LevelUp = "22513510044518"; break; }

Extracting the data
Now, when your level up script has fired off, after the LevelUp variable is set with the switch event you will need to retrieve your data from the LevelUp string variable. This is how: HP = real(string_copy(LevelUp,1,3)); MP += real(string_copy(LevelUp,4,3)); ATT += real(string_copy(LevelUp,7,2)); DEF += real(string_copy(LevelUp,9,2)); ACC += real(string_copy(LevelUp,11,2)); EVA += real(string_copy(LevelUp,13,2));

What this is basically doing is breaking up your LevelUp string variable into six portions and adding them into the appropriate variables. For example, at level 9 the string is broken up and added in like this: 20012008044016

When you retrieve it, real converts the string into a number, and string_copy is returning a portion of our LevelUp string as given in it's arguments. The first argument in string_copy is the string you are returning from, which in this case is your LevelUp string variable. The second argument is what position in that string you are starting from (i.e. from where in the string you are returning from). The third argument is how far from the starting point you are going (i.e. where the portion ends that you are returning). So, in the level 9 string, if you were returning the HP value... // 20012008044016 HP = real(string_copy(LevelUp,1,3));

...then basically what you are doing is looking at position one in the string LevelUp, 2 0012008044016, and returning from there to 3 positions further in the string, 20 0 12008044016. So the value that is being returned is 200 12008044016. That value is then converted to a real number and added into the variable HP.