Sign in to follow this  
Kjansen92

When to Implement Game Loop

Recommended Posts

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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

...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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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).

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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).

 

You could always pass a function and check for functionality. As long as your features work seamlessly, you could create a tutorial loop (such as, mentioned above) which allows active responding to game elements until that loop ends. 

 

With a standard loop like the one above, it would maintain forever the end desired game formula. 

 

Objectively, with a tutorial-type loop, you could actively test different parts of your game and improve them, without structuring game schematics.

 

Why else would I build anything other than to learn and produce? 

Share this post


Link to post
Share on other sites

 

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.

 

Yes, although, one other implementation could last forever. This does not make it a game loop any more than RPG Maker makes a game.

 

The purpose of a loop is vital and easy to understand. 

 

It's the input/output of human energy consumption to anomalies in the creative approaches - that should be utilized to make practice that one notch better - that is valid information to explore objectively.

 

I don't care if the code hardly works, and I have no idea how to fix it yet. What's relevant is that those avenues are being explored, while being subjective to rigidity of code. 

Share this post


Link to post
Share on other sites

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