Skip to content

Commit

Permalink
make callbacks come through options, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
kkukshtel committed Feb 13, 2022
1 parent d2debb3 commit f9390a7
Show file tree
Hide file tree
Showing 12 changed files with 745 additions and 55 deletions.
18 changes: 0 additions & 18 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,6 @@
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "get dir",
"command": "dir",
"type": "shell",
// "args": [
// "build",
// "./Squiggle/Squiggle.csproj",
// // Ask dotnet build to generate full paths for file names.
// "/property:GenerateFullPaths=true",
// // Do not generate summary otherwise it leads to duplicate errors in Problems panel
// "/consoleloggerparameters:NoSummary"
// ],
// "group": "build",
// "presentation": {
// "reveal": "silent"
// },
// "problemMatcher": "$msCompile"
},
{
"label": "build squiggle",
"command": "dotnet",
Expand Down
1 change: 1 addition & 0 deletions Squiggle.Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ [sampleCustom someArgForACustomCommand]
string myScript = "Speaker: Hello! The Speaker is talking!";
Squiggle.Core.Run( squiggleText : myScript,
runnerOptions : new Squiggle.Runner.Options(){Debug = true});

[SquiggleCommand("sampleCustom")]
public class SampleCustom : SquiggleCommand
{
Expand Down
Binary file modified Squiggle.Test/Squiggle.dll
Binary file not shown.
613 changes: 613 additions & 0 deletions Squiggle.Unity/Assembly-CSharp.csproj

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using Squiggle;

public class SquiggleTextBoxSample : MonoBehaviour
{
public InputField InputField;
public Button RunSquiggle;
void OnEnable()
{
RunSquiggle.onClick.AddListener(() => {
EventSystem.current.SetSelectedGameObject(null);
TryExecuteSquiggle();
});
}
void OnDisable()
{
RunSquiggle.onClick.RemoveAllListeners();
}

void TryExecuteSquiggle()
{
Squiggle.Core.Run( squiggleText : InputField.text,
runnerOptions : new Squiggle.Runner.Options(){
Debug = true,
LogHandler = (text) => Debug.Log(text)
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,51 @@ NavMeshSettings:
debug:
m_Flags: 0
m_NavMeshData: {fileID: 0}
--- !u!1 &5173931
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 5173932}
- component: {fileID: 5173933}
m_Layer: 0
m_Name: SquiggleTextBoxSample
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &5173932
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5173931}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 1031.7043, y: 674.87836, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &5173933
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5173931}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f93d94badc69d1746aace43554641247, type: 3}
m_Name:
m_EditorClassIdentifier:
InputField: {fileID: 689565804}
RunSquiggle: {fileID: 2054484118}
--- !u!1 &9069187
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -1075,7 +1120,7 @@ GameObject:
- component: {fileID: 2054484119}
- component: {fileID: 2054484118}
m_Layer: 5
m_Name: Button
m_Name: RunSquiggleCodeButton
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
Expand Down
5 changes: 5 additions & 0 deletions Squiggle/Squiggle.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@
<Target Name="CopyDLLs" AfterTargets="Build">
<Message Text="Executing CopyDLLs task" Importance="High" />

<!-- Copy into dotnet test project -->
<Copy SourceFiles="$(TargetDir)$(ProjectName).dll;" DestinationFolder="$(ProjectDir)../Squiggle.Test/" />
<Copy SourceFiles="$(TargetDir)Sprache.dll;" DestinationFolder="$(ProjectDir)../Squiggle.Test/" />

<!-- Copy into Unity samples project -->
<Copy SourceFiles="$(TargetDir)$(ProjectName).dll;" DestinationFolder="$(ProjectDir)../Squiggle.Unity/Assets/Squiggle/Plugins" />
<Copy SourceFiles="$(TargetDir)Sprache.dll;" DestinationFolder="$(ProjectDir)../Squiggle.Unity/Assets/Squiggle/Plugins" />

<Message Text="Copied build files" Importance="High" />
</Target>
Expand Down
6 changes: 3 additions & 3 deletions Squiggle/src/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public static SquiggleCommandGroup Parse(string squiggleText)
// Clog.L($"text to parse: {Text.Replace("\n", "D")}"); also works - may need to do this as well for OSX line endings
return SquiggleGrammar.Commands.Parse(squiggleText);
}
public static Runner Run(string squiggleText, Runner.Options runnerOptions = null, Action<Squiggle.Commands.Dialog> dialogHandler = null) => Run(Parse(squiggleText),runnerOptions,dialogHandler);
public static Runner Run(SquiggleCommandGroup group, Runner.Options runnerOptions = null, Action<Squiggle.Commands.Dialog> dialogHandler = null)
public static Runner Run(string squiggleText, Runner.Options runnerOptions = null) => Run(Parse(squiggleText),runnerOptions);
public static Runner Run(SquiggleCommandGroup group, Runner.Options runnerOptions = null)
{
var options = runnerOptions;
if(options == null)
{
options = new Runner.Options();
}
return new Runner(group.SquiggleCommands,options,dialogHandler);
return new Runner(group.SquiggleCommands,options);
}
}
}
24 changes: 11 additions & 13 deletions Squiggle/src/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,12 @@ public class Runner
public Action CompletedExecution;
public Action<SquiggleCommand> CommandExecutionStarted;
public Action<SquiggleCommand> CommandExecutionCompleted;
Action<Squiggle.Commands.Dialog> DialogHandler;
public Runner(List<SquiggleCommand> commands, Options runnerOptions, Action<Squiggle.Commands.Dialog> dialogHandler = null)
public Runner(List<SquiggleCommand> commands, Options runnerOptions)
{
GUID = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
this.commands = commands;
options = runnerOptions;
DialogHandler = dialogHandler;
if(DialogHandler == null)
{
DialogHandler = (command) => {Squiggle.Events.Commands.CompleteDialog?.Invoke(command);};
}
Squiggle.Events.Dialog.EmitDialog += DialogHandler;
Squiggle.Events.Dialog.EmitDialog += options.DialogHandler;

if(runnerOptions.AutoStart)
{
Expand Down Expand Up @@ -88,10 +82,7 @@ void OnCurrentCommandComplete()

public void Cleanup()
{
if(DialogHandler != null)
{
Squiggle.Events.Dialog.EmitDialog -= DialogHandler;
}
Squiggle.Events.Dialog.EmitDialog -= options.DialogHandler;
foreach (var command in commands)
{
workingCommand.CommandExecutionComplete -= OnCurrentCommandComplete;
Expand All @@ -101,16 +92,23 @@ public void Cleanup()

void Log(string text)
{
var logtext = $"Squiggle Runner {GUID}: {text}";
if(options.Debug)
{
Console.WriteLine($"Squiggle Runner {GUID}: {text}");
Console.WriteLine(logtext);
if(options.LogHandler != null)
{
options.LogHandler?.Invoke(logtext);
}
}
}

public class Options
{
public bool AutoStart = true;
public bool Debug = false;
public Action<Squiggle.Commands.Dialog> DialogHandler = (command) => {Squiggle.Events.Commands.CompleteDialog?.Invoke(command);};
public Action<string> LogHandler = null;
}
}
}
56 changes: 36 additions & 20 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,42 +59,62 @@ Squiggle.Core.Run(myScript);
```
The above code will read the text in `myScript` as Squiggle code and attempt to execute it. Squiggle will see that someone name `Speaker` is talking, and they are saying `Hello! The Speaker is talking!`.

If you use the above code, the code will run and then immediately exit. This may not be desireable, as you'll likely want to "catch" the dialog, and handle it somewhere else in your code. You can specify a dialog callback with the `dialogHandler` parameter to handle dialog when running Squiggle code to achieve this:
### Runner Options / Printing to Console
Squiggle is "silent" by default, meaning if you run the above code, especially in Unity, it can seem like its not working. To get baseline "visibility", you can pass options to the Runner:

```cs
using Squiggle;

string myScript = "Speaker: Hello! The Speaker is talking!";
Squiggle.Core.Run( squiggleText : myScript,
dialogHandler : (command) => {
// This will automatically complete the dialog
Squiggle.Events.Commands.CompleteDialog?.Invoke(command);
runnerOptions : new Squiggle.Runner.Options(){
Debug = true
});
```

The above code will still immediately "complete" the dialog, but you can see how you could instead call out to other functions to process the dialog, then call `Squiggle.Events.Commands.CompleteDialog?.Invoke(command)` elsewhere when finished (like when a player presses the "advance" button in a dialog UI).
The above code will now print debug information that looks like this to the console (NOTE: Unity users need to do a second step, see below):

You can see more robust examples like this in the Unity package sample scenes.
```
Squiggle Runner J+NeBhab4UGr6GBTW9BBBQ==: Beginning Runner Execution With 1 Commands
Squiggle Runner J+NeBhab4UGr6GBTW9BBBQ==: Starting Execution for Command Type Dialog With Args: (0)Speaker (1)Hello! The Speaker is talking!
Squiggle Runner J+NeBhab4UGr6GBTW9BBBQ==: Completed Execution for Command Type Dialog With Args: (0)Speaker (1)Hello! The Speaker is talking!
Squiggle Runner J+NeBhab4UGr6GBTW9BBBQ==: Execution Complete, Exiting
```

## Runner Options
You may want to debug your Squiggle execution when you are first getting setup. Squiggle provides an easy route to do this by specifying `runnerOptions` when you execute Squiggle and setting the `Debug` value to `true`:
**If you're using Unity, you'll also need to specify a logging callback function in the options**:

```cs
using Squiggle;

string myScript = "Speaker: Hello! The Speaker is talking!";
Squiggle.Core.Run( squiggleText : myScript,
runnerOptions : new Squiggle.Runner.Options(){Debug = true});
runnerOptions : new Squiggle.Runner.Options(){
Debug = true,
LogHandler = (text) => Debug.Log(text)
});
```

This will print something similar to this in the console:
```
Squiggle Runner J+NeBhab4UGr6GBTW9BBBQ==: Beginning Runner Execution With 1 Commands
Squiggle Runner J+NeBhab4UGr6GBTW9BBBQ==: Starting Execution for Command Type Dialog With Args: (0)Speaker (1)Hello! The Speaker is talking!
Squiggle Runner J+NeBhab4UGr6GBTW9BBBQ==: Completed Execution for Command Type Dialog With Args: (0)Speaker (1)Hello! The Speaker is talking!
Squiggle Runner J+NeBhab4UGr6GBTW9BBBQ==: Execution Complete, Exiting
If you use the above code, the code will run and then immediately exit. This may not be desireable, as you'll likely want to "catch" the dialog, and handle it somewhere else in your code. You can specify a dialog callback with the `DialogHandler` parameter to handle dialog when running Squiggle code to achieve this:

```cs
using Squiggle;

string myScript = "Speaker: Hello! The Speaker is talking!";
Squiggle.Core.Run( squiggleText : myScript,
runnerOptions : new Squiggle.Runner.Options(){
Debug = true,
LogHandler = (text) => {Debug.Log(text)},
DialogHandler : (command) => {
// This will automatically complete the dialog
Squiggle.Events.Commands.CompleteDialog?.Invoke(command);
}
});
```

The above code will still immediately "complete" the dialog, but you can see how you could instead call out to other functions to process the dialog, then call `Squiggle.Events.Commands.CompleteDialog?.Invoke(command)` elsewhere when finished (like when a player presses the "advance" button in a dialog UI).

You can see more robust examples like this in the Unity package sample scenes.

## Multiple Commands
Squiggle code can contain as many lines as you like, with a single command being on each line (read more here about what constitutes valid Squiggle source).

Expand All @@ -107,11 +127,7 @@ Speaker 2: Now Speaker 2 is talking.
Speaker 3: And here's a word from Speaker 3.
";

Squiggle.Core.Run( squiggleText : testScript,
dialogHandler : (command) => {
// This will automatically complete the dialog
Squiggle.Events.Commands.CompleteDialog?.Invoke(command);
});
Squiggle.Core.Run(testScript);
```

## Non-Dialog Commands
Expand Down

0 comments on commit f9390a7

Please sign in to comment.