Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Need advice with game loop


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 YyYo   Members   -  Reputation: 137

Like
0Likes
Like

Posted 13 July 2014 - 06:31 AM

Hi...

 

I am newbie to game programming, so excuse me if I ask silly question.

I start to implement my first game. The game is like a turn-base board game.(implemented using Java for Android)

Each player have 14 soldiers. each soldier placed/located in default position.

Before starting game, there is an 'INIT' stage, where each player can change the default location of each of his soldiers.

After each player set their soldiers, the game can start.

 

My game loop(pseudo code)

while(true){
	processUserEvents();	// if its player's TURN, will update the event(else will drop it)
	draw();			// redraw			
}

My question is: should the stage of 'INIT' need to be inside game loop?

or it should be in a different place, and when 'INIT' finished, only then execute game loop ?

 

any byte of advice will help.

 



Sponsor:

#2 Lysy   Members   -  Reputation: 681

Like
0Likes
Like

Posted 13 July 2014 - 06:43 AM

Init should be done before game loop.



#3 YyYo   Members   -  Reputation: 137

Like
0Likes
Like

Posted 13 July 2014 - 07:09 AM

Ok Thanks..

 

BTW, user input(such as touch event), are stored in Queue, instead of handling them immediately.

In every iteration of game loop i 'poll' event from the queue and handle it.



#4 Glass_Knife   Moderators   -  Reputation: 6731

Like
5Likes
Like

Posted 13 July 2014 - 07:36 AM


Before starting game, there is an 'INIT' stage, where each player can change the default location of each of his soldiers.

 

This is part of the game, and needs to be done in the game loop.  Performing an initialize() before the game loop starts is a different thing.

Anything that happens while the game is running goes inside the game loop.  


I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532


#5 rip-off   Moderators   -  Reputation: 9563

Like
0Likes
Like

Posted 13 July 2014 - 07:57 AM

Agreed, this is part of the game loop. It is a particular rule state, or phase, rather than a separate step.

#6 Lysy   Members   -  Reputation: 681

Like
1Likes
Like

Posted 13 July 2014 - 09:35 AM

 

Before starting game, there is an 'INIT' stage, where each player can change the default location of each of his soldiers.

 

Ooops, didn't read it thorough. Of course in this case this should be in game loop. SORRY for inconvenience.



#7 mark ds   Members   -  Reputation: 1655

Like
1Likes
Like

Posted 13 July 2014 - 11:51 AM

In this instance I'd be inclined to do something like...

initialise_game();

bool replay = true;

while( replay )
{
    get_player_start_locations(); // this function would have a mini game loop while players set their starting locations
    
    bool running = true;

    while( running )
    {
        running = process_user_events(); // return false when player hits quit button etc.
 
        if( running == true )
        {
            draw();
        }
    }

    replay = ask_if_player_wants_another_game(); // again, a mini game loop in here while replay question is displayed
}

shut_down_game();

Edited by mark ds, 13 July 2014 - 11:55 AM.


#8 cozzie   Members   -  Reputation: 3062

Like
0Likes
Like

Posted 13 July 2014 - 12:49 PM

Maybe you can create a set of states, so depending on the game state something will happen when you're in the game loop. In your case you could have a state "menu" in which the player can either change the positions you mentioned or config something else. And another state coud be "game_running"
Crealysm game & engine development: http://www.crealysm.com
My games: http://www.crealysm.com/games

Looking for a passionate, disciplined and structured producer? PM me

#9 Servant of the Lord   Crossbones+   -  Reputation: 24635

Like
6Likes
Like

Posted 13 July 2014 - 12:50 PM

In this instance I'd be inclined to do something like...

initialise_game();

bool replay = true;

while( replay )
{
    get_player_start_locations(); // this function would have a mini game loop while players set their starting locations
    
    bool running = true;

    while( running )
    {
        running = process_user_events(); // return false when player hits quit button etc.
 
        if( running == true )
        {
            draw();
        }
    }

    replay = ask_if_player_wants_another_game(); // again, a mini game loop in here while replay question is displayed
}

shut_down_game();

If you expand the functions with the "mini game loops", you get:

while( gameRunning )
{
    while( gettingPlayerStartPositions )
    {
        //'Place starting positions' logic
        //'Place starting positions' drawing
    }

    while( playingGame )
    {
        //Playing logic
        //Playing drawing
    }

    while( askIfPlayerWantsToPlayAgain)
    {
        //Play Again? logic
        //Play Again? drawing
    }
}

Then you see it has unnecessary looping.

So it can be simplified like this:


enum GameState { PlaceStartingPositions, PlayerTurn, MainMenu, GameOver,
GameState gameState = MainMenu;

while( gameRunning )
{
    if( gameState == PlaceStartingPositions )
    {
        //'Place starting positions' logic
        //'Place starting positions' drawing
    }
    else if( gameState == PlayerTurn )
    {
        //Playing logic
        //Playing drawing
    }
    else if( gameState == EnemyTurn )
    {
        //Watching enemy moves
    }
    else if( gameState == GameOver)
    {
        //Play Again? logic
        //Play Again? drawing
    }
}

Just to be clear, I'm not advocating against functions - just against the use of infinite loops within other infinite loops to control the flow of gameplay.

(By 'infinite loop' I mean loops where the game can't break out of it without player action).


Edited by Servant of the Lord, 13 July 2014 - 12:52 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS