Created
March 8, 2021 03:23
-
-
Save desplesda/8ac6a316abccb91a16fb003892bcd8b4 to your computer and use it in GitHub Desktop.
Sample code for runtime compilation and loading of Yarn scripts in Yarn Spinner 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Demo : MonoBehaviour { | |
public DialogueRunner dialogueRunner; | |
private void LoadYarnFilesAndRun(string sourceFilesDirectoryPath) | |
{ | |
Debug.Log($"Compiling and loading Yarn files in {sourceFilesDirectoryPath}"); | |
var yarnFiles = Directory.EnumerateFiles(sourceFilesDirectoryPath, "*.yarn") | |
.Concat(Directory.EnumerateFiles(sourceFilesDirectoryPath, "*.yarn.txt")) | |
.ToList(); | |
if (yarnFiles.Count() == 0) { | |
Debug.LogWarning("No Yarn files exist in the directory."); | |
return; | |
} | |
var allPrograms = new List<Program>(); | |
var allStringTables = new List<IDictionary<string, StringInfo>>(); | |
foreach (var filePath in yarnFiles) { | |
var fileName = Path.GetFileNameWithoutExtension(filePath); | |
var fileContents = File.ReadAllText(filePath); | |
try { | |
Compiler.CompileString(fileContents, fileName, out var newProgram, out var stringTable); | |
allPrograms.Add(newProgram); | |
allStringTables.Add(stringTable); | |
} catch (Yarn.YarnException e) { | |
Debug.Log($"Error parsing {fileName}:\n{e.Message}"); | |
} | |
} | |
var compiledProgram = Program.Combine(allPrograms.ToArray()); | |
dialogueRunner.Dialogue.AddProgram(compiledProgram); | |
foreach (var stringTable in allStringTables) { | |
dialogueRunner.AddStringTable(stringTable); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment