Game Development Community

Beating the Watchdog

by Lin Chear · 11/15/2009 (8:26 pm) · 2 comments

In Torque2D iPhone, the App Delegate's applicationDidFinishLaunching method executes most of the startup routine that gets the game up and running. A part of this process involves running the entry script main.cs through runEntryScrip(). However, spending too much time in applicationDidFinishLaunching can get your app killed before it even finishes launching.

If you're including plenty of external scripts through exec() commands, this could take quite a bit of time. Even for a modest number of scripts, it can be enough to get killed by the iPhone's watch dog timer.

A simple way to get around this is to get out of applicationDidFinishLaunching as soon as you can. The best way I've found is to defer execution of other scripts upon launch by placing them in a schedule.

In the very first main.cs I placed the bootstrap voodoo in a function deferMain() and execute that with a schedule timer.
//---------------------------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//---------------------------------------------------------------------------------------------


/// Player Initialization Procedure
/// 
function onStart()
{   
}

function onExit()
{
}

//---------------------------------------------------------------------------------------------
// Load the paths we need access to
//---------------------------------------------------------------------------------------------
function loadPath( %path )
{
   setModPaths( getModPaths() @ ";" @ %path );
   exec(%path @ "/main.cs");

}

//---------------------------------------------
// Do some bootstrap voodoo to get the game to 
// the initializeProject phase of loading and 
// pass off to the user
//---------------------------------------------

schedule(100, 0, deferMain);


function deferMain() {
	// Output a console log
	setLogMode(6);
	
	loadPath( "common" );
	
	loadPath( "game" );
	
	onStart();
	
	// Initialized
	echo("nTorque Game Builder (" @ getT2DVersion() @ ") initialized...");
	
	if( !isFunction( "initializeProject" ) || !isFunction( "_initializeProject" ) )
	{
	   messageBox( "Game Startup Error", "'initializeProject' function could not be found." @
				   "nThis could indicate a bad or corrupt common directory for your game." @
				   "nnThe Game will now shutdown because it cannot properly function", "Ok", "MIStop" );
	   quit();
	}
	
	_initializeProject();
	
	// Startup the project
	initializeProject();
	
        // execute any other functions that would normally be done after this script runs.
	// initOpenFeint();	
}

This will exit you out of applicationDidFinishLaunching and onto the rest of the game code and gaurantees you don't spend too much time in the delegate function.

About the author

http://thebrokentoy.com

Recent Blogs


#1
11/16/2009 (2:35 pm)
Very useful hack! I'm going to give this some testing when I have time.
#2
03/06/2010 (6:44 pm)
I tried this with iTGB 1.3beta and it worked but OpenFeint wouldn't initialize.