A C# port of Tracery, a text generation library/language/tool originally designed by Kate Compton. Primarily intended to be used within the Unity engine.
This project adds the ephemeral extensions of Tracery to Max Kreminski's C#/Unity port.
Download the Source
directory from this repository and drop it into your Unity project's Assets
folder. Alternatively you can download the Sample Project
in this repository and arrive at the same point.
No matter what you're trying to do, the first step will almost always be to get your hands on a Grammar
object. There are a few different ways to do this.
In the Unity editor, select a game object to which you'd like to attach a grammar. Then, in the inspector, click Add Component > Scripts > Tracery Grammar
. A GUI editor for the grammar will appear. Here, you can add and remove rules, edit existing rules, and test the grammar by viewing examples of generated strings.
Once you're satisfied with your grammar, you can access it in your scripts as follows:
GameObject go = GameObject.Find("foo"); // the GameObject to which you attached the TraceryGrammar script
Grammar grammar = go.GetComponent<TraceryGrammar>().Grammar;
Add the Ephemerald grammar file containing your serialized grammar to the Assets/Resources
directory within your Unity project. Then you can access it in your scripts as follows:
TextAsset jsonFile = Resources.Load("grammar") as TextAsset; // assuming the file is at Assets/Resources/grammar.json
Grammar grammar = Grammar.LoadFromCCC(cccFile);
You can also use Grammar.LoadFromCCC(string cccString)
to load a grammar directly from an Ephemerald string.
Add the JSON file containing your serialized grammar to the Assets/Resources
directory within your Unity project. Then you can access it in your scripts as follows:
TextAsset jsonFile = Resources.Load("grammar") as TextAsset; // assuming the file is at Assets/Resources/grammar.json
Grammar grammar = Grammar.LoadFromJSON(jsonFile);
You can also use Grammar.LoadFromJSON(string jsonString)
to load a grammar directly from a JSON string.
In a C# script, you can create a Grammar
object directly using the public constructor. Then you can programmatically populate it with rules using the PushRules
method:
Grammar grammar = new Grammar();
grammar.PushRules("origin", new string[]{"Hello, #name#!"});
grammar.PushRules("name", new string[]{"Max", "world"});
...but note that this approach can get unwieldy pretty quickly, especially for more complicated grammars.
Once you've acquired a Grammar
object, you can use the Flatten
method to generate a fully expanded string from an initial base string. For example:
string expanded = grammar.Flatten("#origin#"); // assuming the grammar has a rule named 'origin'
EphemeraldUnity is still incomplete, but you should be able to use most of the basic syntax described in the Tracery tutorial.
Much like Tracery itself, you can make TracerySharp deterministic by setting Tracery.Rng
to an instance of System.Random
constructed with a specified seed:
Tracery.Rng = new System.Random(42); // replace 42 with whatever seed you want
Martin Pichlmair is currently working on extending this port of Tracery. He's the author of the highly unpopular and obscure Ephemerald editor that allows nicer Tracery editing than fumbling around with JSON.
Tracery was originally designed and developed by Kate Compton.
Max Kreminski ported it to C# for use with Unity.
JSON parsing is done with SimpleJSON. SimpleJSON was originally developed by Bunny83 and later modified by oPless.