Leaving/Re-entering Game States Problem...

dhp

03-03-2007 20:03:31

I've been working on a fighting game and have encountered a major problem when dealing with OgreNewt. Basically, I have 2 game states: MenuState and MultiPlayerState. The MultiPlayerState is where the actual fighting takes place.

In my code I have...

void MultiPlayerState::enter()
{
.
..
...
// Initialize the physics world
mWorld = new OgreNewt::World();

mWorld->setWorldSize(Ogre::Vector3(-500, -500, -500), Ogre::Vector3(500, 500, 500));
mNewtonListener = new COgreNewt_FL(mSceneMgr, mWorld, 200);

mRoot->addFrameListener(mNewtonListener);

...
..
.
}


and elsewhere I have...

void MultiPlayerState::exit()
{
mSceneMgr->clearScene();
mSceneMgr->destroyAllCameras();

mRoot->removeFrameListener(mNewtonListener);
mRoot->getAutoCreatedWindow()->removeAllViewports();
mRoot->destroySceneManager(mSceneMgr);

if(mNewtonListener)
delete mNewtonListener;
if(mWorld)
delete mWorld;
}


and finally...

bool MultiPlayerState::frameEnded(const FrameEvent& evt)
{
.
..
...
if(keyDown(KC_F12)) // keyDown is macro
{
changeState(MenuState::getInstance());
}

return true;
}



For testing purposes, I start my game in the MultiPlayerState. When I press F12 to change to MenuState, that's when the game crashes. I've verified that MultiPlayerState::exit() is called and the game gets into and to the end of MenuState::enter(). From what I know, it should then go to frameStarted? But it never reaches that point in MenuState. Here are images of the crash I get...

(this pops up first)



(followed by this)




Anyway, the reason I believe I'm doing something wrong with OgreNewt is because when I comment out all OgreNewt related stuff, it switches states fine! No errors at all.

Any help will be greatly appreciated. Thanks!

Game_Ender

03-03-2007 20:48:52

Have you tried to clean up OgreNewt before the Ogre stuff? and do you mean to completely destroy your scene everytime you go into the menu?

dhp

03-03-2007 20:56:49

I just tried what you suggest Game_Ender, but it still crashes. And yes, I mean to destroy my entire scene everytime I go into the menu.

Acid_Gambit

30-03-2007 21:13:15

I am kind of having the same problem. So, if you've solved yours could please reply?

walaber

01-04-2007 07:59:26

use the debugger to step through where you delete everything, and see exactly where the crash is happening. that will explain a lot.

Abraxas77

07-12-2008 00:16:01

I realize this thread is over a year old, but seeing as how the issue was never resolved and that I recently encountered a similar issue. . . .


I found the exception occurs when the listener's frameStarted method is called after the GameState's exit method (where the listener was supposedly removed). I suppose the listener is not actually removed until the end of the frame. And due to the order in which the GameStates and listeners have their frameStarted methods called (in respect to the main game loop), the OgreNewtListener winds up having it's frameStarted method called despite the fact the listener was removed earlier that same frame.

So, to correct the issue, I simply removed the mRoot->addListener() and mRoot->removeListener() calls and instead manually called the listener's frameStarted and frameEnded methods from my GameState. Like so:

{from PlayState.h}
// ...
OgreNewt::BasicFrameListener* m_pOgreNewtListener;
// ...




{from PlayState.cpp}
// ...

void PlayState::enter()
{
// ... (create camera, add viewport, ...)
m_pOgreNewtListener = new OgreNewt::BasicFrameListener(pRenderWindow, m_pSceneMgr, pWorld, frameRate);
// ...
}

void PlayState::exit()
{
// ...
if (m_pOgreNewtListener)
{
OgreNewt::Debugger::getSingleton().deInit();
delete m_pOgreNewtListener;
m_pOgreNewtListener = NULL;
}
// ... (clear scene, destroy camera, remove viewport)
}

// ...

bool PlayState::frameStarted(const FrameEvent& evt)
{
// ...
if (m_pOgreNewtListener) m_pOgreNewtListener->frameStarted(evt);
// ...
return true;
}

bool PlayState::frameEnded(const FrameEvent& evt)
{
// ...
if (m_pOgreNewtListener) m_pOgreNewtListener->frameEnded(evt);
return true;
}

// ...


Note: this is my first project using Ogre, Newton, and OgreNewt so my explanation could, perhaps, be slightly inaccurate. Likewise, my solution may not be the most elegant. Nevertheless, it resolved the issue for me.

dzeligman

24-12-2008 22:17:31

I am also curious to this sort of situation involving Newt as I am new to it.