Game Optimization Tips

You must understand the Game Editor concepts, before post here.

Re: Game Optimization Tips

Postby Game A Gogo » Tue Dec 04, 2007 1:50 am

To reduce the file size, always consider about your images format, PNG and GIF are the most recommended to be used!
It has saved me about 10mb of space in one of my game! This also apply for music, I suggest Modules and OGG files

Try to always simplify your code, this will help for the CPU usage.

I'm not sure about this, but I think that this code can replace the distance(); thingy
Code: Select all
dist=((player2.x-player1.x)+(Player2.y-Player1.y))/2;

But I highly doubt it would work.
But if it does, I think it could also be faster then the distance() command.
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Game Optimization Tips

Postby pyrometal » Thu Dec 06, 2007 6:08 am

To answer that question... first that code is wrong, it should be:

Code: Select all
dist = sqrt(((player2.x-player1.x)^2)+((Player2.y-Player1.y)^2))


Also, there aren't ten thousand way to calculate distance between objects so I would be ready to bet that the distance() function in GE executes this very same piece of code for you instead of having to rewrite all of it. This would also mean that whichever you decide to use would have little or no difference on your game's fps.

I hope this helps somewhat... Maybe I should post some advice here too!

Anyway, cheers!

-- Pyrometal --
SPRITE WARRIOR:
Free, open-source & crossplatform pixel art editor (currently under construction).
User avatar
pyrometal
 
Posts: 706
Joined: Wed Nov 28, 2007 4:07 am
Location: Kingston, ON, Canada
Score: 86 Give a positive score

Re: Game Optimization Tips

Postby pyrometal » Thu Dec 06, 2007 4:21 pm

Everyone! We cannot let this topic die off like most of the others that are started by people, its too important! Keep posting your advice here if you have anything you believe could help anyone's situation. To do my part, I will post some advice about how you structure your conditional statements below.

The following 2 sets of code do the exact same thing but are structured differently one of them is more effective:

Code: Select all
if (condition1 == Option1  &  condition2 == Option1){
              - some instructions here}

         else if (condition1 == Option1  &  condition2 == Option2){
              - some instructions here}

         else if (condition1 == Option1  &  condition2 == Option3){
              - some instructions here}

         etc...


This example works but is not very effective since it checks "condition1" for the same value until it hits the combination it wants defined by condition 2.
Therefore the code could be rewritten the following way:

Code: Select all
if (condition1 == Option1){

               if (condition2 == Option1){
                     - some instructions here}

               else if (condition2 == Option1){
                     - some instructions here}

               else if (condition2 == Option1){
                     - some instructions here}

               etc...
               }


This method is a BETTER choice since it avoids checking for the same value over and over until it hit what it wants. In longer multiconditional sets, this could save you from doing up to 1/2 or more of the checks that the previous method would have had to do. The next piece of code is the best choice:

Code: Select all
if (condition1 == Option1){
               
               switch (condition2){

               case Option1:   {instructions}
                                      break;

               case Option2:   {instructions}
                                      break;

               case Option3:   {instructions}
                                      break;

                etc...
                }}


This method is probably the BEST choice to go at this problem since it only checks condition1 once and then makes the use of a switch/case statement to determine what to do from the value of condition2. Switch/Case is more effective than "if" in this case because it only acquires the value of condition2 once and then compares it instead of aquiring it every single time it makes a check. Thus Switch case saves even more processing power and should be used in this situation over the 2 other options.

However, note that "if" is often times more appropriate and effective at other tasks such as the following:

Code: Select all
if (A >= 1  &  A <= 10000)
               {instructions}


Using a Case statement here would definetly not be practical here as you would be checking a grand total of 10 000 values! lol (so don't use it there...)


Conclusively, as a rule of thumb, you should always select options that require the least loads and checks as possible to keep everything efficient. Always evaluate the possibilities and select the best opions before you start to code anything.

Anyway, that's all for now!

-- Pyrometal --
SPRITE WARRIOR:
Free, open-source & crossplatform pixel art editor (currently under construction).
User avatar
pyrometal
 
Posts: 706
Joined: Wed Nov 28, 2007 4:07 am
Location: Kingston, ON, Canada
Score: 86 Give a positive score

Re: Game Optimization Tips

Postby Fuzzy » Thu Dec 06, 2007 9:12 pm

pyrometal wrote:Everyone! We cannot let this topic die off like most of the others that are started by people, its too important! Keep posting your advice here if you have anything you believe could help anyone's situation. To do my part, I will post some advice about how you structure your conditional statements below.

The following 2 sets of code do the exact same thing but are structured differently one of them is more effective:

Code: Select all
if (condition1 == Option1  &&  condition2 == Option1){
              - some instructions here}

         else if (condition1 == Option1  &&  condition2 == Option2){
              - some instructions here}

         else if (condition1 == Option1  &&  condition2 == Option3){
              - some instructions here}

         etc...


-- Pyrometal --


Quite right. Lets keep this going!

However, in the same situation, if you are using 'or' instead of 'and', this works faster.


Code: Select all
if (condition1 == Option1  ||  condition2 == Option1){
              - some instructions here}

|| means 'or'.
What happens with || is that it checks the first condition and if its true, it skips the second, because its fulfilled the conditions for executing the instructions that follow. Therefor, if testing two elements, place the more important, or more likely one first.

Also, for this kind of comparison use && not &. & is bitwise 'and' while && is logical 'and'. Logical means it returns true or false, while bitwise means it combines numbers. Its something that i have to double check all the time as well.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Re: Game Optimization Tips

Postby pyrometal » Fri Dec 07, 2007 4:33 pm

Thank you for bringing up those points Fuzzy. It kinda slipped my mind for a second...

Another piece of advice, if you can avoid using the draw actor event, by all means do so (This was mentioned by marathon332 earlier but I want to stress importance). There is nothing worse than killing fps because an action is being repeated or checked unnecessarily before every frame. This applies especially for an actor checking conditions every frame to decide what to do next according to some algorithms... Instead, make it so this environment check only occur on specific events that may have triggered the need for actions. Once again it's all about being effective, keep that in mind.

Also, what I do when I create games with GE is using tons of activation regions. I usually create them in a consistent matrix pattern with all of them being the size of the resolution, all of them placed only where actors exist of course. By using this practice I never forget to include anything and only load the needed sections of the game.


Some things you should think about doing all the time:

-Out of Vision --> Destroy Actor (for projectiles and such)

-if (!transp) { DestroyActor(Event Actor);} (Note that this is a Draw Actor Event, use with only things that appear and start vanishing right away)

-Use Switch/Case/Default/Break for operations such as selecting a weapon (Usually weapons are set to integer values on a single variable)

-Do you know how to bit shift? If yes, use it, its a useful skill which can save quite a bit of memory (bit shifting implies using bits in a variable for many purposes, example, an unsigned char variable could be used to simulate 8 boolean values, saving yourself from creating 7 other variables. Watch out though, this process takes more processing power from the CPU since you need algorithms to deducewanted values afterwards...)

-Do you have one frame animations in your game? I don't know if this will help save processing power but you might want to try put their fps at 1 and pausing the animation. (Can someone tell me if this actually helps any?)

-By all means USE C as much as possible, that way you know exactly what the computer is doing and helps avoid uneeded codes that you cannot see when calling GE's preset functions.

-Making variable timers by combining Draw Actor with {var++;} and if(var == x) statements in a script is a good idea but ONLY when you need frame precision, otherwise use timers, they're more effective and don't uselessly recheck the if statement.

Anyway that's all for now!

-- Pyrometal --
SPRITE WARRIOR:
Free, open-source & crossplatform pixel art editor (currently under construction).
User avatar
pyrometal
 
Posts: 706
Joined: Wed Nov 28, 2007 4:07 am
Location: Kingston, ON, Canada
Score: 86 Give a positive score

Re: Game Optimization Tips

Postby pyrometal » Mon Dec 17, 2007 3:59 am

The subject is dying! Noooo!!! I will not let that happen, so I guess I should post more tips.

I'll address a problem concerning the making of puzzle games (static block type) with GE today. The reason is that I've had quite a few problems in the past in making these because I was using very ineffective methods. I will start by posting a list of "don'ts" then I'll explain so "dos". Note that some of this advice can easily be ported elsewhere into other types of games.

The DON'Ts list:

- AVOID AT ALL COST putting any code in your block actors. This will create a huge problem when you get numerous clones active at the same time, since you risk making tons of actors repeat code instructions uselessly and considerably lag the game.

- If your game depends on a block moving through "block mazes" with static grid positions, do not use Collide Actor event at all. There are far to many possiblities of screw ups, such as misallignment and workablity.

- DO NOT use Draw Actor event to check if the block positions need to be updated.

- Making something "tetris" like? Do not resort to "pseudo" methods of verify the conditions of elimination of the blocks (A little more info later)


The DOs list:

- Is your game based on a grid like basis? Use matrices!!! They are extremely effective. The way to make this work is to assign values to certain types of blocks, then make a number matrix holding all the positions and objects. For example, the player actor could be represented by a value of 1 and placed into the matrix at position (2,4). Then when the player presses a key (like the DOWN key to move) you can set algorithms to work with the values in the matrix and then refresh the matrix to illustrate the changes.

- I recommend using a central "control actor" which takes care of all the inputs needed in the game, this way, with your code centralized, you are far less prone to writing unecessary code, you can simplify variable systems by quite a bit, and you can also find the source of errors more quikly, since you will not of to check 10 different actors.

- MAKE FUNCTIONS! This is key to effective and clean programming (should have figured that out earlier...). This is true for all games, but is even more of a vital tool for puzzle game (and RPGs), since they are generally very structured games. Why use functions as an integral part of your coding? Why not!!! Seriously, they incredibly simplify your code and its versatility. In games such as puzzles, a RefreshGame(); custom function could be used instead of copy/pasting your code all over the place. Not only will you avoid errors, it makes file sizes smaller, and more effective in your routine calls, thus optimizing every aspect of your game.

- Do you really need a high fps for your puzzle game? If not, reduce it, be friendly with the CPU and it will be friendly too! (lol)

This is all the advice I can think up for now, so anyway... Cheers!

-- Pyrometal --
SPRITE WARRIOR:
Free, open-source & crossplatform pixel art editor (currently under construction).
User avatar
pyrometal
 
Posts: 706
Joined: Wed Nov 28, 2007 4:07 am
Location: Kingston, ON, Canada
Score: 86 Give a positive score

Re: Game Optimization Tips

Postby Fuzzy » Mon Dec 17, 2007 4:30 am

I wont let it die either.

I did some experimenting.

From what I have deduced is that GE calls the draw function for view. It then executes any code within itself and from there, asks each actor to do its draw event. (the draw function for view calls the draw function for each actor before it returns).

The short of it is that anything you put in draw event for view will happen before the code in the actor draw events. This is a good opportunity to calculate things for actors. Set things like global variables here. You'll avoid the problem of having a million clones do the same thing over and over each frame.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Re: Game Optimization Tips

Postby DST » Mon Apr 21, 2008 5:47 pm

Here's a couple:

Stick it to the player! Wall him in and make him deal with enemies. If you have many enemies that can be easily avoided by the player, then those enemies are just wasted cpu!

When you have code to handle an enemy's demise, don't put it in draw actor. The enemy can only be killed by player 1's attack anyway, so put
Code: Select all
if (health <= 0){do whatever}

in collision with player1's weapon.

This prevents it from checking every frame.
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score

Re: Game Optimization Tips

Postby Fuzzy » Tue Apr 22, 2008 12:31 am

Good points DST. You are a novel thinker.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Re: Game Optimization Tips

Postby Game A Gogo » Tue Apr 22, 2008 3:56 am

also, sometimes you don't need something to be calculated exactly at each frame, so you just use a timer event instead! (smallest is 12 milliseconds, a 60fps game is 16.66~ milliseconds, so timer can go small enough for anything)

also try to keep your code well organized, to prevent errors that you will easily look over without noticing.
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Game Optimization Tips

Postby Fuzzy » Tue Apr 22, 2008 5:15 am

Avoid duplications. If you are typing the same lines over and over put it into a function.

At the same time, and contrary to my advice above, removing things from functions will make them faster. Same with unrolling loops.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Re: Game Optimization Tips

Postby Bee-Ant » Thu Apr 24, 2008 11:05 am

Will line value on code affects the value of file size too???How much???
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: Game Optimization Tips

Postby DST » Tue Apr 29, 2008 7:48 pm

Well the line values (i assume you mean AMOUNT of code) by itself is not a problem; It depends on how often the code is called in the game. For instance, a 50 line function executed on game startup is not bad compared to a 1 line code in a draw actor for 40 clones. Because that's a one time 50 line execution vs. 40 1line executions 30 times a second!!!!!

The same could be true for a create actor event vs. a function....lets say there is a create actor event for a block actor, one that limits his animation, or defines him as solid....if its in the createactor code for each one, that could mean 100 lines to execute that one line. If you call it as a function, the code exists only once. True, its still going to be executed 100 times, but in the memory and the game file itself, the function only takes up 1/100th the space....not to mention the fact that you only have to edit the code once, instead of going thru each actor and changing their individual scripts.

Which leads me to my next point....

Optimization is not just for performance of the game; Its also for YOU!
Your time and your mental energy are precious, don't waste them doing menial tasks over and over....that's what the computer is here for! Program it to do the repetetive things for you!

That is a major factor of bonding with programming and learning to become successful at it. The computer is here to work for you. If you have to do lots of changes by hand each and every time, you will make fewer overall changes, and you will not polish your game like you should; you won't add the sparkles and details that make it SING... ...eventually your game will need so much work that you won't want to even open it anymore....and that....would be the worst thing of all!
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score

Re: Game Optimization Tips

Postby Fuzzy » Wed Apr 30, 2008 7:42 am

Oh I see what Bee-Ant means now. I'll show an example that explains what DST was saying. It will also show you how to speed your game up(at times). Unfortunately it also goes against what he said about letting the computer think for you.

To a degree, it makes sense that the computer should think for you. But too often people have the computer make decisions over and over when the outcome is already known. Or the decision doesnt actually have to be made. This is why I call myself the enemy of IF.

here is the example, with comments about what is happening.

Code: Select all
if (x > y) // in this line a comparison is made. it takes a certain amount of time. if its too complicated it will slow down your game(a tiny bit)
{ // this only marks that the following code belongs to the if and nobody else. a very small amount of time.
    x = x +1; // do an operation. this also takes time. With this one it takes far less time than the if that precedes it.
} // closes the if. another very small amount of time.


So the total time taken is the sum of the time that each line takes. if its a very simple code line inside the if, it might not make sense to bother with if. Which is why i say people should avoid if. and example is with jumping.

here is the common way to jump. (inside key press)
Code: Select all
if (canjump == 1)
{
    yvelocity = 5;
}
canjump = 0;


Five lines. And an if that we might not need. plus somewhere is a canjump = 1.

here is how I like it. (also in key press)
Code: Select all
yvelocity = 5*canjump;
canjump != canjump;


Dont you like that better?

If we are using a for loop, we can also speed things up.

Code: Select all
// old way
for (i=0;i < 4;i++)
{
    do_something_here;
}


looking at that we can see that line 1(for) takes some time. then each line does too. the final } sends the computer back to the first where it must check the variable again. so line one and the last line happen once for every time we do_something_here; which is 2/3 of the total time taken, or 66 per cent!

Here is a better way. It takes more of your time(sorry DST) but it will run faster.

Code: Select all
do_something_here;
do_something_here;
do_something_here;
do_something_here;
do_something_here;


most of the time you dont want to do this. but 100% of the time is taken for the interesting part of the code. Its called unrolling a loop. you dont have to unroll it completely either. even 50% would be great.

Code: Select all
code]
// 50% unrolled
for (i=0;i < 2;i++)
{
    do_something_here;
    do_something_here;
}


These are poor examples. The CPU cache on your computers CPU is designed to do fast repeats of short loops. You shouldnt see much gain from these, but if you have huge loops, you might.


And the final part. how long does it take to do an if? We can do a test and find out! make an actor and use a key down event. in that, take a copy of the current time into a variable. then run the command you want and then take another copy of the time and subtract it from the first. The difference is how long that command takes. More than likely you will have to repeat the command thousands of times before you take a second copy of the time. then divide by how many times you repeated.

and lastly, on the topic of time.. using spaces in your code does not add any time to the game itself. It can in interpreted programming like HTML, but for GEs finished games, its all taken out(thanks makslane!)
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Re: Game Optimization Tips

Postby Bee-Ant » Tue May 06, 2008 10:42 am

Oh sorry, I meant...the code lines count...
ex :
Code: Select all
if(canjump==1)       // line 1
{                            // line 2
    yvelocity=-10;     // line 3
    canjump=0;        // line 4
}                            // line 5

There are 5 lines. If there are more lines, will it affect the game file size???
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

PreviousNext

Return to Advanced Topics

Who is online

Users browsing this forum: No registered users and 1 guest