summaryrefslogtreecommitdiff
path: root/src/host
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-09-25 23:02:09 -0300
committerGabriel Schneider <[email protected]>2026-09-25 23:02:09 -0300
commit0b42902ecd169d9c2d5bf4347c939bdb6da78605 (patch)
tree3be52297e520e82c89d80fb3a239d7a038675c1f /src/host
parent5523d23a46821d1fc65d670edd636c040d280a32 (diff)
downloadsketchbook-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.
Diffstat (limited to 'src/host')
-rw-r--r--src/host/script.zig11
1 files changed, 10 insertions, 1 deletions
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);