diff --git a/examples/play-opus/App.zig b/examples/play-opus/App.zig index 7cb469e3..e2543e8d 100644 --- a/examples/play-opus/App.zig +++ b/examples/play-opus/App.zig @@ -20,6 +20,7 @@ pub const Mod = mach.Mod(@This()); pub const events = .{ .init = .{ .handler = init }, + .after_init = .{ .handler = afterInit }, .deinit = .{ .handler = deinit }, .tick = .{ .handler = tick }, .audio_state_change = .{ .handler = audioStateChange }, @@ -32,9 +33,8 @@ pub const components = .{ sfx: Opus, fn init(core: *mach.Core.Mod, audio: *mach.Audio.Mod, app: *Mod) !void { - // Initialize audio module, telling it to send our module's .audio_state_change event when an - // entity's sound stops playing - audio.send(.init, .{app.event(.audio_state_change)}); + audio.send(.init, .{}); + app.send(.after_init, .{}); const bgm_fbs = std.io.fixedBufferStream(assets.bgm.bit_bit_loop); const sfx_fbs = std.io.fixedBufferStream(assets.sfx.death); @@ -63,6 +63,12 @@ fn init(core: *mach.Core.Mod, audio: *mach.Audio.Mod, app: *Mod) !void { core.send(.start, .{}); } +fn afterInit(audio: *mach.Audio.Mod, app: *Mod) void { + // Configure the audio module to send our app's .audio_state_change event when an entity's sound + // finishes playing. + audio.state().on_state_change = app.event(.audio_state_change); +} + fn deinit(core: *mach.Core.Mod, audio: *mach.Audio.Mod) void { audio.send(.deinit, .{}); core.send(.deinit, .{});