diff options
| author | Gabriel Schneider <[email protected]> | 2026-09-25 23:02:09 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-09-25 23:02:09 -0300 |
| commit | 0b42902ecd169d9c2d5bf4347c939bdb6da78605 (patch) | |
| tree | 3be52297e520e82c89d80fb3a239d7a038675c1f | |
| parent | 5523d23a46821d1fc65d670edd636c040d280a32 (diff) | |
| download | sketchbook-0b42902ecd169d9c2d5bf4347c939bdb6da78605.tar.gz sketchbook-0b42902ecd169d9c2d5bf4347c939bdb6da78605.zip | |
sketchbook: await in scripts
await FIELD VALUE plays frames until a state field holds the value, for up
to a minute: for waits whose length is the sketch's business, such as a line
of dialogue typing out.
| -rw-r--r-- | README.md | 1 | ||||
| -rw-r--r-- | src/host/script.zig | 11 |
2 files changed, 11 insertions, 1 deletions
@@ -97,6 +97,7 @@ queued and played. sketch. - **Scripts** (`--headless --fresh --script file`) drive the same routing and frames as a player: `click`, `key`, `type`, `console`, `expect field value`, + `await field value` (frames until it holds, a minute at most), `expect-audio`, `screenshot`, `load`, `reload`, `await-reload`. A failed `expect`, or a console line that errors (a sketch command returning an error included), fails the script. Headless always means offscreen, even diff --git a/src/host/script.zig b/src/host/script.zig index ec716e9..38954f3 100644 --- a/src/host/script.zig +++ b/src/host/script.zig @@ -11,6 +11,7 @@ //! console LINE run a console command //! frames N advance N frames without input //! expect FIELD VALUE compare a state field (enums by name or number) +//! await FIELD VALUE run frames until it holds (a minute at most) //! expect-audio KEY stream KEY has received samples //! screenshot FILE save the next frame as PNG //! load LIBRARY swap in another build of the sketch @@ -24,6 +25,8 @@ const Host = @import("Host.zig"); const fields = @import("fields.zig"); pub const frame_ns: u64 = std.time.ns_per_s / 60; +/// The most frames `await` plays: a minute. +const await_limit = 60 * 60; pub fn run(host: *Host, source: []const u8, defines: []const [2][]const u8) !void { var arena: std.heap.ArenaAllocator = .init(host.gpa); @@ -82,10 +85,16 @@ fn step(host: *Host, arena: std.mem.Allocator, command: []const u8, rest: []cons const count = try std.fmt.parseInt(usize, rest, 10); for (0..count) |_| try advance(host, now); return; - } else if (eq(u8, command, "expect")) { + } else if (eq(u8, command, "expect") or eq(u8, command, "await")) { const path = words.next() orelse return error.ExpectedField; const want = std.mem.trim(u8, words.rest(), " \t"); const leaf = host.state.layout.find(path) orelse return error.UnknownField; + // `await` plays frames until the field holds the value, for up to a + // minute: for waits whose length is the sketch's business. + if (eq(u8, command, "await")) { + var waited: usize = 0; + while (!fields.matches(leaf, host.live(), want) and waited < await_limit) : (waited += 1) try advance(host, now); + } if (!fields.matches(leaf, host.live(), want)) { var buf: [128]u8 = undefined; var w: std.Io.Writer = .fixed(&buf); |
