Skip to content

Commit

Permalink
Add 'visited' and 'visited_count' functions to console compiler and r…
Browse files Browse the repository at this point in the history
…unner
  • Loading branch information
desplesda committed Sep 23, 2023
1 parent 03970c5 commit 11c3763
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/YarnSpinner.Console/Commands/RunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ namespace YarnSpinnerConsole
using System;
using System.IO;
using System.Linq;

using Yarn;

public static class RunCommand
{
public static void RunFiles(FileInfo[] inputs, string startNode, bool autoAdvance)
Expand Down Expand Up @@ -38,6 +39,24 @@ string TextForLine(string lineID)
LogErrorMessage = (m) => Log.Error(m),
};

dialogue.Library.RegisterFunction("visited", (string nodeName) =>
{
var visitedCountVariableName = Library.GenerateUniqueVisitedVariableForNode(nodeName);
return storage.TryGetValue<int>(visitedCountVariableName, out var count)
? count > 0
: false;
});

dialogue.Library.RegisterFunction("visited_count", (string nodeName) =>
{
var visitedCountVariableName = Library.GenerateUniqueVisitedVariableForNode(nodeName);
return storage.TryGetValue<int>(visitedCountVariableName, out var count)
? count
: 0;
});

dialogue.SetProgram(program);
dialogue.SetNode(startNode);

Expand Down
24 changes: 24 additions & 0 deletions src/YarnSpinner.Console/YarnSpinnerConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,30 @@ public static CompilationResult CompileProgram(FileInfo[] inputs)

var compilationJob = CompilationJob.CreateFromFiles(inputs.Select(fileInfo => fileInfo.FullName));

// Declare the existence of 'visited' and 'visited_count'
var visitedDecl = new DeclarationBuilder()
.WithName("visited")
.WithType(
new FunctionTypeBuilder()
.WithParameter(Yarn.Types.String)

Check failure on line 285 in src/YarnSpinner.Console/YarnSpinnerConsole.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Types' does not exist in the namespace 'Yarn' (are you missing an assembly reference?)

Check failure on line 285 in src/YarnSpinner.Console/YarnSpinnerConsole.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Types' does not exist in the namespace 'Yarn' (are you missing an assembly reference?)
.WithReturnType(Yarn.Types.Boolean)

Check failure on line 286 in src/YarnSpinner.Console/YarnSpinnerConsole.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Types' does not exist in the namespace 'Yarn' (are you missing an assembly reference?)

Check failure on line 286 in src/YarnSpinner.Console/YarnSpinnerConsole.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Types' does not exist in the namespace 'Yarn' (are you missing an assembly reference?)
.FunctionType)
.Declaration;

var visitedCountDecl = new DeclarationBuilder()
.WithName("visited_count")
.WithType(
new FunctionTypeBuilder()
.WithParameter(Yarn.Types.String)

Check failure on line 294 in src/YarnSpinner.Console/YarnSpinnerConsole.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Types' does not exist in the namespace 'Yarn' (are you missing an assembly reference?)

Check failure on line 294 in src/YarnSpinner.Console/YarnSpinnerConsole.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Types' does not exist in the namespace 'Yarn' (are you missing an assembly reference?)
.WithReturnType(Yarn.Types.Number)

Check failure on line 295 in src/YarnSpinner.Console/YarnSpinnerConsole.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Types' does not exist in the namespace 'Yarn' (are you missing an assembly reference?)

Check failure on line 295 in src/YarnSpinner.Console/YarnSpinnerConsole.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Types' does not exist in the namespace 'Yarn' (are you missing an assembly reference?)
.FunctionType)
.Declaration;

compilationJob.VariableDeclarations = (compilationJob.VariableDeclarations ?? Array.Empty<Declaration>()).Concat(new[] {
visitedDecl,
visitedCountDecl,
});

CompilationResult compilationResult;

try
Expand Down

0 comments on commit 11c3763

Please sign in to comment.