A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: realistic game optimization - small code optimizations don't matter that much, right?

  1. #1
    Senior Member
    Join Date
    Jul 2008
    Posts
    418

    realistic game optimization - small code optimizations don't matter that much, right?

    Hello,

    When I google "game optimization" i get a lot of serious (and complicated) articles about all kinds of complicated techniques, like multithreading etc.
    when i google "game optimization" as3 , all i get is: multification by 0.5 is faster than division by 2, and stuff like that. I suppose, that when you're running a function 85% of the time, these minor optimizations can make a difference.
    But obviously, the, more 'high level' solutions help a lot more, right? things like using bitmaps instead of movieclips, etc.
    anyway. I've optimized my code pretty much in terms of minor optimization, but the point is, My game sometimes features 100 moving objects on screen, that sometimes change collor, and some of them change rotation, and i need to maintain AT LEAST 30 fps. so this multiplication by 0.5 won't help me much on that ground.

    I have googled alot, but i can't find anything except for people saying: use bitmaps. now i believe them that this might help (sometimes). But i dont understand how/when to use bitmaps, to get the most effect. On top of that, i heard using bitmaps, after you've used mcs, will change your entire design of code, which obviously, is not something I want.

    Basically: what are the best ways to optimize my intensive game, besides Bitmapping, and how/when exactly to use/do them?

    ps. I actually have problems iwht my game in terms of performance, so this is not an idle question.


    Edit:
    Questions that still remain:


    i dont know alot about hardware, and all that. but I do know that, the highest % my cpu usage gets to in my game is 50, and sometimes goes over 55, but my fps is still below 20. why doesn't the cpu go to 100%, to get my fps higher?

    on my macbook, i dont even have to create enemies, for the fps to drop to 30fps, and then when the player enters screen to 25fps. when the enemies enter, i can just forget it, because the fps turns to 10. Should i just give up trying to improve it enoguh so it can run on macBook? only 5% of the people have mac anyways.
    How come the mac has such a big trouble, with only one active mc on stage?

    My mcs have i think 1 vector in them. something like that. they're all rounds/circles/triangles/rectangles, etc. and they have an inside and an outside (the outside is just a very thick line around it (the one that is automattically added, i set it to a high thickness). and it changes colors, sometimes, so not all instances of the mc's, have the same color.
    my graphics will not have much effect will they? and, will cacheAsBitmap help here?
    Last edited by omniscient232; 03-09-2009 at 10:35 AM.

  2. #2
    Member
    Join Date
    Jan 2009
    Posts
    90
    The first thing is to identify what functions are eating up most of the time in your game. I believe the full version of FlexBuilder has a profiler that will tell you this, but otherwise you can use the timer function.

    For example in the game I'm working on, I made a Profiling class. At the start of commonly called functions I request a new profile object and at the end of the function I tell the profile object I'm done. The profile object registers with a singleton so keeps track of: how many times a function was called, what the average length of execution was, what the peak execution time was and the average length of execution per frame. At any point in the game I could hit "ESC" and it dump out the stats for all functions to trace(). (Obviously there's an overhead to profiling, but I'll pull this code out before release)

    This confirmed what I suspected were the slow areas, revealed some slow parts I didn't suspect, and told me not to worry about some things I had been concerned about.

    The techniques you'll find by googling are useful, but probably not the lowest hanging fruit. The lowest hanging fruit is going to be something specific to your game. It might be how you do collision detection, pathfinding, sound or graphic bells and whistles.

    100 moving objects shouldn't be a problem at all, but it may be something else that those objects are doing which is causing a problem.

  3. #3
    Funkalicious TOdorus's Avatar
    Join Date
    Nov 2006
    Location
    Nijmegen, Netherlands
    Posts
    697
    To be honest, I have to say that blitting gives a far more structured code than using movieclips. It takes some getting used to, but now that I've grown accustomed to it, it actually makes more sense then movieclips. You don't need to keep track of depths, you just have to sort an array. You don't need to keep track if the movieclip is on stage (so no addChild() removeChild, or attachMovieClip(), removeMovieClip()) you just check your character is onscreen or offscreen. It might be worth your while to learn about BitmapData.

    Ofcourse you asked for ways to optimize without using bitmaps. The next thing I can think of is spatial indexing, with the holy grail being the quadtree (but I've heard something about spheretrees too), but a static grid should already cut off some checks. You break up the games space in tiles and for example only do collision checks against the stuff in the same tile ande the ones surrounding it.

    That last thing is actually what most higher level optimizing is all about: try to avoid checks. This isn't always possible, but you should have a method where a check returns true most of the time. If it doesn't, that means you need to find a way to reduce the number of cases that will return false fed to the check in the first place. Sorry if that all didn't really make sense, I really can't seem to find the right words.

  4. #4
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    interesting justKevin. and i'll look into flexBuilder. will flexBuilder work conveniently with flash? I don't use flex.

    TOdorus. I will learn about BitmapData, and ill maybe put it in my next game, but is it a good idea to completely change my code, i wonder?
    I don't have collision checking at all in my game, except for the player and the playerBullet, but that shouldn't eat up much. I work with potentialFunctions, and i have already made a system, with which i can set the distance between objects, if the function will be executed at all. I know this isn't as good as a quad tree, but it does help ALOT. but only in cpu execution.

    Now i dont know alot about hardware, and all that. but I do know that, the highest % my cpu usage gets to in my game is 50, and sometimes goes over 55, with one exeption being, when a new flock of enemies is created, it jumps to 80 then, for about 0.5 secs or less(there is a delay in the cpu monitor so i guess that will be less than 0.5 secs?)
    Anyway, at this point, my fps is still below 20. In this case, my gpu is the bottleneck, right? so AI optimization won't work that much will it?

    well, it will still, because when i completely remove AI, it jumps back to 60 fps. this is good news, for me in the sence that i have a good idea on how to optimize my AI, and not such a good idea on Bitmap optimization, but i dont understand at all how come my cpu isn't running at 100%, while my fps is 50%, and the cpu is the bottleneck. does this have to do with multicores? if this is so, how do I utilize both cores, if possible at all?

    And will bitmapping improve my performance AT ALL, at this point? I read from proffessional JAVA/C++ optimizers, that ONLY optimizing the bottleneck will help, and the rest will give a 0% fps increase. is this the same with flash? I read that flash player works a slightly different way, and doesnt have parrallel processing, which would suggest that graphics processing and other processing is in the same category withing the flash player?

    Tell me if i'm worng, because i probably am.

    edit: on my macbook, i dont even have to create enemies, for the fps to drop to 30fps, and then when the player enters screen to 25fps. when the enemies enter, i can just forget it, because the fps turns to 10. Should i just give up trying to improve it enoguh so it can run on macBook? only 5% of the people have mac anyways. i still dont understand why that macbook has such a big trouble with only so little on screen.
    Last edited by omniscient232; 03-08-2009 at 04:57 PM.

  5. #5
    Funkalicious TOdorus's Avatar
    Join Date
    Nov 2006
    Location
    Nijmegen, Netherlands
    Posts
    697
    I wouldn't change my engine either. Just try and set a few classes up with your next game.

    You are aware that Flash uses the cpu for graphics, especially vector graphics? So an increase in cpu usage can also be because of more movieclips. Ofcourse, if your movieclips don't change (animate) a lot and don't rotate you can always use cache as bitmap to get some boost. The idea is the same, cache a graphic in memory so the cpu only needs to calculate it once. I am curious how you've organised your AI, because I found I can squize quite a lot out of Flash in terms of code-execution. Well, it well never be Java, but the bottleneck is really at the graphics end. Flash just doesn't really have all the bells and whistles to support high-end graphics.

    Since this is still all bitmaptalk it doesn't really help you much. Have you considered distributing your AI calculations? Say with every enemy you create you assign him to group 1 or 2. On the first frame only group 1 executes it's AI routine, on the second frame only group 2, on the first frame group 1 again and so on.

  6. #6
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    "You are aware that Flash uses the cpu for graphics, especially vector graphics?"
    No, i wasn't. Do you mean ALL graphics? You mean the graphics card isn't even used, in a flash app?
    "Well, it well never be Java, but the bottleneck is really at the graphics end"
    My mcs have i think 1 vector in them. something like that. they're all rounds/circles/triangles/rectangles, etc. and they have an inside and an outside (the outside is just a very thick line around it (the one that is automattically added, i set it to a high thickness). and it changes colors, sometimes, so not all instances of the mc's, have the same color.
    my graphics will not have much effect will they? and, will cacheAsBitmap help here?
    also, about cacheAsBitmap: will EVERY instance of the mc be individually cached, or does flash have a system, for cacheing all the mc's that are the same (so in my case, that still have the same color) as one BitmapData?

    distribution:
    Yes i have considered it. Is it a good idea? I dont know.
    How would this speed up my game? they all need to be executed, so what does it help to spread them. or is it just that all of them will be executed less frequently (which would be the same, as setting the timer.delay twice as high?)

  7. #7
    Junior Member
    Join Date
    Jun 2008
    Posts
    18
    You mean the graphics card isn't even used, in a flash app?
    Flash does not interact with the graphics card directly.

  8. #8
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    well, does an swf with relatively heavy graphics run faster if it has a graphics card?

    ps. about blitting, (and maybe double buffering?), are there any open source classes, for this? or at least some kind of explanation, of how to do it in detail?
    Last edited by omniscient232; 03-09-2009 at 02:45 AM.

  9. #9
    Funkalicious TOdorus's Avatar
    Join Date
    Nov 2006
    Location
    Nijmegen, Netherlands
    Posts
    697
    Quote Originally Posted by omniscient232 View Post
    also, about cacheAsBitmap: will EVERY instance of the mc be individually cached, or does flash have a system, for cacheing all the mc's that are the same (so in my case, that still have the same color) as one BitmapData?
    To be honest I can't tell really, as I never had to use it. I'd say experiment with instances of one item.

    Quote Originally Posted by omniscient232 View Post
    distribution:
    Yes i have considered it. Is it a good idea? I dont know.
    How would this speed up my game? they all need to be executed, so what does it help to spread them. or is it just that all of them will be executed less frequently (which would be the same, as setting the timer.delay twice as high?)
    I think you're missing the point. They will be executed less frequently but not at the same time. So instead of a full load of AI each frame you get half a load of AI. Like you implied this means the AI's reaction time will not be 1/60 of a second but 1/30 of a second, for example.


    Quote Originally Posted by omniscient232 View Post
    well, does an swf with relatively heavy graphics run faster if it has a graphics card?
    Well every pc has to have a graphics card, so not sure what you mean here. Flash relies on the cpu to calculate what to draw to the screen and not the gpu. That information is sent to the graphics card to drawn to the screen.


    Quote Originally Posted by omniscient232 View Post
    ps. about blitting, (and maybe double buffering?), are there any open source classes, for this? or at least some kind of explanation, of how to do it in detail?
    Googles 2nd hit on: as3 blitting

  10. #10
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    I think you're missing the point. They will be executed less frequently but not at the same time. So instead of a full load of AI each frame you get half a load of AI. Like you implied this means the AI's reaction time will not be 1/60 of a second but 1/30 of a second, for example.
    I understand. i'm just asking: is having 1 timer of 100 milliseconds, that does half of the AI one time, and the next time the other half. (so both are executed 5 times every sec), is that faster, than having one timer of 200 milliseconds, that executes all the AI, in which case, they are both still executed 5 times every sec. Would this matter?

    Well every pc has to have a graphics card, so not sure what you mean here. Flash relies on the cpu to calculate what to draw to the screen and not the gpu. That information is sent to the graphics card to drawn to the screen.
    I admit, the question was not well formulated. what I mean is: how much will a good graphics card help to run your swf witha good performance? so basically: will someone with a 8800GT run your swf faster, than someone with a 6200 GT, if the cpu and Ram, etc are the same.

    Questions that still remain:
    i dont know alot about hardware, and all that. but I do know that, the highest % my cpu usage gets to in my game is 50, and sometimes goes over 55, but my fps is still below 20. why doesn't the cpu go to 100%, to get my fps higher?

    on my macbook, i dont even have to create enemies, for the fps to drop to 30fps, and then when the player enters screen to 25fps. when the enemies enter, i can just forget it, because the fps turns to 10. Should i just give up trying to improve it enoguh so it can run on macBook? only 5% of the people have mac anyways.
    How come the mac has such a big trouble, with only one active mc on stage?

    My mcs have i think 1 vector in them. something like that. they're all rounds/circles/triangles/rectangles, etc. and they have an inside and an outside (the outside is just a very thick line around it (the one that is automattically added, i set it to a high thickness). and it changes colors, sometimes, so not all instances of the mc's, have the same color.
    my graphics will not have much effect will they? and, will cacheAsBitmap help here?
    Last edited by omniscient232; 03-09-2009 at 10:34 AM.

  11. #11
    Member
    Join Date
    Jan 2009
    Posts
    90
    Quote Originally Posted by omniscient232 View Post
    I admit, the question was not well formulated. what I mean is: how much will a good graphics card help to run your swf witha good performance? so basically: will someone with a 8800GT run your swf faster, than someone with a 6200 GT, if the cpu and Ram, etc are the same.
    No, they should run about the same. Flash Player 9 can't use GPU acceleration (that is, use the graphics chip to perform calculations). Flash Player 10 can use GPU acceleration in special cases, but in general will not.

  12. #12
    Funkalicious TOdorus's Avatar
    Join Date
    Nov 2006
    Location
    Nijmegen, Netherlands
    Posts
    697
    Quote Originally Posted by omniscient232 View Post
    I understand. i'm just asking: is having 1 timer of 100 milliseconds, that does half of the AI one time, and the next time the other half. (so both are executed 5 times every sec), is that faster, than having one timer of 200 milliseconds, that executes all the AI, in which case, they are both still executed 5 times every sec. Would this matter?
    The point is, that when your calculations between frames exceed the time in between each screen refresh, you'll get a framerate drop (as Flash first executes the actionscript and then draws to the screen), so you want to avoid peaks in processor usage. Let's say the amount of calculations Flash can proces per frame (bad way of saying it, but hey) is 100%. All of your enemies AI uses 120%, which will result in a frame drop everytime they're called. If you spread them out, you get 2 calls which use 60% each, so no frame drop. Same interval between AI calls, but less frame loss.

    As I said Flash only uses the cpu, and as justkevin says not the gpu. So the better the processer (and to some extent) and motherboard the better the performance. Ram is so plentifull these days, that I doubt you'll ever use it all before flooding the cpu. I'm trying that at the moment by really pushing it with bitmapdata's, but there seem to be some limits on a swf that aren't widely known.
    Last edited by TOdorus; 03-09-2009 at 12:14 PM. Reason: rushed

  13. #13
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    Thats clear. ok i get it.
    Is there a way to see cpu peaks? i know you can do it with task manager, but that one refreshes every 0.5 secs or so, so it wont recognize those peaks, right?

    It's ok to copy about 120 constants from other objects, so that in the AI it doesn't have to refernce to the objects, every AI cycle. or is this bad for some reason?
    does it slow down to check for vars, if you have about 120 vars in one obj? I can use an array, but it's a pretty intensive function that uses these vars, and arrays are slower than normal vars. is there a faster way?
    Last edited by omniscient232; 03-10-2009 at 10:30 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center