Think carefully about which event code needs to be executed. For instance an
AI can probably update his action every second rather then every step. With
a room speed of 30 this is a huge optimalisation. But even having something
being calculated every other step saves 50% of your coding already. Just move
the code from the step event to an alarm event and at the end of the alarm event
set the alarm to 2 steps.
You should avoid using the step event as much as possible. The same goes for
the drawing event. Note that the drawing event is executed every step for every
view you use. So if you have 3 views and a room speed of 60 that means that
your code is executed 180 times per second. Drawing goes reasonably fast within
GM6 but as the code is being executed very often you still need to try and optimalise
it. If something only needs to be drawn in view0 then use: if view_current=0
then draw_sprite(sprite,image,x,y); this way you save 66% of the drawing event
by not drwawing this within the other three views.
Disable/destroy objects
Game Maker has the ability to temporary disable an object. That means that
no event of the object will be executed any longer. This saves you drastically
on processing times. Also you can easely destroy all objects that leave the
room. These objects stay existing and will keep performing events and even
if you still need them later on in the game you can disable several actions
for them when not visible. For instance an object outside the screen does not
need to be visible. So you can disable visible which disables the drawing event
of the object. Also you can disable the AI of the object in most cases as most
of the time you do not need to have units being busy which you won't meet for
an additional half hour. For instance in a platform game you don't need to have
the AI work from left to right and back again until you are close enough to
them. Watch out with disabling all objects outside the view/room though and
make sure you do not accidently disable for instance a controller object. Also
note that functions to count objects will not count disabled objects. So if
you end the game when there are no longer any objects you need a special method
for detecting this as disabled objects are not recognised.
Rooms
As has been discussed before, the drawing code is executed for every view you
have defined in your view. So try to limit the number of views. Also it is logical
that if you keep the room_speed down that you will have less problems with lag
within your game as less code needs to be processed per step. You can also make
your games go faster by using small rooms. The less objects that are in a room
the faster the game goes. You can quite often devide a level up into multiple
rooms. With a bit of good timing you can even program it to slide to the
next room and have the new room look the same so you do not notice that you
enter the new room. This can for instance be done by having a long corridor
which you have both in the first and second room. With good timing the player
will never notice it switched rooms. Also you can for instance disable the drawing
of a background color if you are using a view. This saves you another bit of
drawing work. Also you can easely use tiles instead of objects for things like
ground. In that case you only need to add objects to the borders of blocks rather
then fill the entire block with objects as well. The less objects there are
the faster the game goes.
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:38
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:53
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:23
my sirname is coidling! =)
Marlow posted at 2005-07-16 19:59:25
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.
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
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:22
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.