Abstracting the generic spawn function
Hi!
I am thinking about enhancing my code for spawning actors. I do it by a custom Factory script, which allows me pooling etc. Although, I have to spawn object that have different initial setups and the amount and type of variables that need to be passed on differs. Thus, I use different functions for each of those types, although this is not the most elegant way of doing things, especially if there will be more of those "custom" spawn operations.
Any ideas? Can't use generics, I guess, because of the different params each time.
Excerpt below:
public static GameObject Spawn(GameObject prefab, Vector2 p, bool poolCreation)
{
GameObject spawned;
//Debug.Log ("Spawned as part of pool creation!");
spawned = PoolSetup(prefab, p, poolCreation);
//return as reference
return spawned;
}
/*
* END OF BASIC SPAWNS
*/
public static GameObject SpawnCharacter(GameObject prefab, Vector2 p)
{
GameObject spawned;
spawned = Spawn(prefab, p);
Character ch;
if (ch = spawned.GetComponent<Character> ()) {
//ch.StartSetup ();
} else
Debug.LogError ("Couldn't find character component on spawn in factory!");
//return as reference
return spawned;
}
public static GameObject SpawnSpeechBubble(GameObject prefab, Vector2 p, GameObject instigator, string _sentence, SpeechScript.Mood _mood)
{
GameObject spawned;
Debug.Log ("Spawning speech bubble!");
SpeechBubble sb;
spawned = Spawn (prefab, p);
//return as reference
//SpeechBubble
if (sb = spawned.GetComponent<SpeechBubble>())
{
Debug.Log("prefab was speechbubble");
if (!sb.isActiveAndEnabled)
{
sb.gameObject.SetActive(true);
}
sb.Setup(instigator, _sentence, _mood);
}
return spawned;
}
Comment