Avoiding pointless overhead
It's always a good idea to write less code especially if it means the program runs better. Less is more! While cleaning up my code base for Project Explore I noticed that there were numerous occasions where I was storing reals as strings due to the file parsing process. In response to this I created a 2nd pass script for variables being written to memory.
Normally stackmax would be passed in as a string instead of a real. It is always used as a real though which means that we would need to use real() every time we want to do any sort of math operation with the variable. Instead of being messy and unoptimized we can convert the string to a real while the variables are still being written to memory. In the code window shown below argument2 is the value connected to stackmax.
Now when we access stackmax we don't need to convert it from a string to a real. This saves us CPU time and some code space.
Breaking down data structures
Sometimes you'll find yourself using nested data structures. When using json_decode() you'll be given a map with at least one list in it depending on what it is you are parsing. In the code below you'll see that I use json_decode() to parse a string into the game as a map. Within this map is a list that contains additional lists in each of its indexes.
I could just leave it as it is and access the lists when needed via the map but that would be (once again) both a waste of memory and code space. Instead I take the list containing the other lists and copy it into a list of its own. The reason the list must be copied is because when you destroy the top-level map Game Maker will automatically clear all of its lower level data structures.
After doing all this we are able to replace the JSON string paired to "sprite_combat" with a list that has already been processed. So why did we go through all that trouble? Now that we have preprocessed this information we won't have to use json_decode() during runtime meaning once again that we will be using less memory and CPU time.