Skip to content

zz.msg.Tick.delta contains the time since the last frame, not the last tick #116

Description

@davo9819

Setup:

const Model = struct {
    t: i64 = 0,

    pub const Msg = union(enum) {
        tick: zz.msg.Tick,
    };

    pub fn init(self: *Model, _: *zz.Context) zz.Cmd(Msg) {
        return .everyMs(100);
    }

    pub fn update(self: *Model, msg: Msg, ctx: *zz.Context) zz.Cmd(Msg) {
        switch (msg) {
            .tick => |t| {
                if (self.t == 0) {
                    self.t = t.timestamp;
                    return .none;
                }

                const delta = t.timestamp - self.t;
                self.t = t.timestamp;
                ctx.log(
                    "got tick t={d} d={d} delta={d}.",
                    .{ t.timestamp, t.delta, delta },
                );
            },
        }
        return .none;
    }

    pub fn view...
};

pub fn main(init: std.process.Init) !void {
    var program = zz.Program(Model).initWithOptions(
        init.gpa,
        init.io,
        init.environ_map,
        .{ .log_file = "debug.log" },
    );
    defer program.deinit();
    try program.run();
}

Output:

$ cat debug.log
...
[12:56:59] got tick t=2050144898 d=16625916 delta=116578957.
[12:56:59] got tick t=2166747197 d=16667237 delta=116602299.
[12:56:59] got tick t=2266760613 d=16599477 delta=100013416.
[12:56:59] got tick t=2366813976 d=16561227 delta=100053363.
[12:56:59] got tick t=2483457637 d=16652152 delta=116643661.
[12:56:59] got tick t=2600137341 d=16723427 delta=116679704.
[12:56:59] got tick t=2700144376 d=16651503 delta=100007035.
[12:56:59] got tick t=2816815283 d=16690178 delta=116670907.
[12:56:59] got tick t=2933465864 d=16721195 delta=116650581.
[12:57:00] got tick t=3033517868 d=16703615 delta=100052004.
[12:57:00] got tick t=3150079062 d=16669207 delta=116561194.
[12:57:00] got tick t=3250125305 d=16660515 delta=100046243.
[12:57:00] got tick t=3366771559 d=16691863 delta=116646254.
[12:57:00] got tick t=3483505355 d=16762681 delta=116733796.
...

Issue

The delta value in the tick message indicates "nanoseconds since the last tick", which is misleading to me because it actually means the last event loop tick, not the last tick event.

/// Tick message for timer-based updates
pub const Tick = struct {
    /// Monotonic timestamp in nanoseconds since program start.
    timestamp: i64,
    delta: u64, // Nanoseconds since last tick
};

Proposal

For my use case, I prefer the delta field to contain the time since the last tick event because the frame delta can also be obtained from the context, and I can avoid tracking the time in the model when the event loop already does this for me.

Anyways, at least changing the comment to reflect the actual value stored in Tick.delta would be useful. The comment can also be made into a doc string.

I'm happy to take care of this my self :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions