A bit of programming inside baseball today! I know most of you are going to chuckle and shake your head slowly, but as of last week, each room class had some code in it that would a) make sure the game controller is loaded (this happens when I playtest by just pushing play in a room I'm working on, the controller is actually loaded in the main menu so the game won't work if I don't do this), b) place the character at the right spot depending on where he came from and c) most rooms with enemies have the code to spawn them.

In theory, I know about inheritance in OOP, but in practice, the fact that room already inherited from MonoBehaviour objects, combined with the fact that I wasn't too sure how it worked in C#, made me do this. And now it's a huge pain because for each of the 1000 room, I have to copy/paste the code. So I just decided to rework that bit, creating a base 'room' class and adding most of the code re-used in it. I think the result is saving me some time overall, although I have to rework all classes now. I probably won't stop there, if I can save time in the future, it's worth to fix now, I think?

BEFORE

public class Room195 : MonoBehaviour {

controller_script cs;
void Start () 
{
if(GameObject.Find ("Controller") == null) Application.LoadLevel ("mainMenu");
cs = GameObject.Find ("Controller").GetComponent<controller_script>();

cs.character.transform.position = GameObject.Find ("entrance"+cs.movingFrom).transform.position;
InvokeRepeating("spawn",0f,5f);
}

void spawn()
{
GameObject megabit = Resources.Load<GameObject>("prefabs/enemies/megabit");
GameObject megabyte = Resources.Load<GameObject>("prefabs/enemies/megabyte");

GameObject[] enemies = new GameObject[2]{megabit,megabyte};
int[] nbMax = new int[2]{1,1};
cs.spawnEnemies (enemies,nbMax);
}
}

AFTER

public class Room195 : room 
{
new void Start () 
{
base.Start ();
invokeSpawn (new string[]{"megabit","megabyte"},new int[]{1,1});
}
}

Currently working on: Re-factoring the rooms

Time spent on the project so far: 211 hours

Some code after the jump!

Posted
AuthorJérémie Tessier