Working with Source Code (C# 7.3)
Accessing game data during runtime is possible by utilizing the generated source code.
This section provides examples using default class names, but it is possible to customize class names during the source code generation process. Additionally, this customization allows to avoid naming collisions with existing code.
Loading Game Data
The following C# code creates GameData
class and loads your game data into memory.
using System.IO;
var fileStream = File.OpenRead("RpgGameData.gdjs"); // or .json
var gameData = new GameData(fileStream, new Formatters.GameDataLoadOptions {
Format = Formatters.Format.Json,
// Patches = new [] { patchStream1, patchStream2, ... }
});
fileStream.Dispose();
The file RpgGameData.gdjs
could be published game data or original database file (.gdjs or .gdmp).
Accessing Documents
You can access your documents as a list:
var allHeroes = gameData.AllHeroes.AsList // -> IReadOnlyList<Hero>
var heroes = gameData.Heroes.AsList // -> IReadOnlyList<Hero>
Or you can access specific documents by their Id
or Unique properties:
var heroById = gameData.AllHeroes.Get(heroId); // -> Hero
var heroByName = gameData.AllHeroes.ByName().Get(heroName); // -> Hero
Settings
schemas are accessed by name:
var startingHeroes = gameData.StartingSet.Heroes; // -> IReadOnlyList<Hero>
Formulas
Formulas are executed with Invoke method:
var reward = gameData.LootSettings.RewardFormula.Invoke() // -> int
Formula’s parameters are passed as arguments of Invoke
method.
Extension of Generated Code
Partial Classes and Methods
When generating source code for game data, the resulting C# classes are declared as partial. This means that the classes can be extended by the programmer to add custom functionality.
For example, let’s say that you have generated a GameData
class for your game data. This class contains properties and methods for accessing and manipulating the data. However, you want to add some custom functionality to this class, such as a method for getting specific documents by criteria.
To do this, you can create a new C# file and declare a partial class with the same name as the generated GameData
class. You can then define your custom method in this class, and it will be merged with the generated class at compile time.
Here is an example of how this could look:
In this example, the GameData
class is declared as partial, and two partial classes are defined with the same name: one generated by the source code generation process and one containing custom code added by the programmer.
By using partial classes in this way, you can extend the functionality of the generated classes without modifying the generated code directly. This allows you to keep your custom code separate from the generated code, making it easier to maintain and update your game data classes over time.
There is also extension point on GameData
class:
partial void OnInitialize(); // Called after loading and prepping all data.
Customizing Attributes
You can append additional C# Attributes to the generated classes and their properties by modifying the Specification
field of the related schema or property.
Attributes are specified using the csAttribute
key in the Specification
string, which uses the application/x-www-form-urlencoded
format.
To help construct the correct value, you can use a spreadsheet formula (e.g., in Excel or Google Sheets):
# Place your attribute in cell A1
=TEXTJOIN("&", 1, IF(ISBLANK(A1), "", "&csAttribute=" & ENCODEURL(A1)))
Alternatively, use JavaScript to generate the encoded string:
const params = new URLSearchParams();
params.append("csAttribute", "Obsolete(\"Value is obsolete, do not use\")");
console.log(params.toString());
// → csAttribute=Obsolete%28%22Value%20is%20obsolete%2C%20do%20not%20use%22%29
After obtaining the encoded string, append it to the existing Specification
value.
Example:
# Original Specification value:
icon=material&group=Metadata
# New attribute to add:
csAttribute=Obsolete%28%22Value%20is%20obsolete%2C%20do%20not%20use%22%29
# Final Specification value:
icon=material&group=Metadata&csAttribute=Obsolete%28%22Value%20is%20obsolete%2C%20do%20not%20use%22%29
You can add multiple attributes by including multiple csAttribute
keys:
csAttribute=Serializable&csAttribute=DebuggerDisplay%28%22Id%3D%7BId%7D%22%29
These attributes will be emitted directly into the generated C# code, attached to the appropriate class or property.