Skip to main content
edited tags
Link
Tetrad
  • 30.1k
  • 12
  • 96
  • 143
Tweeted twitter.com/#!/StackGameDev/status/287091545948188672
improved grammar and formatting
Source Link
kevintodisco
  • 3.8k
  • 1
  • 18
  • 26

For example, I have a Game class and it keeps an intint that tracks the playersplayer's lives. I have a

if ( mLives < 1 ) conditional

check and inside the if scope it does some work.

if ( mLives < 1 ) { // Do some work. } 

However this condition keeps firing and the work is done over and over, so forrepeatedly. For example i, I want to set a timer to exit the game in 5 seconds. It Currently, it will keep setting it to 5 seconds every frame and the game will never endends.

This is just one example, and I have the same problem repeats itself over and over in several areas of my game,. I want to check for some condition and then do something once and only once, and then not check or do the code in the if statement-statement again. Some possible solutions that come to mind are having a boolbool for each condition and set the boolbool when the condition fires, however. However in practice this gets very messy handling lots of bools thatbools, since they have to be stored in aas class fieldfields, or even statically within the method itself, that would get nasty fast.

What is the proper solution to this?   ( Notnot for my example, but for the problem domain  )? How would you do this in a game?

For example I have a Game class and it keeps an int that tracks the players lives. I have a

if ( mLives < 1 )

check and inside the if scope it does some work.

However this condition keeps firing and the work is done over and over, so for example i want to set a timer to exit the game in 5 seconds. It will keep setting it to 5 seconds every frame and the game will never end.

This is just one example, and problem repeats itself over and over in my game, I want to check for some condition and then do something once and only once, and then not check or do the code in the if statement again. Some possible solutions that come to mind are having a bool for each condition and set the bool when the condition fires, however in practice this gets very messy handling lots of bools that have to be stored in a class field, or even statically within the method itself, that would get nasty fast.

What is the proper solution to this? ( Not for my example, for the problem domain  ) How would you do this in a game?

For example, I have a Game class and it keeps an int that tracks the player's lives. I have a conditional

if ( mLives < 1 ) { // Do some work. } 

However this condition keeps firing and the work is done repeatedly. For example, I want to set a timer to exit the game in 5 seconds. Currently, it will keep setting it to 5 seconds every frame and the game never ends.

This is just one example, and I have the same problem in several areas of my game. I want to check for some condition and then do something once and only once, and then not check or do the code in the if-statement again. Some possible solutions that come to mind are having a bool for each condition and set the bool when the condition fires. However in practice this gets very messy handling lots of bools, since they have to be stored as class fields, or statically within the method itself.

What is the proper solution to this  (not for my example, but for the problem domain)? How would you do this in a game?

Source Link
EddieV223
  • 437
  • 3
  • 14

Game loop, how to check for conditions once, do something, then not do it again

For example I have a Game class and it keeps an int that tracks the players lives. I have a

if ( mLives < 1 )

check and inside the if scope it does some work.

However this condition keeps firing and the work is done over and over, so for example i want to set a timer to exit the game in 5 seconds. It will keep setting it to 5 seconds every frame and the game will never end.

This is just one example, and problem repeats itself over and over in my game, I want to check for some condition and then do something once and only once, and then not check or do the code in the if statement again. Some possible solutions that come to mind are having a bool for each condition and set the bool when the condition fires, however in practice this gets very messy handling lots of bools that have to be stored in a class field, or even statically within the method itself, that would get nasty fast.

What is the proper solution to this? ( Not for my example, for the problem domain ) How would you do this in a game?