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!)