I have some classes that store data.
An example of one such class has this…
public string name;
public string description;
public GameObject prefab;
public GameObject ghostPrefab;
public int buildTime;
public ItemData[] inputMaterials;
public Vector3[] inputOffsets;
public Vector3[] inputRotations;
public bool[] inputRotationDependent;
public int[] inputCapacity;
public ItemData[] outputMaterials;
public Vector3[] outputOffsets;
public Vector3[] outputRotations;
public int[] outputProductionTimes;
As of now I tried expanding the constructor to hold take all of these as arguments but it just looks ridiculous. Should I make individual structs for the input and output material data to cut down on the things that get passed to the constructor? That’s what I’ve already started on but what if I add more? I dont want to end up having structs within structs or something for my data. It just sounds like a real hassle.
Should I use structs or is there a better way to do this (passing many arguments to the constructor)?
EDIT: I should also mention, all of these classes are stored in a large array (for the many different objects that use this data)
Just use the class initializer rather than writing a constructor:
var x = new SomeClass { aVatiable = soneValue, anArray = new [] { value1, value2 } };
Etcetera
If you want to store that data on the HDD, inherit from ScriptableObject
. You’ll be able to see those through inspector too (and extend/redesign it to fit your needs).
If you want all those fields to be filled/retrieved at runtime, inherit from MonoBehaviour
, create a GameObject on the scene and fill all those from that GameObject
’s inspector.
If none of those options fit you and want to go “the OOP way”, you need to fill those manually. However you still need to deal with saving those to disk, so I recommend you going the ScriptableObject
way.
From what I gather you want to define the values of the class quickly and neatly. I reckon the best way is to pass a struct into a “Load” method on the class, no matter what form that struct comes in or from what source (ex: this could be a DataRow from a SQL call), it’s the cleaning way to load in values at runtime. By doing this you’d be externalising the actual definition of the values you’re loading in as well as the time when you actually load it, and suits your external XML file philosophy well.
There’s another way, which is to inherit from the original class, calling it a suitable name, and defining the values of the base class within the constructor of the child. This would keep all your data class value definitions in one place, in your project folder, no need for external data retrieval. You can also be certain the data is already present whenever you try to use it.
As for structs within structs, you should be thinking along the lines of having an array of data associated with a object that you load into it by referencing that particular array of data by a unique identifier (like an id). So for example MyObjectCar.Load(intCarId); Would load in that array of data. You would reference child object’s structure Ids within the array you loaded into your parent class, and load those on demand, quite possibly the same time you load your parent.
This is all way off the mark though if you’re wanting to calculate those values you’re passing in on the fly, and it’d require another post for that
Besides, this post is really old.