Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
CsvDeserializeList can be used to load a CSV into a list of objects, where the type of the object is passed in to the method call.
The CsvFileManager can be used to load a list of specific types (such as weapon data for an RPG) or a list of strings if there is no appropriate class to deserialize to. The following code can be used to deserialize a CSV to a list of strings:
The CsvFileManager supports CSV (or .txt files) cells with multiple rows. If you are using Text objects to display the contents of a CSV cell, the Text object will obey the new lines.
To enter multiple lines in Excel:
Click on a cell
Begin entering text
Hold down the ALT key and press ENTER to insert a new line
Continue typing
If you are used to programming new lines in strings in C# code, then you may be familiar with the '\n' character. However, FlatRedBall CSVs will not obey the newline character. The reason for this is because in code the '\n' is actually a single character, while writing \n in a CSV actually generates two characters: the '\' character followed by the 'n' character. To represent the \n character as a single character, you just insert an actual newline - this is what ALT+Enter in Excel accomplishes.
The CsvFileManager supports cells that contain color values. Color values can be specified either by standard name or can be custom defined by components (as shown below).
The following shows how to define Colors in a CSV file:
The first two cars (Mustang and Elantra) are defined using standard color names. These names match the XNA Color names. The second two colors are custom colors created by setting the Red, Green, Blue, and Alpha components. The XNA Color struct uses 0 - 255 values for these components so keep that in mind when defining colors in CSV values.
CarName
PaintColor (Microsoft.Xna.Framework.Color)
Mustang
White
Elantra
Green
Corvette
A=255,R=0,G=27,B=128
Camry
A=255, R=255,G=0,B=100
The CsvFileManager is a class which provides functionality for working with .csv files. The CSV file format is a simple file format that can be loaded and saved by spreadsheet programs. Glue provides a lot of functionality for working with CSV files. For information on working with CSV and Glue, see the using CSVs in Glue tutorial.
FlatRedBall engines are built to work well with the XML format. The FileManager provides methods for serializing to and deserializing from XML files, and most FlatRedBall file types are actually XML files for simplified readability, debugging, and to be cross-platform. However, despite its merits, the XML file type has a few disadvantages:
XML files tend to be bloated for the data contained. In many cases most of the data in the XML file comes from the tags rather than the data.
XML files can be difficult to maintain by hand. Despite being human readable, adding new entries without copying/pasting other entries is tedious and error prone.
XML files do not have a standard editor. While there are likely options for specifically editing XML files, most by-hand XML editing is performed in text editors. These editors do not provide any support for the file format.
The CSV format is a human readable format which while still being a text format can be very compact compared to XML files and can be edited in a number of common editors such as Google Docs, Open Office, and Microsoft Excel. These editors are very easy to use, support advanced editing such as the use of formulas, and the first two are completely free!
CSV stands for Comma Separated Values. CSV files can be displayed in spreadsheet programs, or be used as data files in FlatRedBall and Glue. CSV files can also be opened in text editors. Investigating the contents of a CSV file is often not necessary but can be helpful when debugging problems related to CSVs. A typical CSV file may look like this in a text editor:
This file would appear as follows in a spreadsheet program:
The comma is the most common separator of cells (although other separators such as tabs and the | character are supported). Commas are also common in regular written English as well. The CSV format supports commas inside cells. Quotes are used to distinguish between a comma within a cell and a comma separating cells. For example, the following text shows the contents of a CSV file which includes commas.
This would result in the following CSV (notice the quotes do not appear)
Notice that single-quotes around a cell are not included in the contents of a CSV cell. Therefore, quotes may appear around cells even if they do not include a comma (this is the default behavior of OpenOffice):
The CsvfileManager provides methods for loading CSV files and creating lists of items. The code included in this article defines a simple struct which contains properties used to initialize newly-created Sprites. But before getting to the code, we will explain how to create CSV files in Google Docs. The following lists how to create a CSV file using Google Docs.
Go to the Google Docs website: http://docs.google.com/.
Log in to Google Docs. If you have never logged in before, but have a GMail address, use your GMail address and password to log in. Otherwise, you will need to create a new account.
At this point you should have a CSV file saved on your computer. The file should be similar to: Export.csv Either save this file in the the same folder as your project's EXE file, or add it to the solution and set the file to copy.
The following code uses the CSV file from the previous section to create a group of Sprites. Create the following struct:
Add the following to Initialize after initializing FlatRedBall
The following table shows the supported types.
(A) Some applications like OpenOffice Calc convert "true" to "TRUE". FlatRedBall will parse bool values regardless of capitalization. In other words, "TRUE" will be parsed correctly. (B) CSVs support enumerations. If using enumerations in Glue, you need to fully-qualify the enumeration in the type. For example, for a color operation, the header might read:
You do not need to qualify the value. For example, you can simply use "Modulate" instead of "ColorOperation.Modulate" in the cell for a value under a ColorOperation header. (C) FlatRedBall needs a content manager to load Texture2Ds. The CsvFileManager will automatically use FlatRedBallServices.GlobalContentManager if not content manager is explicitly set. You can set the content manager as follows:
[subpages depth="1"]
Select New->Spreadsheet:
Enter the headers for the spreadsheet. These headers will be the fields of the class that we'll be creating later in this article:
Enter values for each of the structs. Each row represents an item in the list that will be created when the CSV file is deserialized. In this case I'll create four entries:
Export the files to a .csv file.
Depending on your browser the CSV file may open right in the browser. If that's the case, save the file out from the browser through the File menu:
The CsvHeader class represents a header in a CSV file in the class. The class contains an array of CsvHeaders called Headers.
When a is loaded, it will (by default) contain an array of CsvHeaders. Each CsvHeader represents the first item in a column in a CSV file.
By default the IsRequired property will be set to false. Both the IsRequired and Name properties can be modified through the .
Did this article leave any questions unanswered? Post any question in our for a rapid response.
Header1
Header2
Header3
FirstValue
SecondValue
ThirdValue
Morning Greeting
Regular Greeting
Good morning
Hello, how are you?
Type
Member Example
CSV Entry Example
bool (A)
public bool IsHungry;
true
double
public double LengthOfTime;
3.244
Enum (B)
public BorderSides Sides;
Top
float
public float X;
1
int
public int CurrentLevel;
3
Matrix
public Matrix OrientationMatrix;
1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1
string
public string FirstName;
Martin
Texture2D (C)
public Texture2D TextureToUse;
redball.bmp
Vector2
public Vector2 GoalPosition;
23,18
Vector3
public Vector3 DirectionToFace;
11,0,-14
Vector4
public Vector4 FourDimentionalVector;
24,18,0,1
List<string> (or lists of any primitive)
public List<string> Names = new List<string>;
See this article
Color
public Color PaintColor;
See this article
Custom types (such as classes and structs defined in your game)
public SpecialAbility AbilityInstance;
see this article
The FromList method will create a RuntimeCsvRepresentation according to the object passed to it. This method can be used to generate a RuntimeCsvRepresentation which can be saved to disk to create a CSV.
The following code will create a RuntimeCsvRepresentation from a list of Vector3's:
The GenerateCsvString method can be used to generate a string in CSV format. The resulting string can be saved to a file to create a CSV which can be opened in any application that supports CSVs.
The following code shows how to use the GenerateCsvString method:
The result string will contain:
As of the December 2011 release of FlatRedBall the CsvFileManager supports entries which are not basic types, but rather full classes and structs. This tutorial will show how to add structs and classes to your CSV file.
Many games require data types which are more complex than can be identified with simple primitives. For example, you may be creating a game which needs enemy types defined. Each row in your CSV may represent an enemy type; however each instance may also need a non-primitive type to define the type of attack damage it does. If these classes were laid out in code, they might look like this:
As we'll see later in the tutorial, using classes and structs also allows for your CSV to benefit from inheritance.
The type for AttackInfo must be fully-qualified - in other words we use "MyGame.DataTypes.AttackInfo", not just "AttackInfo". This requirement may be eased in a future version of Glue.
Each parameter can be assigned to any value valid for the type. Note that order does not matter (AreaOfEffect could be assigned before Damage), and it is not necessary to assign variables. For example, you could simply have "Damage=4" and AreaOfEffect would remain as its default value.
Types used by the CSV must be defined manually Glue will automatically define the type that the entire CSV is using - in this case "EnemyInfo", however types used inside the CSV (such as AttackInfo) must be manually defined inside your current project. This condition may change in future versions of Glue.
Now you can either load this CSV through the CsvFileManager, or you can use the automatically-loaded member created in Glue for the given CSV file.
Objects in CSVs also support inheritance. Inheritance allows you to define a base type for a property on your objects, but then specifying derived types in each individual row. First, we'll modify our data classes as follows:
The CsvFileManager doesn't differentiate between types that you have defined and types that are defined by other libraries (such as types in the XNA libraries). This means that you can immediately add a lot of types of objects to your CSV without ever having to define them in your project. For example, the following shows how to define a column of XNA Rectangles:
Microsoft.Xna.Framework
But what if you wanted to also include a list of modifications on the car. In other words, something like this:
The CsvFileManager understands special syntax allowing for objects represented in a row to actually take up multiple rows, and to define lists.
The class shown above for Car is all that you need to do code-side to prepare your class to take a list of strings. To create a CSV that can support rows, you would want to create a spreadsheet that looks like this:
If using the CSV plugin, the CSV would appear as shown in the following image:
In this case the Ford Taurus has 2 modifications ("Paint protection" and "After-market stereo"). The Toyota Corolla has 3 modifications ("Tinted Windows", "4-way rear speakers", and "Remote start system"). The spreadsheet displayed above has two things that are needed for lists. The first, which is quite obvious, is to declare the type for the appropriate header as a List<string> (or whatever other primitive type you are using). In this case, we're using List<string>, so the header is declared as follows:
This tells the CsvFileManager that the "Modifications" row should be treated as a List of strings, and that entries in the row may belong to the same object. However, this does raise one issue - how is the CsvFileManager to know when one list starts and when one ends. While it may seem obvious to you that the "After-market stereo" modification applies to the Taurus, it is entirely possible that the "After-market stereo" could also belong to a car with an empty Make, Model, and Horsepower. This is why the Make column has "(required)" after its name. This tells the CsvFileManager that this column has a required value, and that this column will define new instances. In other words, since there are two entries in the Make column, the CsvFileManager knows to make two instances when deserializing the CSV.
If you've used CSV files in Glue, then you probably know that Glue will automatically generate classes to match the given CSV format. Glue understands the List<TYPE> syntax described above. In other words, feel free to use this CSV syntax in your own files and Glue will automatically build classes that will match your CSVs.
The RuntimeCsvRepresentation class is a general-purpose class which represents a CSV file. It can be easily created through the .
For more information see the page.
Did this article leave any questions unanswered? Post any question in our for a rapid response.
The FromList method will ignore any fields or properties which have the attribute. This allows objects which are built for XmlSerialization to serialize the same using RuntimeCsvRepresentations.
First we'll start by creating the CSV that contains our information, then we'll explain the syntax. The following is a screenshot from Excel, but you can use any spreadsheet editing program (such as OpenOffice or Google Docs): There's a few things to note here:
The Name property is marked as "required". This is common practice so that the CSV can be deserialized to a Dictionary. For more info on deserializing to Dictionaries in Glue, see .
Using the code above, we now have FireAttackInfo and SlashingAttackInfo, both of which inherit from AttackInfo. We can now instantiate any of those three types in our AttackInfo column. The following shows a CSV that instantiates different types: When using inheritance we must specify the type that we want to use in each column. The syntax for this is the same as it is when instantiating an object in code - use the keyword "new" followed by the type. The contents of the parenthesis are the same as as before. You can assign values defined in the derived class as well as the base class. You can also leave out assignments that you want to keep as the default. For example the EndBoss enemy does not define the AreaOfEffect - it defaults to 0.
As explained in the main , you can easily create CSV files to define data classes such as:
Boundaries (Microsoft.Xna.Framework.Rectangle) |
X=0, Y=0, Width=64, Height=64 |
X=40, Y=30, Width=80, Height=55 |
Make (required) | Model | Horsepower (int) | Modifications (List<string>) |
Ford | Taurus | 200 | Paint protection |
After-market stereo |
Toyota | Corolla | 140 | Tinted windows |
4-way rear speakers |
Remote start system |