Sorry if this sort of thing has already been answered.
I am making a tile based game in C# (think CIV5) and I have a class Building that stores a building object whenever one is created. In the Building class, I could have things like unit name, health, resource cost etc. but somewhere I need to have defined every single building type in my game and all their stats so that I can grab the respective one on initialising a Building.
Somewhere in my game logic ill have something like:
On condition, instantiate Building class object with the name of the building to be made “Barracks”
In Building constructor, use the name to look up the stats already defined for the “Barracks” building type
Populate the Building object with these stats.
Continue game logic
How/where do I store all these predefined building types?
Thanks for the reply, I think that is exactly what I need.
So far I have implemented something along the lines of a Building ScriptableObject class and a number of classes which inherit from the Building class:
public abstract class Building : ScriptableObject
{
public string buildingName;
public GameObject prefab;
public int hitPoints;
public abstract void Initialise(GameObject obj);
}
public class firstRaceBuildings : Building
{
public int armor;
public int durability;
public override void Initialise(GameObject obj)
{
...
}
}
public class secondRaceBuildings: Building
{
public int regen;
public int expansionRate;
public override void Initialise(GameObject obj)
{
...
}
}
This allows me to make scriptable objects for different races that inherit from the base class which they all have in common but can differ in their unique characteristics as well.
My new problem is as follows. Say I wanted to have a bunch of building buttons (like RTS building menu selector) that call a function createBuilding(Building newBuilding). If I pass my button parameter a scriptable object “CommandCenter” which is a firstRaceBuildings scriptable object that inherits from Buildings class, how can I get the info contained within the firstRaceBuildings class such as armor and durability when the object is a Buildings type?
I don’t want to make the function take firstRaceBuildings as a parameter because then I cant pass it secondRaceBuildings or any new race buildings classes that I create. I want to make a generic function that can take anything derived from Buildings and still be able to access the parameters in the derived class.
EDIT: I’m aware I could use overloaded functions for all the different parameters that I want to pass the function. I was just wondering whether I could somehow determine just from the Building object that the scriptable object was using the firstRaceBuildings class.
In C# you can handle these as “Building”. firstRaceBuildings and secondRaceBuldings are Buildings.
So if you write a method like
void AcceptBuilding(Building acceptedBuilding) {}
then you can call that with the subclasses as well.
It also works with interfaces as well (and in general it’s better if you use common interface rather than common ancestor, but in this case I think you’re good).