Last updated
Was this helpful?
Last updated
Was this helpful?
JSON files can be added to FlatRedBall projects. JSON files are treated as content and their membership in the game's .csproj is automatically managed by FlatRedBall, but they must be manually loaded in code. JSON files can be used to store definition data - data which is defined during the development of a game and which does not change while the game is running. Examples of game definition data incude
Weapon damage, cost, rarity, and description text
Enemy health, movement speed, and experience awarded
Level difficulty rating, preview image name, and description text
JSON can also be used to store game progress or game settings, but these files are usually created and loaded during runtime so they are not added to the FlatRedBall Editor.
Both JSON and Comma Separated Value files can be used to store data. JSON data provides more flexibility and also can integrate with more standardized deserializers such as JSON.net and System.Text.Json. However, due to this flexibility FlatRedBall does not automatically generate classes for deserialization. You must create your own to load a JSON file.
Ultimately both JSON and Comma Separated Values can be used to store and load definition data, so usage often comes down to developer familiarity.
The first step in loading a JSON file is to define either the JSON or the desired C# class for JSON. You can start with either depending on which you are more familiar with.
If you are more familiar writing C#, then you can define your class and create a simple example object which can be used to create your JSON. For example, the following code could be used to define definition data for enemies in a game:
The output JSON might look similar to the following text:
Notice that the generated C# does not include names like Enemy and EnemyData so you may need to make modifications to the generated C# to make it more expressive.
Once you have created a JSON file and a C# file, you can add the JSON to FlatRedBall. JSON files can be added to any location - Global Content, Screen Files, or Entity files. Since you will be performing the loading in custom code, the only difference between these locations is organization. JSON data tends to be relatively small compared to other game data (such as textures and audio), so adding it to Global Content is usually preferred.
To add a JSON file to your project:
Save the file to disk in its desired location, such as your game's content folder ( Content\GlobalContent
)
Drag+drop the file into the Global Content Files in the FlatRedBall Editor
Now the file is part of your project, so you can load it in your custom code. For example, the following code could be used to load the data in Game1.cs:
We can add a breakpoint to verify that enemyData contains the data we added to our JSON file:
Notice that EnemyData includes a constructor which populates the Data list - this is only being done temporarily to obtain valid JSON. You can use similar code to obtain a valid data object which can be used to create JSON. You can either add this code to a C# console project and serialize the JSON, or you can use an online tool such as
if you are more familiar writing JSON text first, you can write this JSON and use online converters to create C# classes. For example, the code above could be used on the website to produce the following C# classes:
The contents of the JSON are loaded using FromFileText. For more information, see the page.