- Home /
Formatting Data Classes
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)
Answer by whydoidoit · Apr 09, 2013 at 02:03 PM
Just use the class initializer rather than writing a constructor:
var x = new SomeClass { aVatiable = soneValue, anArray = new [] { value1, value2 } };
Etcetera
So far seems like the best solution so far. I just liked passing it to the constructor just because its nice to know if i forget an arg somewhere, itll throw an error. Ill just use this though for now
You can make really required parameters a constructor requirement and use the class initializer for the rest if you like.
Would you be better off making them all serializable and configuring them in the inspector?
Answer by frarees · Apr 09, 2013 at 01:49 PM
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.
Well, the end goal was to be getting all this data from an X$$anonymous$$L file which i guess means id be doing this all at run time. I created my own custom class ($$anonymous$$achineData and $$anonymous$$achineHandler) to take all the different machine data and store them all in an array in the handler.
The problem is that when making a $$anonymous$$achineData instance, i pass like 15+ variables to it which just looks super messy. I needed a solution to this problem
So, the class doesn't store data but loads it, isn't? If you just need those specific fields you declared and you want to define them as you do (one field per data value or set of values you're gonna get), then you need to set all those fields individually, yes. We could talk about a better way to handle your specific problem, but if your question is strictly about feeding those fields at construction time, then the class initializer is a quite clear & easy solution.
Answer by danrayson · May 01, 2013 at 09:33 PM
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 :P Besides, this post is really old.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# Syntax Question 3 Answers
Reading XML Data C# 2 Answers
Most convenient and fast way to persist/access/modify many values from scene to scene? (C#) 0 Answers