Skip to content

Instantly share code, notes, and snippets.

@videlais
Created October 5, 2018 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save videlais/4e7e23c193ee47f62d9039ff7607cc23 to your computer and use it in GitHub Desktop.
Save videlais/4e7e23c193ee47f62d9039ff7607cc23 to your computer and use it in GitHub Desktop.
A very basic example of using Tracery to create dialogue
// Create two Tracery objects
var tracery = require("tracery-grammar");
// Create a Person class
function Person(id) {
this.id = id;
this.say = function(words) {
console.log(id + ": " + words);
}
}
// Create two simple people objects
var personA = new Person("A");
var personB = new Person("B");
var symbols = {
"like": ["benches", "trees", "grass"]
};
// Create a grammar
var grammar = tracery.createGrammar(symbols);
// Set what they like
personA.like = grammar.flatten("#like#");
personB.like = grammar.flatten("#like#");
personA.say("I like " + personA.like + ".");
if(personA.like == personB.like) {
personB.say("I also like " + personA.like);
} else {
personB.say("I don't like that. I like " + personB.like + ".");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment