• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
AnthonyVelazquez

Can anyone explain the concept of a game loop?

7 posts in this topic

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.

0

Share this post


Link to post
Share on other sites

 

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 ?

1

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
0

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
1

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.

1

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.

0

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  
Followers 0