When to Implement Game Loop

Started by
13 comments, last by Kjansen92 9 years, 6 months ago

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

Advertisement

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?

- Jason Astle-Adams

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.

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.

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

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.

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

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.

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.

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

This topic is closed to new replies.

Advertisement