Jump to content
  • Advertisement
Sign in to follow this  
Samurai Jack

Game init error handling

This topic is 4353 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

Greetings! I have some problems with error handling. The problem lies in the code because it looks ungly. Like this: bool InitApplication() { if( InitVideo() == false ) { logWrite("Video init failed.\n"); return false; } else logWrite("Video init successful.\n"); if( AudioInit() == false ) { logWrite("Audio init failed.\n"); return false; } else logWrite("Audio init successful.\n"); return true; } should I use macros like: logAssert( InitVideo(), "Video init" ); logAssert( InitAudio(), "Audio init" ); or should i take exceptions? is there a better way to do it?

Share this post

Link to post
Share on other sites
One way that I like doing initialization( If I understand your 'problem' correctly) is to have a gamestate class like this:

class CGameState
virtual bool Init() =0;
virtual bool ShutDown() =0;

virtual bool Update() =0;
virtual bool Render() =0;

virtual bool Minimize() =0;
virtual bool Restore() =0;

virtual bool TakeInput() =0;

and all of each states initialization code is in the Init()... but if your just trying to clean up your code, I would make an abstract class with pure virtual methods to represent each system, ie video,sound, input all inherit from it and then all you do is :

ISystem* video = new Graphics(...);
ISystem* sound = new Audio(...);
ISystem* input = new Input(...);

//Now you could've just made it all an array of ISystems and then just for thru
//them, but Im not, that way I know what I've inited etc.

//And then you just call sound, video, and input's 'special' methods for what //their job is etc. As for error checking, just build in some virtual=0; methods
// into ISystme.

I hope that was enough to help you there =D

Share this post

Link to post
Share on other sites
I'd do something like:

bool LogInit ( bool b, string name )
if ( b )
LogSuccess ( "%s Initalized", name.c_str() );
LogError ( "%s failed to Initalize", name.c_str() );
return b;

and call it like:

bool Init ( )
bool rVal = true;
rVal &=LogInit ( GraphicsInit(), "Graphics Engine" );
rVal &=LogInit ( SoundInit(), "Sound Engine" );
return rVal;

Share this post

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!