To make balancing stats easier I’d like to have a centralized place to get and set the stats for the objects in my game. Currently I use a struct to contain the data for each object.
public struct ObjectData
{
public string name;
public int points;
public ObjectData (string text, int value) : this()
{
this.name = text;
this.points = value;
}
}
I also have a class where I create instances of the struct and set the specific values. This class acts like a repository of object stats for the game.
public class ObjectStats: MonoBehaviour
{
ObjectData smallBuilding = new ObjectData("Small Building",10);
ObjectData mediumBuilding = new ObjectData("Medium Building", 20);
ObjectData largeBuilding = new ObjectData("Large Building", 40);
}
In the script on the objects in my game I’m trying to use an Enum that uses the same name as the relevant variable name in ObjectStats class.
public enum objectTypes { smallBuilding, mediumBuilding, largeBuilding }
public objectTypes objectType;
public string hudTxt;
private void Start()
{
hudTxt = ObjectStats.(objectType.ToString()).name;
}
However its not working. Can anyone suggest the right way to approach doing something like this?
Have you looked into using scriptable objects? Seems like they would be perfect for this type of thing. Something like :
[CreateAssetMenu(fileName = "New Object Type", menuName = "Create New Object Type")]
public class MyObjectType : ScriptableObject
{
public List<ObjectStat> stats;
}
They you could create stats for objects as files in your project for your game object components to reference from within the scene.
Hi. Thanks for the heads up on the Scriptable Objects. They seem to come with some performance benefits but unless I’m missing something they don’t offer the centralization I’m looking for? You have to make individual files for each different object? I’m used to using spreadsheets and the thought of having to open millions of individual files to balance the game is not appealing at all.
Im not sure if I understood the question, but maybe you are looking for a Dictionary? You could use the enum as key for better performance instead of strings, so your ObjectStats would contain a property of type Dictionary<objectTypes, ObjectData> and then you can access the ObjectData by using the correct enum key (of course you have to fill the dictionary values before you access the key)
+1 to this. C# is not an interpreted language - you can’t do things like access a variable based on the name of that variable at runtime (unless you use reflection which is slow and unwieldy). A Dictionary is the simplest way to go - you can build up the dictionary itself in a static constructor, or initialize it lazily in a property getter.