Script Optimization Help

First off i would like to thank the community for all the help they have giving me via all the great answers you’ve given everyone else. This is such a great forum that most of my questions are easily answered with a simple search.

That said, Im in the optimization phase of my game development, and i have this one dirty little script that ive been putting off fixing for a while.

Below is a load controller script that will continuously call the functions Load, Loading, Loaded, Unload, Unloading, and Unloaded on the current gameObject inside another component depending on the loadState of the this script.

#pragma strict

enum loadStates {Load, Loading, Loaded, Unload, Unloading, Unloaded, None}

var script : Component;
var loadState : loadStates  = loadStates.Load;

function Start(){
	if(!script){
		print("Script Unassigned: " + gameObject.name);
	}
}

function Update(){
	switch(loadState){
		case loadStates.Load:
			//script.Load();
			SendMessage("Load", SendMessageOptions.DontRequireReceiver);
			break;
		case loadStates.Loading:
			//script.Loading();
			SendMessage("Loading", SendMessageOptions.DontRequireReceiver);
			break;
		case loadStates.Loaded:
			//script.Loaded();
			SendMessage("Loaded", SendMessageOptions.DontRequireReceiver);
			break;
		case loadStates.Unload:
			//script.Unload();
			SendMessage("Unload", SendMessageOptions.DontRequireReceiver);
			break;
		case loadStates.Unloading:
			//script.Unloading();
			SendMessage("Unloading", SendMessageOptions.DontRequireReceiver);
			break;
		case loadStates.Unloaded:
			//script.Unloaded();
			SendMessage("Unloaded", SendMessageOptions.DontRequireReceiver);
			break;
	}
}

This script has seriously organized alot of my scripts since most of my scripts Update functions were managing the load states of my game object. So a sample script would look something like this:

private var loadController : LoadController;

function Start(){
	// Get the loadeController on the game object
	loadController = GetComponent(LoadController);
}

function Load(){
	// Place Loading Code Here
	
	...
	
	loadController.loadState = loadStates.Loading;
}

function Loading(){
	// Check if script is finished loading
	if(...){
		// You may want to add some loaded code here
		
		...
		
		loadController.loadState = loadStates.Loaded;
	}
}

function Loaded(){
	// Here you may want to be check if the script should be unloaded
	if(...){
		loadController.loadState = loadStates.Unload;
	}
	
	// Or you may want to set it to load state to None to save cycles
	// loadController.loadState = loadStates.None;
}

function Unload(){
	// Place Unloading Code Here
	
	...
	
	loadController.loadState = loadStates.Unloading;
}

function Unloading(){
	// Check if script is finished unloading
	if(...){
		// You may want to add some unloaded code here
		
		...
		
		loadController.loadState = loadStates.Unloaded;
	}
}

function Unloaded(){
	// Here you may want to be check if the script should be loaded again
	if(...){
		loadController.loadState = loadStates.Load;
	}
	
	
	// Or you may want to set it to load state to None to save cycles
	// loadController.loadState = loadStates.None;
}

Note that you can skip any one of the load state depending on how your game logic works, its just a simple state based function caller.

Now all is working great. I would just like some advice how i can replace the SendMessage calls, if possible, with actual function calls to the other component script. The other component script is always going to be different so i cant simple provide the proper component name for the ‘script’ variable in my load controller. Is it possible to cast a component as a type at runtime on the iPhone? or am i stuck in the situation I’m in. Maybe I’m stressing to much about the SendMessage calls?

Any advice would awesome!
-Keith

If you don’t have a clear target, you can’t replace the sendmessage actually.

And no you don’t stress it too much. It will cost you performance and having it in the update definitely isn’t the best idea.

How much and if it will be too much performance lost is something you will have to test in reality.

Yeah i guess its always a tug-of-war with performance and maintanablilty.

What alternative is there to using the Update function as a way of checking the load process of a script?

Leave it in the update but delay it.

count++;
if (count>1000)
{
count=0;
.... put your code here ...
}

Only use Update for things that actually need to run every frame. Otherwise use coroutines, which has the side effect of making the code better structured and more easily understandable.

–Eric