• Advertisement
Sign in to follow this  

Can anyone explain the concept of a game loop?

This topic is 1465 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

For example lets say I want to create a Visual Novel. Im trying to figure out how I can create a proper game loop where, the story will progress. So far this is what I normally do when I want to create a "story". -Check what to draw(ill explain more) -Draw Scene -Input -Rest

For checking what to draw, lets say I have Option 1 and Option 2, in a function, I would have a switch statement nested into another switch statement, in another, etc...and each switch statement would have choices 1-4 maybe, where it would draw a scene based on a number that was assigned to it under that switch statement, like this:

You have choice 1 or 2... switch(choice)

case 1: draw(asdf) switch(choice2) case 1:

case 2:

case 2: draw(zxcv) switch(choice2) case 1:

case 2:

I know that is definitely not how you do logic and input so I was wondering what you guys would do in a situation like this.

Share this post


Link to post
Share on other sites
Advertisement

 

For example lets say I want to create a Visual Novel. Im trying to figure out how I can create a proper game loop where, the story will progress. So far this is what I normally do when I want to create a "story". -Check what to draw(ill explain more) -Draw Scene -Input -Rest

For checking what to draw, lets say I have Option 1 and Option 2, in a function, I would have a switch statement nested into another switch statement, in another, etc...and each switch statement would have choices 1-4 maybe, where it would draw a scene based on a number that was assigned to it under that switch statement, like this:

You have choice 1 or 2... switch(choice)

case 1: draw(asdf) switch(choice2) case 1:

case 2:

case 2: draw(zxcv) switch(choice2) case 1:

case 2:

I know that is definitely not how you do logic and input so I was wondering what you guys would do in a situation like this.

 

 

How about using a graph to store the relationship between the scenes and choices and just draw whichever node in the graph you're on at any given time ?

Share this post


Link to post
Share on other sites

I'm not sure your level of programming but mostly, if you just create a simple "Hello World" program, the program will close after the return call.

Observe:

#include <iostream>

int main(){
 return 0;
}

This program just closes, or rather, returns the number 0 to oblivion.

 

A game however, should not close. At least not so fast. So we create a loop. Consider:

#include <iostream>
using namespace std;

int main(){
 int close = 1;

 while(close=1){
  cout<<"Hey, you want to close? Type 0.";
  cin>>close;
 }
 return 0;
}

This program will not close unless close = 0. You see that "while"? That's a loop that says, as long as close=1 start over from "cout<<..."

In most games, the render function is placed in this loop to be repeated over and over, redrawing the screen until it is stopped. I'm not sure what you are using, but that is how the loop is used.

 

edit: As suggested below, while(close=1) should be while(close==1)

Edited by InfinityMachine

Share this post


Link to post
Share on other sites
This is covered in detail in General Game/Engine Structure and Passing Data Between Game States.

A single state can be made as a base state for everything you need to do common to all the pages, such as fading in and out, handing input, etc.
You just pass the page number to display and reload the state with that page number to advance to any given page at any given time.

It fully suits your needs and more states can be added for a start-up screen, ending, credits, whatever.


L. Spiro

Share this post


Link to post
Share on other sites
#include <iostream>
using namespace std;

int main(){
int close = 1;

while(close==1){
cout<<"Hey, you want to close? Type 0.";
cin>>close;
}
return 0;
}

"close=1" to "close==1"

 

We want the loop to eventually end.

 

Although:

#include <iostream>
using namespace std;

int main(){
int close = 1;

while(close!=0){
cout<<"Hey, you want to close? Type 0.";
cin>>close;
}
return 0;
}

 

Will work better for the idea that 0 exits.

Edited by Dragonsoulj

Share this post


Link to post
Share on other sites

I know I'm not the first one to attempt an answer, but here's my two cents:
 
A game loop can be considered the "heartbeat" of a game. It repeats the game code which handles the attributes and interactions between the objects in the game. Each repetition is often called a "tick," and on every tick, the game performs one round of calculations.
 
For example, let's say you have a game where the player is controlled by the WASD keys. The code might look something like this (I've used "pseudocode" reminiscent of Java, but the concept applies to any language or engine):

public static void main(String[] args){
    boolean runGame = true;
    while(runGame){
        if(Controls.wKeyDown) player.yPosition++; //If W is down, move the player up by one unit
        else if(Controls.sKeyDown) player.yPosition--; //If S is down, move the player down by one unit
        
        if(Controls.dKeyDown) player.xPosition++; //If D is down, move the player right by one unit
        else if(Controls.aKeyDown) player.xPosition--; //If A is down, move the player left by one unit

        calculateInteractions(); //This method might handle interactions between the player and other parts of the world

        wait(20); //pause for 20 milliseconds so the game isn't unplayably fast
    }
}

This is a highly simplified version, but it gets the point across. What happens is, every tick, the game checks whether or not the W, A, S, or D keys are held down. If one of them is, it moves them in the correct direction. Then, it pauses for a bit and the loop goes back to the top. If the same key is still held down, it will move them another unit in the right direction.

 

Game loops often loop through every object in the game world to check for interactions with every other object. For example, the calculateInteractions() method mentioned above might be something like this:

private void calculateInteractions(){
    for(gameObject obj1 : worldObjects){ //Loop through everything in the world
        for(gameObject obj2 : worldObjects){ //Loop through everything again
            if(objectsCanInteract(obj1, obj2)) //Can the two objects interact? (Are they close enough? Are they the right kind of objects? Are they not the same object?)
                obj1.doInteraction(obj2);
                // If the two objects can interact with each other,
                // have the first object perform its interactions with the second.
                // When it's the second object's turn to be obj1, it can perform its actions on the original first object
        }
    }
}

Again, this is heavily simplified. Hopefully it gives a general idea of what a game loop is and does.

Share this post


Link to post
Share on other sites

Regarding OP.

Don't do switch. Every time you write switch to discriminate between complex actions, ask yourself if what you write for a specific value is really so unworthy to not need its own object. If it does, then you can write a class for it, and then instancing an object with a name (not necessarily a string) you can put it in a std::map and just have "the switch written for you", with the added benefit of being fully dynamic.

Now...


For checking what to draw, lets say I have Option 1 and Option 2, in a function, I would have a switch statement nested into another switch statement, in another, etc...and
Instead of doing that manually, you leave the above described system nesting the "implicit switches" for you.

So, we have this std::map making us jump directly to the action we need by a name. Of course we cannot expect to check out all the used names every time a new action has to be added but it's reasonable to check the existing actions "nearby", in the same "context" of actions.

So what I suggest to do is to have this map of actions, let's call it ActionMap, to be pushed and popped. So what you get is, in the end:

 

std::vector<ActionMapProxy> modes;

 

Rather than passing data from one state to the other, I've found useful to just build the association at construction time. I guess that's personal opinion though.

 

So, in the end, how does your game loop looks like?

ActionStack as;
InitialActions start; // you wrote this somewhere else.
as.push_back(&start);
char name[128]; // just assuming
while(as.length()) {
    cout<<"What do you want to do?";
    cin>>name;
    auto context = as.GetLast();
    auto action = context.GetByName(name); // internally this->map[name], iterator
    if(action == context.cend()) cout<<"No such action";
    else action->Trigger(/* params here */);
}
return 0;

So it would be basically input = fromSomething(); process(input), no matter how complex your story is going to be. Also feel free to put the code in as many files you want.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement