Stream: beginners

Topic: Basic wasm4 problem


view this post on Zulip Alex Nuttall (Feb 17 2024 at 18:52):

I'm trying to do game of life in roc-wasm4 and I don't undestand how to get started in drawing something using List.walk.

update : Model -> Task Model []
update = \prev ->
    model = { prev & frameCount: prev.frameCount + 1 }

    {} <- W4.text (model.frameCount |> Num.toStr) { x: 32, y: 12 } |> Task.await

    {} <- (
            List.walkWithIndex model.grid (Task.ok {}) \task, row, y ->
                # {} <- task |> Task.await

                W4.rect {
                    height: 4,
                    width: 4,
                    x: 7 * 4,
                    y: (y |> Num.toI32) * 4,
                }
        )
        |> Task.await

    Task.ok model

the result:
Screenshot-2024-02-17-at-18.35.38.png

If I uncommented {} <- task |> Task.await I would expect to get a vertical line, but instead the command line just hangs.

Can anyone tell me what I'm doing wrong?

view this post on Zulip Anton (Feb 17 2024 at 19:07):

Hi @Alex Nuttall :)
Can you share the full code?

view this post on Zulip Alex Nuttall (Feb 17 2024 at 19:10):

Hi Anton

app "game_of_life"
    packages {
        w4: "../platform/main.roc",
    }
    imports [
        w4.Task.{ Task },
        w4.W4,
    ]
    provides [main, Model] to w4

Program : {
    init : Task Model [],
    update : Model -> Task Model [],
}

Grid : List (List Bool)

Model : {
    frameCount : U64,
    grid : Grid,
}

gridSize = 40

white = Color1
blue = Color4

setColorPalette : Task {} []
setColorPalette =
    W4.setPalette {
        color1: 0xfbf7f3,
        color2: 0xe5b083,
        color3: 0x426e5d,
        color4: 0x20283d,
    }

main : Program
main = { init, update }

init : Task Model []
init =
    {} <- setColorPalette |> Task.await

    Task.ok {
        frameCount: 0,
        grid: List.repeat Bool.false gridSize
        |> List.repeat gridSize
        |> List.set
            20
            (
                List.join [
                    List.repeat Bool.false 20,
                    [Bool.true],
                    List.repeat Bool.false 19,
                ]
            ),
    }

update : Model -> Task Model []
update = \prev ->
    model = { prev & frameCount: prev.frameCount + 1 }

    {} <- W4.text (model.frameCount |> Num.toStr) { x: 32, y: 12 } |> Task.await

    {} <- (
            List.walkWithIndex model.grid (Task.ok {}) \task, row, y ->
                # {} <- task |> Task.await

                W4.rect {
                    height: 4,
                    width: 4,
                    x: 7 * 4,
                    y: (y |> Num.toI32) * 4,
                }
        )
        |> Task.await

    Task.ok model

and I'm running zig build -Dapp=examples/game-of-life.roc run

view this post on Zulip Brendan Hansknecht (Feb 17 2024 at 19:10):

That looks correct at first glance. Might be hitting a roc bug. Yeah, full code as anton said, would be really helpful.

Worst case, to work around the bug, you probably need to pull that into a sub function and use Task.loop

view this post on Zulip Alex Nuttall (Feb 17 2024 at 19:37):

Just extracting the List.walk part into a function resolves it, thanks!

drawGrid = \grid ->
    List.walkWithIndex grid (Task.ok {}) \task, row, y ->
        {} <- task |> Task.await

        W4.rect {
            height: 4,
            width: 4,
            x: 7 * 4,
            y: (y |> Num.toI32) * 4,
        }

update : Model -> Task Model []
update = \prev ->
    model = { prev & frameCount: prev.frameCount + 1 }

    {} <- W4.text (model.frameCount |> Num.toStr) { x: 32, y: 12 } |> Task.await

    {} <- drawGrid model.grid |> Task.await

    Task.ok model

Screenshot-2024-02-17-at-19.36.55.png


Last updated: Jul 05 2025 at 12:14 UTC