From 0b42902ecd169d9c2d5bf4347c939bdb6da78605 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 25 Sep 2026 23:02:09 -0300 Subject: 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. --- src/host/script.zig | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'src/host/script.zig') 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); -- cgit v1.3