Sign in to follow this  

how to run once in a loop

This topic is 4353 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Use a bool, set it from false to true, rather than incrementing an int.
In C, a static or a global is a good approach.
In C++, gameLoop could be a member function, and the flag a member variable of some object.
In some cases, the code could also just be taken out and left in a separate function to be called independently.

If you're in a multithreaded environment, beware of race conditions.

Share this post


Link to post
Share on other sites
Hi

Don't understand exactly what you are looking for but I seldom use a state system to run the loops I want within the game loop.

Example one

gameLoop()
{
int state = 0;

while ( state == 0 )
{
// Do whatever you want to do here

if ( someCondition )
{
state = 1;
}
}

while ( state == 1 )

( and so on... make a state = 0 when you want to return to top )

I'm using this with excelent results, but I never used it in big and complex game loops with lots of AI for instance. Another thing that is important is that you will have to refresh screen etc inside each while loop or the game will aparently stall until the someCondition is met.

Hope this helps.

Share this post


Link to post
Share on other sites
I agree a bool, your approach could even result in i being 0 again after 2^32 iterations. Is there any reason that you can't just move the code to only be executed once, outside of the loop?

Like this:

DoSomethingOnce();
gameLoop
{
DoSomethingEveryFrame();
}

Share this post


Link to post
Share on other sites
If you only want it to execute once I recommend something similar to this.

boolean flag = false;

gameLoop()
{
if (flag == false)
{
//do something
flag = true;
}
}

Using an if statement makes it more obvious that it shouldn't loop multiple times.

Greig

Share this post


Link to post
Share on other sites
Quote:
Original post by CTar
I agree a bool, your approach could even result in i being 0 again after 2^32 iterations.


It could not, because he increments conditionally, but a bool is still superior.

Quote:
Is there any reason that you can't just move the code to only be executed once, outside of the loop?

Like this:

DoSomethingOnce();
gameLoop
{
DoSomethingEveryFrame();
}


I would also suggest this.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Quote:
Original post by CTar
I agree a bool, your approach could even result in i being 0 again after 2^32 iterations.


It could not, because he increments conditionally, but a bool is still superior.

Quote:
Is there any reason that you can't just move the code to only be executed once, outside of the loop?

Like this:

DoSomethingOnce();
gameLoop
{
DoSomethingEveryFrame();
}


I would also suggest this.


While you are correct, once he substitutes code in the "//do something" position that code will execute and then i++ will be called every iteraton.

ie:
gameLoop
{
static int i = 0;
if (i == 0)
updateGamestate();
i++;
}

but it depends on what he does

Share this post


Link to post
Share on other sites
Quote:
Original post by Greig Hamilton
If you only want it to execute once I recommend something similar to this.

boolean flag = false;

gameLoop()
{
if (flag == false)
{
//do something
flag = true;
}
}

Using an if statement makes it more obvious that it shouldn't loop multiple times.

Greig


Further if its in a function it will be reset to FALSE each time the function is called.. Try putting STATIC before it, it makes it act as if its a global but without the naming conflict.

Example:

int gettime()
{
static bool firstrun = true;
if (firstrun)
{
SetupPerformanceTimer()
}

return time;
}

Share this post


Link to post
Share on other sites

This topic is 4353 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this