Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
The CsvHeader class represents a header in a CSV file in the RuntimeCsvRepresentation class. The RuntimeCsvRepresentation class contains an array of CsvHeaders called Headers.
When a RuntimeCsvRepresentation is loaded, it will (by default) contain an array of CsvHeaders. Each CsvHeader represents the first item in a column in a CSV file.
The contents of the cell can be obtained through either the Name or OriginalText properties - they will be initially identical.
By default the IsRequired property will be set to false. Both the IsRequired and Name properties can be modified through the RuntimeCsvRepresentation's RemoveHeaderWhitespaceAndDetermineIfRequired method.
Did this article leave any questions unanswered? Post any question in our forums for a rapid response.
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"]
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:
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:
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 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 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 FromList method will ignore any fields or properties which have the XmlIgnore attribute. This allows objects which are built for XmlSerialization to serialize the same using RuntimeCsvRepresentations.
The RuntimeCsvRepresentation class is a general-purpose class which represents a CSV file. It can be easily created through the CsvFileManager.
RuntimeCsvRepresentations can be loaded as follows:
For more information see the CsvFileManager page.
Did this article leave any questions unanswered? Post any question in our forums for a rapid response.
The GetUserFolder method returns a string which can be used to save and load files in the user folder. To use this, the InitializeUserFolder method must be called first. The return value for GetUserFolder depends on the platform that the call is made on; however, it can be used on any platform with most methods that access the file system through the FileManager.
For an example on how to use GetUserFolder, see the InitializeUserFolder article.
The CloneObject method performs a deep clone of an object. This uses XML serialization so the object that is to be cloned must be XML serializable. This can be used to clone any "save" object, but should not be used on runtime objects such as Sprite.
Example of "save" objects include:
Engine objects with the name "Save" at the end like SpriteSave, SceneSave, and AnimationChainSave
Instances of objects that come from CSVs
Objects which do not contain references to other objects, like Vector3. Although these may not technically be considered "save" objects, they can be properly cloned using CloneObject.
The SaveEmbeddedResource method can be used to save a file which has been compiled as an EmeddedResource to disk. This method is useful for PC tools, especially Gum plugins.
The following will save the redball.bmp image to the given target directory assuming it exists in the current assembly:
As explained in the main CsvFileManager page, you can easily create CSV files to define data classes such as:
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.
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
The FileManager's RelativeDirectory is a directory that is used as the directory that file loading is performed relative to.
For example, let's say the FileManager's RelativeDirectory is
In this case any file-related FRB call will result in FlatRedBall looking in this directory for files. Therefore, we could create a Sprite as follows:
This would result in the engine looking for the following file:
Generated Glue code assumes that the RelativeDirectory is the directory of the .exe file. Therefore, if your code ever changes the RelativeDirectory and you are using Glue, you should change the RelativeDirectory back to its old value. See below on an example of how to preserve RelativeDirectory.
Virtually any FlatRedBall call which access information off of the disk will use FileManager.RelativeDirectory. For example the following calls will use the RelativeDirectory when relative paths:
Any FlatRedBall Save Class's FromFile and Save methods
The RelativeDirectory defaults to the directory of your executable if your application is running on the PC (as opposed to Silverlight or the Xbox 360). This file is determined when execution first starts. In other words, running your application in a different folder or on a different machine will result in a different RelativeDirectory at startup. This keeps file loading portable.
In some cases you may want to change the FileManager's RelativeDirectory property; however, there are some restrictions when you do this:
The RelativeDirectory must always be an absolute path.
The RelativeDirectory should preferably be relative to the location of your executable.
These two requirements might seem contradictory - how can you guarantee that RelativeDirectory stays relative to your .exe, but at the same time achieve that by setting an absolute path?
The answer is to use the old RelativeDirectory value when setting a new RelativeDirectory.
Let's say that you want to set the RelativeDirectory to your "Content" folder, as you plan on doing all loading from that folder. To do this, you'll simply want to append "Content\" to your RelativeDirectory as follows:
This means that your program will use the "Content" folder for all of its loading logic; however, your file access code will remain relative.
You should NOT do this:
If you do this then your game will break if you decide to move it to another computer, if you send it to a friend, or if you switch to a different platform like the Xbox 360 or Silverlight.
Many file formats assume that contained references are relative to the file itself. For example, consider a .scnx file which references "redball.bmp". By default if you try to load "redball.bmp" your program will attempt to look for a redball.bmp file in the same folder as the .exe. If you are writing code which will load objects relative to another file, you may want to temporarily set the RelativeDirectory value, then change it back after you're finished.
For example, let's say that MySaveClass has a list of Sprites that it will load. The code to load this might be as follows: