Jump to content

  • Log In with Google      Sign In   
  • Create Account


When to Implement 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
12 replies to this topic

#1 Kjansen92   Members   -  Reputation: 182

Like
0Likes
Like

Posted 16 December 2013 - 10:32 PM

Hey all!

 

I've been coding this simple text-based business game for the past couple of days, and I'd like to hear your opinions on this, be they idealistic, or totally rudimentary.

 

As I've hear and am now coming to realize, coding a text-based game is relatively simple. Adding whatever functionality I may think of has been a breeze and a lot of fun so far.

 

I'm getting to the point where I may consider adding a game loop.

 

Is there anything to say about when to add a game loop, and how it impacts the production of a game, be it as simple as my project?

 

Thanks!

Kevin John Jansen



Sponsor:

#2 jbadams   Senior Staff   -  Reputation: 17013

Like
0Likes
Like

Posted 17 December 2013 - 12:47 AM

If you need a game loop, it's time to add one.  If you're able to implement your game without one then it isn't necessary.  There really isn't much more to the decision! smile.png

 

Will the addition of a game loop improve your game, simplify your development, or allow you to add features that you can't currently add or which are currently prohibitively difficult to add?



#3 Kjansen92   Members   -  Reputation: 182

Like
0Likes
Like

Posted 17 December 2013 - 01:09 AM

Hey J.B.,

 

I'm assuming not. I'm currently testing all of my functionality through a tutorial function in the main file. 

 

I just thought there would be possibly be something to it that I wasn't seeing, such as:

-How to make it more fun

-Studying things that I will need to know for building graphics, engines, AI, etc.

 

But you're right. As far as testing goes, a main loop isn't really going to add to anything.

 

I also thought I'd put a post up here just to be part of the community.



#4 wintertime   Members   -  Reputation: 1575

Like
0Likes
Like

Posted 17 December 2013 - 02:29 AM

I think the game loop is the first thing to add when starting any game.

That is because without it people get tempted to create their game as an endless intermingling of repeated input, output and game logic (cout, cin, if inside a textbased game), that is impossible to refactor into a gameloop. That results in having to start anew when realizing it.



#5 Nypyren   Crossbones+   -  Reputation: 3647

Like
0Likes
Like

Posted 17 December 2013 - 02:50 AM

...an endless intermingling of repeated input, output and game logic...


Surprising fact: The original X-Com game for DOS was uses that kind of "intermingling" style, and the windows port didn't change it much, either (imagine seeing the windows message pump being called FROM the gameplay code when the game is ready for input). I was pretty shocked when I discovered this, but it got me thinking that there might be ways to exploit the general idea in modern games.

I'm not necessarily saying that you shouldn't use a game loop; just that sometimes you can think outside the box and it won't necessarily be catastrophic.

Edited by Nypyren, 17 December 2013 - 02:51 AM.


#6 Kjansen92   Members   -  Reputation: 182

Like
0Likes
Like

Posted 17 December 2013 - 04:36 AM

Working without a game loop is nothing different, no less catastrophic, assuming that you keep in mind that the functionality has to work together as one unit.

 

That's pretty common sense.

 

I was more looking for answers on the constructive side.



#7 timgranstrom   Members   -  Reputation: 473

Like
0Likes
Like

Posted 17 December 2013 - 05:35 AM

A game loop can be thought of as the core of a game. Pro's why you would want to try using a game loop in your game:
For learning purposes. Thats really all the reason you need!

Generally a game loop handles 3 parts: initializing the game,updating and drawing.

Even though you don't really need this in your text game, it's a nice way to learn the basics I guess.
Basics as in, learning how to set the speed of the cycle, aka fps(frames per second) and ups (updates per second).

I'm not sure how your game is constructed but chances are that you will need to edit some stuff or the entire game to get it working.

But if you wanna learn how it works then go ahead and implement it!

Generally I would say that the first thing you should always do is implement a game loop.

Good luck!

psst, a little example about how your gameloop would probably look (in psuedo code):

 

gameLoop()
{
    init(); // Initialize
    while(gameIsRunning)
    {
        input();
        update();
        render();
    }
    clearMemory(); // clear/empty allocated memory.
}

As you see, it does not need to differ much from the game loop of a graphical game



#8 AticAtac   Members   -  Reputation: 309

Like
2Likes
Like

Posted 17 December 2013 - 09:00 AM

the "init();" and "clearMemory();" are not part of the game loop.and shouldn't be in a the "gameLoop()" !

In this case it is the game entry-point.



#9 warnexus   Prime Members   -  Reputation: 1370

Like
1Likes
Like

Posted 17 December 2013 - 10:26 AM

Game loop generally used when you have a real time application that needs to update the game objects on the screen and draw them and then sleep the application inside one game loop.

 

Any real time application be it games or whatnot use loop to keep the application running.

 

Since you are designing a text based game which is not a real time application, you would still need a game loop but not the one that does any rendering and sleeping on the application. You just need a loop (both a main loop and a game loop combined for the text based program flow) You may still need to update or not update the game objects depending on the design.


Edited by warnexus, 17 December 2013 - 10:48 AM.


#10 rpiller   Members   -  Reputation: 651

Like
0Likes
Like

Posted 17 December 2013 - 10:31 PM

How are you implementing you game without a loop? In pretty much every game there are things that need to be repeated (even in text games), so how are you doing that now? goto? I don't even understand how you could make a game without a loop of some kind. Even an event driven game has a loop (it may be hidden from you but it's there).



#11 AticAtac   Members   -  Reputation: 309

Like
0Likes
Like

Posted 18 December 2013 - 01:55 AM

How are you implementing you game without a loop? In pretty much every game there are things that need to be repeated (even in text games), so how are you doing that now? goto? I don't even understand how you could make a game without a loop of some kind. Even an event driven game has a loop (it may be hidden from you but it's there).

 

The game could use an Event/Message System where Messages are sent around.

But even in this case there will be somewhere a message-pump-Loop for dispatching Events.



#12 wintertime   Members   -  Reputation: 1575

Like
2Likes
Like

Posted 18 December 2013 - 05:13 AM

How are you implementing you game without a loop? In pretty much every game there are things that need to be repeated (even in text games), so how are you doing that now? goto? I don't even understand how you could make a game without a loop of some kind. Even an event driven game has a loop (it may be hidden from you but it's there).

There are always loops. IMHO, a typical beginner game is often just structured around the game logic with many different loops and many different ways to get input, like this simplified pseudocode:

int main() {
  show_intro();
  wait_for_keypress(); // first loop hidden there
  string answer;
  while(1) { // next loop that repeats each time a new game is started
    while(first_scene) { // next loop that repeats in same game state
      do { // next loop that waits for acceptable input
        show_question();
        answer=input_text(); // loop hidden here to collect chars
      } while(!text_validated(answer));
      do_something_with(answer);
      show_something();
      int number;
      do { // next loop that waits for acceptable input in a different way
        show_question();
        number=input_number(); // loop hidden here to collect chars and only let numerals through to convert them
      } while(!number_validated(number,lower_bound,higher_bound));

      // many more loops here
    }
    // many more loops here
  }
  show_end();
  wait_for_keypress(); // another loop hidden here
}

Then sometime later they hear about the game loop and hopefully do the mental leap to realize its just a simplification, by turning this mess inside out, cutting out all those hidden repetitions of slightly modified output, input, validations, loops and replacing it with just one loop and some conditionals inside.


Edited by wintertime, 18 December 2013 - 09:57 AM.


#13 Kjansen92   Members   -  Reputation: 182

Like
0Likes
Like

Posted 20 December 2013 - 07:32 AM

I was simply doing one event after the other. For example, I'd display a list of options:

1) Use phone

2) Work on robots 

3) Rest

4) End day

5) Go into office

6) Quit game

 

And the user would choose, and do whatever was in each. In use phone, there was the option to create an ad to draw up business, and then check and respond to messages, and other things like that. 

 

When I was working on 'work on robots', I simply left the option to use the phone at the beginning, and then ran the 'work on robots' functionality.

 

In order to play the game though, I needed to implement a game loop so that the game didn't quit once you did those things. It was fine, and I could have ultimately kept on adding functionality without the use of a game loop, although I did fix up my code and add one in there anyway.

 

I don't use goto ever. I could upload my code if you want to look at it, although it'll simply look like really clean code with the basic functionality. Nothing too impressive.






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