Problems with my "gamestate"-variable

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Halan
Posts: 447
Joined: Tue Oct 04, 2005 8:17 pm
Location: Germany, Freak City
Contact:

Problems with my "gamestate"-variable

Post by Halan »

Code: Select all

//Including the Irrlicht Engine (http://irrlicht.sourceforge.net/) and other stuff
#include <irrlicht.h>
#include <stdio.h>
#include <iostream>

using namespace irr;

using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

#pragma comment(lib, "Irrlicht.lib")

int gamestate = 1;
IrrlichtDevice *device = 0;

class MyEventReceiver : public IEventReceiver
{
public:
bool OnEvent(SEvent event)
{
if(gamestate=1)
{
	if (event.EventType == EET_KEY_INPUT_EVENT)
	{
    gamestate=2;                    
    }
    return false;
}
if(gamestate=2)
{
	if (event.EventType == EET_KEY_INPUT_EVENT)
	{
    if (event.KeyInput.Key == KEY_KEY_Q)
    {
    gamestate=0;
    return true;
    }                    
    }
    return false;
}
}
};
   
int main()
{
//Create Device
device = createDevice(video::EDT_OPENGL,
core::dimension2d<s32>(1024, 768), 32, true, true, false, 0);
if (device==0)
{    
return false;
}

//Set GUI
IGUIEnvironment* env = device->getGUIEnvironment();

//Create Video Driver		
IVideoDriver* driver = device->getVideoDriver();

//Intialize EventReciver
MyEventReceiver receiver;
device->setEventReceiver(&receiver);

//Set Window Title      
device->setWindowCaption(L"Age of War");  

while(device->run()&&driver)
{
//----------------Quit------------//
while(gamestate=0)
{
device -> drop();
}
//----------------Intro-----------//
while(gamestate=1)
{
driver->beginScene(true, true, SColor(0,0,0,0));

env->drawAll();
driver->endScene();
gamestate=2;//Change to Menu
}
//----------------Menu------------//
while(gamestate=2) 
{
core::stringw str = L"FPS: ";
str += driver->getFPS();
env->addStaticText(str.c_str(), rect<s32>(900,720,1024,768), true, true);

driver->beginScene(true, true, SColor(0,0,0,0));
env->drawAll();
driver->endScene();
//ToDo: Design and UI
}
//---------------Game------------//
while(gamestate=3)
{
//ToDo: Everything
}
}
}
When i compile and start the game stucks at "state 1". dont know what to do :(

greets,
halan
SanderVocke
Posts: 40
Joined: Mon Oct 31, 2005 1:19 pm
Location: Netherlands

Post by SanderVocke »

When using "if" or "while", you should change:

"gamestate = 2"

to

"gamestate == 2".

"=" assigns a value to a variable(even in an if statement). therefore, when stating "if(gamestate = 1)", gamestate will be set to 1 and the if statement will return true. "==" is used to compare two things, running the if statement if they are the same.
"The shortest distance between two points is always under construction."
- Noelie Alite
pfo
Posts: 370
Joined: Mon Aug 29, 2005 10:54 pm
Location: http://web.utk.edu/~pfox1

Post by pfo »

There's another problem as well, you don't check whether the key is pressed or released, so if you press a key to change to state 2, as soon as you release the key it generates another message and changes back to state 1
Halan
Posts: 447
Joined: Tue Oct 04, 2005 8:17 pm
Location: Germany, Freak City
Contact:

Post by Halan »

okay thanks first.

however, i also wanted to ask, how do you normal design the code structure of a game? is it a good way to choose an variable?

in the tutorials they also dont check for releasing the keys. do i have to?
SanderVocke
Posts: 40
Joined: Mon Oct 31, 2005 1:19 pm
Location: Netherlands

Post by SanderVocke »

I usually make a gamestate variable, just like you. Then I define possible values for it like this:

#define GS_MENU 1
#define GS_INGAME 2
#define GS_PAUSED 3
#define GS_WHATEVER_STAGE_YOU_WANT 4

I store it in a seperate class along with other global variables, and it works fine for me. Now I can set it like

globals->gamestate = GS_PAUSED;

I don't know whether others have better suggestions, but this is a good way of doing it.
"The shortest distance between two points is always under construction."
- Noelie Alite
Guest

Post by Guest »

a better way would be to use an entire interface for your gamestates and then create instances of it and pass it to your state manager (game, engine, whatever..). this way you can easily extend your game with gamestates (or publish them as plugins via DLLs) without changing your base game code everytime again and you seperate each gamestate from each other in a clear way.
Post Reply