Simon Donkers
Game Maker
Home > GameMaker > Articles > Coding efficiency > Page 2

GML

There are many different programming methods accepted within GML. There are some minor speed differences between them however none real noteworthy differences. The big speed differences are within inefficient coding methods. One of the most common examples is the switch statement. Take as example:

 Bad example code
if a=b then
{
        //do this
}
else
{
        if a=c then
        {
                //do this
        }
        else
        {
                if a=d then
                {
                        //do this
                }
                else
                {
                        //do default action
                }
        }
}

This code can be rewritten as:

 Good example of GML code
switch a
{
        case b: //do this
        break;
        case C: //do this
        break;
        case d: //do this
        break;
        default//do this
        break;
}

Notice that this code only uses 2 brackets instead of 12 which makes errors with non matching brackets a lot less likely. Also the last block of code is far easier to read once you get used to it. Because now you can simply see that the code for d will be executed if a=d (provided a is not b and not c). For the previous block of code you first needed to check 3 if statements to find this out.

 

An other thing that makes coding far more efficient is using indentation. You see that within my example of the if statements that you can directly see which loop is related to which statement because of this indentation. Having good indentation makes reading code a lot more easier. The same goes for clear use of resource naming. This means no sprite0 but use spr_player for instance. Note that you do not need to use spr_ but you can develop your own custom prefix system but stick to it. This makes reading code a lot more easier for both you and everyone else who will read through your code and this doesn't stop with using resource names. Also do the same thing for variable names. Make it that a variable name clearly represents what the value is.

An other thing to start learning is using script only variables.. These are handles faster then local variables and even more faster then global variables. For many cases you also only need a variable temporary to place a value in it and you do not need te rerefence to it in the future. Using this variable only for this script will make sure that the memory used for the variable is cleared at the end of the script.

An other thing to take in mind is the speed of each function. Certain functions are faster then other. I am not going to post here a list with all functions and there speed. However you can find some info on this subject at the thread functions to avoid and GM functions measured for execution speed within the GMC. Take these things in mind when programming.Of course there are also many cases in which coding contains useless statements. For instance string('5') is a useless command. You do not need to convert a string to a string. An other thing is not using multiple steps that can be merged.

 Bad example of code
a=x;
b=y;
c=a+b*2;
show_message(string(c));

Which can be written as:

 Good example of code
show_message(string(x+y*2));

And this is a simple example but the same goes for many different things. Instead of using image_index=direction/360*image_number you can also know that you have 36 images so use: image_index=direction/10; There are many cases in which coding can be removed to get a speed increase. Note that using multiple lines is a good way to keep code easy to read. So choose a balans between speed optimalisation and readability. For instance a code like the following shows you've gone to far in optimising and it's affecting readability.

 Bad example of code
dist = distance_to_object(instance_nearest(x,y,obj_bullet));

An other example is to store results of complex calculations. For instance the function to get your IP adress is quite slow. So instead of getting your IP adress every step you can also get the IP adress within the create event and store it in a variable. Also for instance if you have something you refer to often within a script. Get the value only once and put it in a script only variable so it can be easely accessed. For instance string_height_ext is a very slow function for very large blocks of text. But you can easely store that value in a variable so you only need to calculate it once. There are many methods of trying to limit the execution of slow coding, experiment a bit with this.

1 2 3 4

Loading...