Organize data in a multi-column table
Hi,
As a complete beginner, I'm working for a study project where I need to do a couple actions and export the resulting data. The way I export this data doesn't matter but it needs to be organized as a 3 columns table where the first column contains strings, the second floats and the third ints. I also need to increase the length of this table at runtime.
How can I manage to do this? How will I be able to add a whole new line (length)? How will I be able access a specific value of this list?
Thanks a lot
Answer by Bunny83 · Feb 13, 2018 at 12:04 PM
What do you mean by "export"? Do you mean visually or just to export the data to a data format like JSON, XML, ...? Since you said "organise" it sounds like a pure programming problem. However if you actually mean to visually display the data it would be a completely different problem.
To represent the data in C# the best solution for a fix data values per "line" / data record would be to simply use a class or struct
[System.Serializable]
public class DataRecord
{
public string nameOfFirstColumn;
public float nameOfSecondColumn;
public int nameOfThridColumn;
}
The actual "table" would be just a List<DataRecord>
List<DataRecord> table = new List<DataRecord>();
To add a new record you just do
DataRecord row = new DataRecord();
// set your values of "row".
table.Add(row);
To export the data to JSON you can either represent each "row" as nested array or as object
[
["some string", 0.45, 5],
["some string", 0.45, 5],
["some string", 0.45, 5],
]
Of course when exporting as object the data values do not have a specific "order" but they are named properties. Something like
{
{
"name":"some string",
"weight": 0.45,
"value": 5
},
{
"name":"some string",
"weight": 0.45,
"value": 5
},
{
"name":"some string",
"weight": 0.45,
"value": 5
},
}
JSON text can be created in several ways. One would be to use Unity's JsonUtility class. However it's very limited as it's strictly bound to actual classes and variable names. I've created the SimpleJSON framework which allows easy creation and parsing of JSON text.
Another common export format for tables would be CSV, so just a comma / character seperated values. Such files can easily be opened in Excel or similar programs.
some string;0.45;5
some string;0.45;5
some string;0.45;5
Those can be easily constructed since it's just plain text with a seperator.
If you want to visually display the data inside your Unity application you have to look into the UI system. There are too many ways how you can tackle this so without further details i will stop here ^^.
Thanks for this awesome answer!
I've never used or seen this DataRecord thing. Can you explain it a bit further?
For exemple, if I need to add "car", "0.25", "3" to the list, how can I proceed?
At first I create the actual list with List table = new List(); Then, in a "CreateRow" function, I would do as you said earlier. But how can I change the value of "row" so that it contains my 3 informations?
DataRecord is just a custom name for a class. It can be anything you like. This comment tells me you don't know much about object oriented program$$anonymous$$g, right?
To extend my example above to set the 3 values you would do this:
DataRecord row = new DataRecord();
row.nameOfFirstColumn = "car";
row.nameOfSecondColumn = 0.25f;
row.nameOfThridColumn = 3;
table.Add(row);
Of course you should rename "nameOfFirstColumn", "nameOfSecondColumn", ... to something that makes sense. You have presented your question in an abstract way so we have no idea what those "columns" should represent. You should pick proper variable and classnames. For example in the JSON example above i used "name" for the string value, "weight" for the float value and "value" for the int value. Though it depends on the usage.
Unity's Transform class for example represents the 3d transformation of a gameobject which includes the scale, rotation and the translation (position) or the object. Therefore the class contains a property names "position", "rotation" and "localScale".
As you guessed I only know half of the basics of program$$anonymous$$g but with your explanation it now makes perfect sense!
For the export part I don't know how I want to do it yet, as a txt or xls file.
To explain my project a bit, you would have a menu where you can choose between 4 paths. Each path will have its own list, feeded with strings and floats the way you showed me above. Then, back to the menu, you would be able to create a file gathering all 4 lists.
Your answer
Follow this Question
Related Questions
Keeping track of dynamic positions of items in a grid using List/Array? 0 Answers
I want to add items from a list into an array using the editor and popup menus 0 Answers
How to compress iterative code, perhaps using a list and/or loops? 1 Answer
Get all Children of an Object with a certain component 0 Answers
Assign role randomly from array for my online game 2 Answers