Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1] 2   Go Down

Author Topic: Great Game Maker Wall Optimization Script  (Read 8492 times)

0 Members and 1 Guest are viewing this topic.

Koh

Great Game Maker Wall Optimization Script
« on: September 28, 2012, 11:22:01 am »
  • Tamer Koh
  • *
  • Reputation: +6/-0
  • Offline Offline
  • Gender: Male
  • Posts: 980
After reading about how to optimize Game Maker games to make them run faster, I decided to try out one method mentioned before, to combine all of the Wall objects into one sprite using a surface, then deleting them all, and creating one big wall, it worked out TREMENDOUSLY.  So I thought it would be useful for me to share my script with everyone.  It's not hard to do of course, but it could save time, and I recommend using it with every game that's bound to have a lot of Wall objects.  You call this script in the Room Creation Code. 

Code: [Select]
var; wallsurface=0; w=0;  //Variables for the Surface on which all the wall sprites will be put on, and the New Wall object that will be made.
wallsurface=surface_create(room_width,room_height+1);  //Prepare a surface as big as the room, adding an extra pixel vertically so we can have transparency.

surface_set_target(wallsurface);  //Prepare to draw on the surface.
draw_clear(c_white);  //Clear the entire surface with the desired transparent color.
with objWall{  //With all of the walls in the room...
    if object_index==objWall{  //If it's not inheriting from anything (you may want to handle slopes with their own combined slope object, for example...
        draw_sprite(sprite_index,-1,x,y);  //Draw their sprite on the surface at their location.
        instance_destroy();  //Then delete theirself.
    }
}
if sprite_exists(global.wallopsprite){sprite_delete(global.wallopsprite);}  //If there was a combined wall surface sprite made before, get rid of it.  This is crucial you store this, as these will build up if you don't get rid of them, and can crash the game.
global.wallopsprite=sprite_create_from_surface(wallsurface,0,0,surface_get_width(wallsurface),surface_get_height(wallsurface),true,false,0,0);  //Make a new sprite from the surface made of all the Wall objects.
w=instance_create(0,0,objWall);  //Make a brand new Wall object at the top left of the room;
w.sprite_index=global.wallopsprite;  //Set its sprite to the sprite made from the surface.
w.mask_index=w.sprite_index;  //Set its mask to its sprite.
surface_free(wallsurface);  //Free up the memory from that surface.
surface_reset_target();  //Reset the drawing location.
It's not much, but it's  effect on a game with a lot of Wall objects like my game is TREMENDOUS.  I have no lag whatsoever now.  You are all free to use this :).
Logged
  • Megaclipse Games
Re: Great Game Maker Wall Optimization Script
« Reply #1 on: September 28, 2012, 12:22:29 pm »
  • 笑い男
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 2124
A very nice idea!
Logged

この世に悪があるとすれば、それは人の心だ
  • .hack//The World
Re: Great Game Maker Wall Optimization Script
« Reply #2 on: September 28, 2012, 12:29:38 pm »
  • *
  • Reputation: +10/-0
  • Offline Offline
  • Gender: Female
  • Posts: 1469
I'd never considered this optimisation, and the funny thing is I use it all the time in other languages! The only difference being I start with a single image in the first place. Very impressive work, especially for so few lines of code.

A suggestion, perhaps have this code in the 'Create' event of an object called 'wallOptimiser'. Then people can just add this object into a room, and not have to copy and paste the code every time. Would make life much easier for the less capable.
Logged
  • Elliott Parkinson

Koh

Re: Great Game Maker Wall Optimization Script
« Reply #3 on: September 28, 2012, 01:36:49 pm »
  • Tamer Koh
  • *
  • Reputation: +6/-0
  • Offline Offline
  • Gender: Male
  • Posts: 980
I'd never considered this optimisation, and the funny thing is I use it all the time in other languages! The only difference being I start with a single image in the first place. Very impressive work, especially for so few lines of code.

A suggestion, perhaps have this code in the 'Create' event of an object called 'wallOptimiser'. Then people can just add this object into a room, and not have to copy and paste the code every time. Would make life much easier for the less capable.
I actually put it in a script, and just call the script in each Room's Creation code :).  One less object floating around.
« Last Edit: September 28, 2012, 01:46:40 pm by dlbrooks33 »
Logged
  • Megaclipse Games
Re: Great Game Maker Wall Optimization Script
« Reply #4 on: September 28, 2012, 02:01:56 pm »
  • *
  • Reputation: +8/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6604
I like your implementation. Having all the wall objects have their collisions defined by a sprite created from a surface would also be easier to check for under Game Maker's existing collision system. Thanks for posting the code.
Logged

Koh

Re: Great Game Maker Wall Optimization Script
« Reply #5 on: September 28, 2012, 02:28:53 pm »
  • Tamer Koh
  • *
  • Reputation: +6/-0
  • Offline Offline
  • Gender: Male
  • Posts: 980
Thanks!  Someone over at 64 digits brought to my attention, though, that this would cause problems for lower-end computers if the room size were to ever be something larger than 2048x2048.  What I would recommend, then, is breaking it up into 4 chunks of 512x512 or 256x256 so.  I may post an alternate version that does this later.  I'll also post a version that does the same for tiles, where it'll take all the tiles and combine them into one big tile.
Logged
  • Megaclipse Games

Zhello

N.A.D.I.
Re: Great Game Maker Wall Optimization Script
« Reply #6 on: September 28, 2012, 04:42:47 pm »
  • L'homme avec beaucoup de noms.
  • *
  • Reputation: +6/-1
  • Offline Offline
  • Gender: Male
  • Posts: 925
Will this work with diagnol walls too?

Also this can be place in a presistent control object or something?
Logged
The IT Guy
Intermediate Coder
Linux is a wonderful thing
~Linkwolf48/Gumstone1080~

The Legend of Zelda - New Beginnings

http://zfgc.com/forum/index.php?topic=34986.0
1.6 Demo -
https://www.dropbox.com/s/56km0iadaras56g/LoZNB%20Game%20Folder.rar?dl=0


Side Projects:

Dragon Diary - Cybernetic Threat
Story: http://wiki.zfgc.com/Cybernetic_Threat

Quote
Aero88
ZFGC is like the Hotel California.  You can come in but you can never leave!

devianart: http://linkwolf48.deviantart.com/

Koh

Re: Great Game Maker Wall Optimization Script
« Reply #7 on: September 29, 2012, 03:32:57 am »
  • Tamer Koh
  • *
  • Reputation: +6/-0
  • Offline Offline
  • Gender: Male
  • Posts: 980
Will this work with diagnol walls too?

Also this can be place in a presistent control object or something?
It could be done with an object that's in every room in the Room Start Event, or it could be in the room Creation Code.  But it's not something that is called every frame.

After having friends test this, I realize there is no need to make a separate script that divides this into chunks, as this script is running perfectly fine even on a computer that only has 1.5 GB of RAM.  This script would probably not be the best way to optimize walls in a REALLY large room, probably bigger than 4800x4800, however, this should be fine for things smaller.  Also, as mentioned, the surface after used is deleted right afterwards (As are the previous combined sprites made) so it's not lingering in memory.
Logged
  • Megaclipse Games
Re: Great Game Maker Wall Optimization Script
« Reply #8 on: September 29, 2012, 04:14:38 am »
  • AKA "Micah DS"
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1347
Aren't surfaces amazing? They are by far the best thing that ever happened to Game Maker, in my opinion. Heck, I've used surfaces to project simple real-time 3d shadows and there are tons of other things you can use them for. I can't believe I didn't think of this. It's so simple and yet very useful. Thank you for bringing this to my attention, dlbrooks33. Great optimization.
Logged
  • My Music
Re: Great Game Maker Wall Optimization Script
« Reply #9 on: September 30, 2012, 10:31:14 am »
  • *
  • Reputation: +16/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1633
Interesting. I always figured using more objects without detailed collision checking would be more efficient than one huge sprite with detailed collision checking. (Still a little sceptical though).
« Last Edit: September 30, 2012, 11:32:07 am by Martijn dh »
Logged

thestig

Re: Great Game Maker Wall Optimization Script
« Reply #10 on: September 30, 2012, 01:41:02 pm »
Should try documenting this on the wiki
Logged
Re: Great Game Maker Wall Optimization Script
« Reply #11 on: September 30, 2012, 04:52:06 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 57
Interesting. I always figured using more objects without detailed collision checking would be more efficient than one huge sprite with detailed collision checking. (Still a little sceptical though).

 :huh:
Logged
Re: Great Game Maker Wall Optimization Script
« Reply #12 on: September 30, 2012, 05:01:00 pm »
  • *
  • Reputation: +16/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1633
What part of my comment do you want me to explain?
I've always gone under the assumption that precise collision checking requires more processing power then let's say several checks using just bounding boxes. So, lets say I have about 25 wall objects in my room (all rectangles) and the room dimensions are 320x320. I'm still a little sceptical if using a compounded sprite will improve performance.
Logged

thestig

Re: Great Game Maker Wall Optimization Script
« Reply #13 on: September 30, 2012, 05:16:34 pm »
It's true though. For pixel-perfect collision checking, you're essentially doing bounds checking at the most exact level you can get in a 2D plane. Where as bounding boxes, you just do some equation to check certain coordinates. Take your pick, doing bounds checking multiple times for one event or passing coordinates for an equation?

That also depends on what sort of bounds checking you're doing. Like if you use the Separating Axis Theorem and do some sweep testing, that might (depending on the implementation) be faster or slower, but with very good precision than bounding boxes.
Logged
Re: Great Game Maker Wall Optimization Script
« Reply #14 on: September 30, 2012, 05:19:50 pm »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Posts: 728
I did something like this when I was working on the GM version of LA. Except I used a tool I created to plot out simple rectangles and triangles. Those then were drawn to a surface and used as a mask that provided the room collision. I BELIEVE it was a more efficient solution (for just one layer at least), although it has been years since then and my memory might be fuzzy. I'm sure there's some trade offs somewhere. It would have to be tested.
Logged
  • Pyxosoft
Re: Great Game Maker Wall Optimization Script
« Reply #15 on: September 30, 2012, 06:00:42 pm »
  • *
  • Reputation: +16/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1633
Here are some numbers:
I used a gamemaker room sized 1280x960 split into three subrooms. Everything outside an active subroom is always disabled. In one subroom sized 1280x520 the game runs at a solid 30 fps while containing 90 wall objects (both rectangles and triangles) and only 3 enemies. To worsen things I've increased the gamespeed to 60fps (which it could handle without problems) and added 13 enemies to drop the perform to, again, a solid 30 fps.

Using brooks' methode without adjustements the performance increases to 30/31 fps.
Using brooks' methode where the compounded sprite is not 1280x960, but just 1280x520 the perfomance increases to 31/33 with spikes between 29/35.

My conclusion would be that it at least doesn't make things worse if you keep the rooms decently sized. I might experiment a little further next weekend.
Logged

Koh

Re: Great Game Maker Wall Optimization Script
« Reply #16 on: September 30, 2012, 06:41:55 pm »
  • Tamer Koh
  • *
  • Reputation: +6/-0
  • Offline Offline
  • Gender: Male
  • Posts: 980
Here are some numbers:
I used a gamemaker room sized 1280x960 split into three subrooms. Everything outside an active subroom is always disabled. In one subroom sized 1280x520 the game runs at a solid 30 fps while containing 90 wall objects (both rectangles and triangles) and only 3 enemies. To worsen things I've increased the gamespeed to 60fps (which it could handle without problems) and added 13 enemies to drop the perform to, again, a solid 30 fps.

Using brooks' methode without adjustements the performance increases to 30/31 fps.
Using brooks' methode where the compounded sprite is not 1280x960, but just 1280x520 the perfomance increases to 31/33 with spikes between 29/35.

My conclusion would be that it at least doesn't make things worse if you keep the rooms decently sized. I might experiment a little further next weekend.
Thanks for performing an extensive test.  The way I tested it was just using it in my game immediately, because I started getting lag in my biggest rooms before I did (1600x1152 and 640x2304), but now I don't.  It probably isn't necessary to do in a really small room (anything less than something like 256x256) unless you are already having performance issues and are looking for something to optimize.
Logged
  • Megaclipse Games
Re: Great Game Maker Wall Optimization Script
« Reply #17 on: September 30, 2012, 07:40:26 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 57
What part of my comment do you want me to explain?
I've always gone under the assumption that precise collision checking requires more processing power then let's say several checks using just bounding boxes. So, lets say I have about 25 wall objects in my room (all rectangles) and the room dimensions are 320x320. I'm still a little sceptical if using a compounded sprite will improve performance.

Well I've always thought that the less objects the better (If you had a look at my original project you would see  XD). The detailed collision checking is an added plus, but I am not sure what you mean by "unprecise collision checking". I do not understand how gamemaker checks collisions, but most of the time, when I was using it, I just tested collisions using masks. Of course, there is always the possibility of making your own bounding box variables and using that but I cannot say whether or not it is any less precise.
Logged
Re: Great Game Maker Wall Optimization Script
« Reply #18 on: October 01, 2012, 07:04:05 am »
  • AKA "Micah DS"
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1347
The reason why I'd choose surfaces over objects for wall collisions is because it should use less memory. Objects have too much unnecessary fluff for simple collision checking, even if there is no code called upon within them. In addition, having more objects increases the overall size of the game and the loading time. Making good use of surfaces can cut down a lot on these things.
Logged
  • My Music

Antidote

>.>
Re: Great Game Maker Wall Optimization Script
« Reply #19 on: October 02, 2012, 05:24:46 pm »
  • In all seriousness who's serious?
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1485
surfaces are native to the GPU while objects are on the CPU, soo while surfaces ARE faster you still have to be careful about resources :P
Logged
  • Axiomatic Data Laboratories
Pages: [1] 2   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.075 seconds with 78 queries.

anything