Skip to content

Commit c038dd3

Browse files
committed
Set variables to initial values on start
1 parent 81784ca commit c038dd3

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

Program.cs

+15
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public class JSCompilation
139139
public Dictionary<string, string> StringTable { get; set; } = new Dictionary<string, string>();
140140
public List<Diagnostic> Diagnostics { get; set; } = new List<Diagnostic>();
141141
public byte[] ProgramData { get; set; } = Array.Empty<byte>();
142+
public Dictionary<string, object> InitialValues { get; set; } = new Dictionary<string, object>();
142143
}
143144

144145
/// <summary>
@@ -257,6 +258,20 @@ public Task<JSCompilation> SetProgramSource(string source)
257258
StringTable = result.StringTable?.ToDictionary(kv => kv.Key, kv => kv.Value.text ?? "") ?? new Dictionary<string, string>(),
258259
Diagnostics = result.Diagnostics.ToList(),
259260
ProgramData = result.Program?.ToByteArray() ?? Array.Empty<byte>(),
261+
InitialValues = result.Program?.InitialValues.ToDictionary(kv => kv.Key, kv =>
262+
{
263+
switch (kv.Value.ValueCase)
264+
{
265+
case Operand.ValueOneofCase.StringValue:
266+
return (object)kv.Value.StringValue;
267+
case Operand.ValueOneofCase.BoolValue:
268+
return (object)kv.Value.BoolValue;
269+
case Operand.ValueOneofCase.FloatValue:
270+
return (object)kv.Value.FloatValue;
271+
default:
272+
throw new Exception($"Variable {kv.Key} has a value of invalid type {kv.Value.ValueCase}");
273+
}
274+
}) ?? new()
260275
});
261276
}
262277

src/playground.ts

+6
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,12 @@ export async function load(script: string) {
346346
var source = editor.getModel().getValue();
347347
var compilation = await dialogue.compileSource(source);
348348

349+
for (const [varName, value] of Object.entries(
350+
compilation.initialValues,
351+
)) {
352+
variableStorage.setValue(varName, value);
353+
}
354+
349355
// Display any diagnostics we have
350356
for (let diagnostic of compilation.diagnostics) {
351357
let displayPosition = `Line ${diagnostic.range.start.line + 1}`;

src/yarnspinner.ts

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ interface CompileResult {
2121
stringTable: StringTable;
2222
diagnostics: Diagnostic[];
2323
programData?: Uint8Array;
24+
initialValues: Record<string, number | string | boolean>;
2425
}
2526

2627
export interface Line {
@@ -220,6 +221,7 @@ class Dialogue implements IDialogue {
220221
nodes: [],
221222
diagnostics: [],
222223
stringTable: {},
224+
initialValues: {},
223225
};
224226
this.onError(error);
225227
}

0 commit comments

Comments
 (0)