Hey there,
Long story short, I’m building a sort-of framework with which I can create different game types.
The basic structure consists of an abstract Game
class, which contains all of the non-game-specific functionality along with an array of [Serializable] abstract Level
objects (which contain non-game-specific level info like name and target score to beat). Each of these are subclassed (eg. BP_Game
and BP_Level
), to implement any game-specific functionality and level information.
Everything works fine, except I’m having to abstract/override the methods which relate to the Level
objects in the Game
subclasses.
This is a small example of how I have to have it currently (in separate files):
public abstract class Game : MonoBehaviour
{
public abstract Level[] GetLevels();
// ...
}
public class BP_Game : Game
{
public BP_Level[] levels;
public override Level[] GetLevels()
{
return this.levels;
}
// ...
}
As you can see, it would be the exact same code no matter what subclass, so it would make sense to put it in the abstract base class… I just can’t figure out how to do that! Is it even possible?
I tried adding Level[] levels
to the Game class, but the BP_Level[] levels
did not override it but rather existed alongside it (calling currentGame.GetLevels()
got a nice NullReferenceException
because while the subclass’s levels
variable was set, it was accessing the base class’s one, which wasn’t).
So… Is it actually possible to do what I’m trying? Or have I just wasted an entire morning :D?
Thanks in advance for any help.