Game Loops
-
This WTF is a kind of "mini tutorial" posted on the "GameDev.net" forums. You have to read it for yourself.
http://www.gamedev.net/community/forums/topic.asp?topic_id=450508&PageSize=25&WhichPage=1
This is how you can make a loop go on forever.
for(;;)
forever loops are useful only when you have another loop that is nested between it. The number of nested loops doesn't matter.
Example:
for(;;){
while(condition){
do something
}
}
But you want to end the loop or do the loop over again.
You use the break; or continue;
Example:
for(;;){
while(condition){
do something
continue;
}
break;
}
While you don't have to use the continue; statment you should always use the break; in a forever loop.
Other alternatives would be abort;
-
Really?
How about:
for(;;)
{
MovePlayer();
if(PlayerDead()) break;
UpdateScores();
MoveAIPlayers();
if(PlayerDead()) break;
MoveMissiles();
if(PlayerDead()) break;
}
-
@GettinSadda said:
Really?
Maybe you did not read the original post:
"forever loops are useful only when you have another loop that is nested between it"
-
@sirhegel said:
@GettinSadda said:
Really?
Maybe you did not read the original post:
"forever loops are useful only when you have another loop that is nested between it"
Ah, so all those forever loops I have written for years must be the useless kind!
;)
-
@GettinSadda said:
Really?
How about:
for(;;)
{
MovePlayer();
if(PlayerDead()) break;
UpdateScores();
MoveAIPlayers();
if(PlayerDead()) break;
MoveMissiles();
if(PlayerDead()) break;
}
Pretty much the right idea. The general way it's done is an infinite loop that runs on the clientside input, network i/o, process data, output, repeat and on the server side network i/o, process data, repeat. In single player games it's pretty much the same minus the network loop. In a loop that's moving that fast there's no reason to check three times if the player's dead. ;)
-
What I find really funny about that thread, is the number of people who believe that even _infinite_ loop should have a _termination_ condition.
...Well I know that "infinite loop" is just an idiom, but still it just makes me smile:)
-
There IS an application for actual infinite loops as far as I know: Microprocessor programming, where the "abort condition" is that the processor gets cut off from power :)
-
@PSWorx said:
There IS an application for *actual* infinite loops as far as I know: Microprocessor programming, where the "abort condition" is that the processor gets cut off from power :)
You have a funny definition of "infinite". On that subject, I have a potion I'll sell you that'll give you infinite life (till you die). Say, $100? ;)
-
lol, alright, alright, alright. But you have to admit, from the point of view of a virtual being that lives inside the processor, it actually ... ok, I'll stop now...
-
I remember being impressed with Ada because you could write an infinite loop without a faked up condition. The idea being that in Ada the typical infinite loop would normally be terminated by detonation.
--Larry Wall