The LoadAsynchronously property tells Glue to load all content in Global Content Files asynchronously. In other words, the first screen (StartUp Screen) will load even if Global Content Files hasn't yet finished loading its content.
By default LoadAsynchronously is false, which means all content must finish loading before the first screen is displayed.
LoadAsynchronously is safe to use even if the first screen depends on content in Global Content Files. If any code asks the GlobalContent object for a file, but the file has not yet loaded, the primary thread will be stopped until the content is loaded. Therefore, the content will either already be loaded (since it may have been loaded asynchronously prior to the request for the content) or the primary thread may pause until that particular content is finished loaded. If the primary thread is paused, it will be resumed once the requested content has been loaded, rather than waiting for all files in GlobalContent.
All files in GlobalContent have a getter and a setter. This means that you are able to set files at runtime if you would like to update them in custom code. This section will discuss the details of setting files, and why you may want to do this in your projects.
Setting files in GlobalContent at runtime is usually done in situations where you would like the files in global content to be set according to some condition in your game such as being in a demo vs. fullly purchased mode, or if you are going to be downloading updated versions of files from a remote server. The example we will use here is a situation where your game has a demo and full version. Specifically, the demo version only provides access to a few levels, while the full version provides access to all levels in your game.
To create a new project, do the following:
Open Glue
Create a new XNA 4 PC project
Name the project CsvReloadTest
To create our CSV files:
Right-click on Global Content Files and select "Add File"->"New File"
Select "Spreadsheet (.csv)" as the file type
Name the file LevelInfo.csv
Repeat the above steps to create a new CSV file called DemoLevelInfo.csv
Next we'll want to make sure that both CSVs have the same columns - since one will replace the other they both need the same columns to be compatible. Mine look like this:
DemoLevelInfo.csv:
LevelInfo.csv:
We have two files - LevelInfo.csv (which contains all of our levels) and DemoLevelInfo.csv which will contain demo levels. If the game is in demo mode, we will replace LevelInfo with the contents of DemoLevelInfo at runtime.
We want DemoLevelInfo.csv to use the same class as LevelInfo.csv. To do this:
Right-click on DemoLevelInfo.csv
Select "Set Created Class"
Click "New Class"
Enter the name "LevelInfo"
Select the newly-created LevelInfo class
Click Use This Class
Click "Yes" if asked if you should remove unused file
Click Done
For more info on Set Created Class, see this page.
First, let's create a Screen that uses LevelInfo to display which levels are currently available in the game:
In Glue, right-click on Screens
Select "Add Screen"
Name your Screen "LevelSelectScreen"
Now let's make a Text object in our Screen. This Text object will display available levels, as determined by looking at LevelInfo in GlobalContent:
Right-click on the LevelSelectScreen's "Objects"
Select "Add Object"
Verify "FlatRedBall or Custom Type" is selected
Select "Text"
Click OK
Now that we have a Text object, let's set its Text in CustomInitialize of our Screen:
Open your project in Visual Studio
Navigate to LevelSelectScreen.cs
Add the following code to CustomInitialize:
All files in GlobalContent have setters. This means that you can simply set the properties and the rest of your project should use the new instance - assuming you do so before your project accesses these files. We'll add some code to Game1.cs to set our game into demo mode. To do this:
Open Game1.cs in your project in Visual Studio
Add the following code after GlobalContent.Initialize and before ScreenManager.Start in Game1.cs:
In this case our code is simply setting the LevelInfo to DemoLevelInfo - overwriting our LevelInfo without making any checks. If this were a real game, we might want to check if the game should really be in demo mode or not. Therefore, you would wrap this assignment in some checks. Also, we had to put our replacement in a very specific place in the Initialize function in Game 1:
It had to be after GlobalContent.Initialize because we needed DemolevelInfo to already be loaded
It had to be before the ScreenManager's Start function because this had to be set prior to the first Screen being initialized.
Although we assigned one CSV in GlobalContent to another CSV in GlobalContent, this is not a requirement. The LevelInfo could be assigned from a completely different file using the CsvFileManager. This means that you could download a CSV from a server somewhere, and assign LevelInfo to a new Dictionary<string, LevelInfo>, then deserialize the file into that dictionary using CsvDeserializeDictionary. If you are doing any form of assignment on an object in GlobalContent, and you are assigning to something that is not part of GlobalContent, then you can assign the property in GlobalContent before GlobalContent is initialized. GlobalContent will respect this assignment and will not overwrite it when initializing.
If you run your game now you will see that the game has 5 levels (according to LevelInfo):
Now if you run your game you'll see that only the first level is available:
Name (string, required)
TimeLimit (float)
Level1
100
Name (string, required)
TimeLimit (float)
Level1
100
Level2
125
Level3
125
Level4
160
Level5
200
Glue generates a Reload function in GlobalContent which simplifies reloading CSV files. Reload can only be used to reload CSV files from disk.
Reload is useful for games which may change CSVs during execution, such as games that download new data from a remote server. Reload will change the member that is passed to the function but will not re-assign properties that have been set from rows in the CSV. In other words, any objects that reference the old CSV must be re-assigned to values in the new CSV.
The following assumes that the Global Content Files folder in Glue contains a file called EnemyDefinition.csv:
Global Content Files is a special FlatRedBall folder used to store files which are always available when a game is running.
Global Content Files are loaded right when the game starts running, and are never unloaded when transitioning between screens. By contrast, files which are tied to specific screens or entities may not be available until the screen is created, or until an instance of the entity is created, and these files may get unloaded when the screen is destroyed.