MonoGame
← Bring content into your game
MonoGame 3.8 · DesktopGL · C# / .NET
Start with one visible sentence. This small example reads the real Harbour export directly; it is not a complete dialogue player. Then connect the data to your game UI.
1 · Prepare files and scene
Start with a MonoGame DesktopGL project. Place story.json in Content/ and copy it as a raw file into the output directory (see project entry below). Add ReadWelcome to Game1. In the Content Pipeline Tool create a SpriteFont named DialogueFont, build content, and load it with Content.Load
story.json · Export files explained
2 · Show the first text
<ItemGroup>
<None Update="Content/story.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>Add fields SpriteFont _dialogueFont; and string _dialogueLine = ""; to Game1. In LoadContent use _dialogueFont = Content.Load
// Game1.cs: add using System.IO; using System.Text.Json; using System.Text;
// Place this method inside the existing Game1 class.
private string ReadWelcome(string locale)
{
using var stream = TitleContainer.OpenStream("Content/story.json");
using var document = JsonDocument.Parse(stream);
var content = document.RootElement.GetProperty("content");
const string id = "fd7b61a3-8233-4ba7-8f77-b88b119a91bf";
var message = content.GetProperty("texts").GetProperty(id).GetProperty("sourceMessage");
if (content.GetProperty("translations").TryGetProperty(locale, out var table)
&& table.TryGetProperty(id, out var translation))
message = translation.GetProperty("message");
var line = new StringBuilder();
foreach (var segment in message.GetProperty("segments").EnumerateArray())
{
if (segment.GetProperty("kind").GetString() != "text")
throw new InvalidDataException("Text-only example");
line.Append(segment.GetProperty("text").GetString());
}
return line.ToString();
}Expected: “Welcome to the harbour. I am Mira.” With locale = "de" or the language parameter de: “Willkommen im Hafen. Ich bin Mira.” A missing language uses source text. This sample supports text segments; it deliberately does not silently discard placeholders.
3 · From one sentence to a conversation
The fixed text ID above is for the first test. In your game choose flowRef/entryRef from story-index.json, read execution.entries[entryRef].nodeRef, then execution.nodes[nodeRef]. The current dialogue supplies text.textRef and speakerRef. Continuations depend on node kind.
Name/dialogue: SpriteBatch.DrawString inside Begin/End. Handle wrapping and font glyph coverage for longer text. Answer rectangles and mouse/keyboard input belong to the game; retain IDs rather than translated text. End: turn off drawing/input with dialogueOpen = false, not Game.Exit().
- Continue and End: follow connections, close the text box
- Choices: answer ID, order and selected continuation
- Conditions and variables: ten or two gold
- Language: the same IDs, newly resolved content
- Command: give an item or open a shop
- Call/Return: visit a substory and return
Inspect answers and the gold check in this language
Read the data for a choice
Insert this fragment where story is available after loading (for RPG Maker: const story = $gameTemp.vnleExample.story;). It prints IDs for inspection. Replace the output with your answer buttons. It covers the two Harbour answers and their following gold check, not a general interpreter.
// Inside ReadWelcome while document is alive; inspect data before returning.
var nodes = document.RootElement.GetProperty("execution").GetProperty("nodes");
var choice = nodes.GetProperty("42edfff8-b8fe-46bf-ae0d-9e2347b6e726");
foreach (var idToken in choice.GetProperty("optionOrder").EnumerateArray())
{
string optionId = idToken.GetString();
var option = choice.GetProperty("options").GetProperty(optionId);
System.Console.WriteLine(optionId + " " + option.GetProperty("text").GetProperty("textRef").GetString());
}
var branch = nodes.GetProperty("c1c3c372-0a73-4cd0-851d-ede59d5709bb");
int gold = 10; // Repeat with 2.
int minimum = branch.GetProperty("condition").GetProperty("right").GetProperty("value").GetProperty("value").GetInt32();
var next = branch.GetProperty(gold >= minimum ? "whenTrue" : "whenFalse");
System.Console.WriteLine(next.GetProperty("nodeRef").GetString());
// Copy strings/values you need later before disposing JsonDocument.With 10 gold, next refers to “Pay five gold”; with 2 it refers to “Not enough gold”. In the actual game, run the check only when the selected path reaches this node. Each selected answer supplies its own continuation.
4 · Map your own images
This supplementary excerpt expects a current dialogue object named node and the UI elements named in its comments. For C++ and MonoGame, speakerRef has already been read as SpeakerId or speakerId. The key is Mira’s actual character ID; the image name and location belong to your game. Insert the lines at the indicated locations.
// Game1 class field, so both LoadContent and Draw can access it:
private readonly System.Collections.Generic.Dictionary<string, Texture2D> portraits = new();
// Inside LoadContent(): import/build mira.png as portraits/mira.
portraits["807b2957-7bab-4a28-83a4-1f3d1589bbc2"] = Content.Load<Texture2D>("portraits/mira");
// Inside a SpriteBatch Begin/End in Draw; speakerId comes from speakerRef:
if (portraits.TryGetValue(speakerId, out var portrait))
_spriteBatch.Draw(portrait, new Vector2(24, 120), Color.White);Resolve speaker names through content.characters[speakerRef].nameTextRef just like dialogue text. Missing mappings should hide the previous portrait or show a placeholder. Images are not selected by language.
Use image references from the complete export
5 · Ship files with the game
Do not load JSON with Content.Load in this example. TitleContainer opens the raw file; the csproj copies it. Build SpriteFont and textures with the Content Pipeline. Use this entry on DesktopGL first; verify other platforms separately.
Check your result
- English and German greetings display correctly; an unavailable language falls back to English.
- When adding flow: try both answers, gold 10/2, the return from the substory and End.
- Run the built game outside your project folder. JSON, fonts and portraits must be available there too.
Official documentation
API and setup references for this approach: