Simon Donkers's Game Maker site siteSimon Donkers's Game Maker site
Version: All Read: 17743 Article
Filesize: 16 kb Creative Commons License

Coding efficiency

1/52/53/54/55/5 (41 votes)

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.

[ Go to page [1] [2] [3] [4] ]

User comments

 Timmins posted at 2005-02-15 20:55:14Quote
Very nice article, Simon. I learned a couple of new things, and thanks for writing this. You use "and" too many times inside your written piece, but I read through it.

Can't wait to see more articles,

Timmins
 Scotley posted at 2005-03-22 09:45:38Quote
Absolutely ripper. Have used c# a bit so I realise most of this, but good to get a refresher! Awesome work Simon.
 Vishal posted at 2005-04-05 16:07:53Quote
Great tips and a great game(Bacteria 2), probably one of the best games made in GM.
 Red man can posted at 2005-06-24 09:12:23Quote

my sirname is coidling! =)
 Marlow posted at 2005-07-16 19:59:25Quote
wonderful...just the type of information I needed to read to make my programming less sloppy and actually easier to write... I especially enjoyed your material about using the alarm event rather than the step event, view usage, and parent relationships... these will help me out the most. I look forward to more of your articles... Thank you.
 Isaac Mattox posted at 2005-10-02 23:40:12Quote
Looking for advise on Ai development for a simple game of checkers. All the movements work properly but I want Ai to play against the computer. Any suggestions? alberley@yahoo.com
 Elmernite posted at 2005-10-31 14:15:37Quote
Very nice! I really like it. It's a great articles. This will really help a lot of people (me included). Your a help to the GM world.
 eleet posted at 2005-12-08 00:38:22Quote
For you question about the game checkers, it is not a simple thing to make a AI for checkers, in fact no one in the world yet found a way to make a perfect AI for checkers there are just to many ways to move and play. Also, if you want the AI to play the AI then all you have to do is make a simple script that says whois turn it is to play and make the AI the player 1 and player 2. You can also have a loop set so they will play the game over and over for millions of times. But trust me AI for that type of game is in no way ez to make.
 Question posted at 2006-03-05 03:32:22Quote
Question:Why dont u include ur sites?
 Jeff van Hees posted at 2006-08-21 10:33:20Quote
Very nice article Simon, just like your others! I think this really help people with their problems. Thanks for writing this...
 mrme posted at 2008-08-09 18:17:18Quote
thank you for this article I didn't know about the "Never use moving solid objects"
rule and i kept having problems with my game.
 Game Detour posted at 2009-05-06 01:30:29Quote
Thanks!!!

m/
 jasperdj posted at 2009-11-14 13:44:33Quote
Soms things i knew some thing i didnīt knew. This i quite a qood example, i is something every advanced game maker user should know!
 detour posted at 2010-01-02 14:50:44Quote
how exactly do you disable the object during the game

Reply

Name:
Site: