I am working on a room/rule driven system
- a
rule
is a defined in a prefab - a
room
is defined in a data structure, and has values based off a specificrule
I am a little confused as to where to store the GameObject
references.
//references (yes the names are meaningful to my game):
public GameObject C01; public GameObject C02;
public GameObject C03; public GameObject C04;
public GameObject C05; public GameObject C06;
public GameObject S01; public GameObject S02;
public GameObject R00; public GameObject R01;
public GameObject R02; public GameObject R03;
public GameObject R04; public GameObject R05;
public GameObject R06; public GameObject R07;
public GameObject R08; public GameObject R09;
public GameObject R10; public GameObject R11;
public GameObject R12; public GameObject R13;
public GameObject R14; public GameObject R15;
public GameObject E01; public GameObject E02;
public GameObject E03; public GameObject E04;
any of these can be interchanged with another, but I need to be able to change them without having to throw around a bunch of object references.
what I have tried:
I tried to place the references into the Room
structure, but this means that each room
instance has a reference to all possible rules
this means that if I need to change out what rule
is being used it is straight forward, but it means that every room
instance will have a large footprint.
I tried to place the references into an external (non-MonoBehavior) class, I would either need to give each room an instance of this class which puts me back at the memory foot print situation, or to make the class static, but the compiler throws that I can’t have instance variables in a static class.
I tried to place the references into the Holder class (where the room
objects will be worked with), but this means that I will be passing the references back, and forth especially if I need to possibly change the reference for that room at some point (completely possible, and required capability)
what am I missing here? Is there an approach that I am missing? (examples would be greatly appreciated)
Update:
// room definition
[System.Serializable]
public class Room{
public GameObject roomRule;
// if I place references here it will give a large memory footprint per instance
// other members/methods removed
}
public class LevelManager : MonoBehaviour {
public List<Room> rooms;
// if I place references here it means that they have to be passed as arguments
// other members/methods removed
}
//static class to hold definitions of the GameObjects
public static class Definitions{
public GameObject C01; public GameObject C02;
public GameObject C03; public GameObject C04;
public GameObject C05; public GameObject C06;
public GameObject S01; public GameObject S02;
public GameObject R00; public GameObject R01;
public GameObject R02; public GameObject R03;
public GameObject R04; public GameObject R05;
public GameObject R06; public GameObject R07;
public GameObject R08; public GameObject R09;
public GameObject R10; public GameObject R11;
public GameObject R12; public GameObject R13;
public GameObject R14; public GameObject R15;
public GameObject E01; public GameObject E02;
public GameObject E03; public GameObject E04;
public static GameObject GetDef(int num){
switch(num){
case 0: return R00;
case 1: return C01; case 2: return C02;
case 3: return C03; case 4: return C04;
case 5: return C05; case 6: return C06;
case 7: return S01; case 8: return S02;
case 9: return R01; case 10: return R02;
case 11: return R03; case 12: return R04;
case 13: return R05; case 14: return R06;
case 15: return R07; case 16: return R08;
case 17: return R09; case 18: return R10;
case 19: return R11; case 20: return R12;
case 21: return R13; case 22: return R14;
case 23: return R15;
case 24: return E01; case 25: return E02;
case 26: return E03; case 27: return E04;
default: return null;
}
}
}
removing static means that I need an instance giving that to the rooms would leave the same memory footprint as putting the references in there, and giving the instance to the LevelManager
means that I would still have to pass the references around. If I could get the static definitions class to work then I think it would be beneficial.