State & telemetry

Widget state lives in daslang, not in ImGui. Every boost widget macro emits a module-scope global named by the first argument — typed as ClickState / SliderStateFloat / ToggleState / etc. The global holds the widget’s value plus any pending overrides queued by external drivers. The same global is what the registry serializes for imgui_snapshot, so the daslang side, the test side, and the external-driver side all see the same value.

Three immediate wins fall out of that design:

  • Auto-emit — no var SAVE_BTN : ClickState declaration to keep in sync with the call site. The macro declares it on first compile.

  • Read anywhereSAVE_BTN.click_count, SPEED.value, etc. are plain daslang globals, readable from any module that requires this one.

  • Dotted flagsIDENT.PUBLIC / IDENT.PRIVATE / IDENT.NOTLIVE tune visibility and live-reload behavior on the emitted global without claiming new syntactic positions in the call.

Source: examples/tutorial/state_telemetry.das.

Walkthrough

state_telemetry recording
  1options gen2
  2
  3require imgui
  4require imgui_app
  5require glfw/glfw_boost
  6require opengl/opengl_boost
  7require live/glfw_live
  8require live/live_api
  9require live/live_commands
 10require live/live_vars
 11require live/opengl_live
 12require live_host
 13require imgui/imgui_live
 14require imgui/imgui_boost_runtime
 15require imgui/imgui_boost_v2
 16require imgui/imgui_widgets_builtin
 17require imgui/imgui_containers_builtin
 18require imgui/imgui_visual_aids
 19
 20// =============================================================================
 21// TUTORIAL: state_telemetry — how widget state lives in daslang, not ImGui.
 22//
 23// Each boost widget macro emits a module-scope global named by the first
 24// argument. The global is a typed state struct (ClickState, SliderStateInt,
 25// SliderStateFloat, ToggleState, ...) that holds the widget's value, plus
 26// any pending overrides queued by external drivers. Three immediate wins:
 27//
 28//   1. Auto-emit:        the variable is declared once, by the macro, on
 29//                        first compile. No "var SAVE_BTN : ClickState;"
 30//                        boilerplate to keep in sync with the call site.
 31//   2. Read anywhere:    SAVE_BTN.click_count, SPEED.value are plain
 32//                        globals — readable from any module that requires
 33//                        this one (assuming PUBLIC visibility).
 34//   3. Dotted flags:     IDENT.PUBLIC / IDENT.PRIVATE / IDENT.NOTLIVE
 35//                        modify visibility / live-reload behavior on the
 36//                        emitted global without claiming new syntactic
 37//                        positions in the call.
 38//
 39// Plus `imgui_snapshot` — the registry serializes every registered widget
 40// to JSON: kind, bbox, hex_id, payload (value / click_count / ...). That's
 41// the surface external drivers, integration tests, and visual aids see.
 42//
 43// STANDALONE: daslang.exe modules/dasImgui/examples/tutorial/state_telemetry.das
 44// LIVE:       daslang-live modules/dasImgui/examples/tutorial/state_telemetry.das
 45//
 46// DRIVE (when running live):
 47//   curl -X POST -d '{"name":"imgui_snapshot"}'                                                       localhost:9090/command
 48//   curl -X POST -d '{"name":"imgui_click","args":{"target":"STATE_WIN/SAVE_BTN"}}'                    localhost:9090/command
 49//   curl -X POST -d '{"name":"imgui_set","args":{"target":"STATE_WIN/SPEED","value":7}}'               localhost:9090/command
 50//   curl -X POST -d '{"name":"imgui_set","args":{"target":"STATE_WIN/STATUS_TEXT","value":"saved"}}'   localhost:9090/command
 51// =============================================================================
 52
 53[export]
 54def init() {
 55    live_create_window("dasImgui state_telemetry tutorial", 720, 540)
 56    live_imgui_init(live_window)
 57    var io & = unsafe(GetIO())
 58    io.FontGlobalScale = 1.5
 59}
 60
 61[export]
 62def update() {
 63    if (!live_begin_frame()) return
 64    begin_frame()
 65
 66    ImGui_ImplOpenGL3_NewFrame()
 67    ImGui_ImplGlfw_NewFrame()
 68    apply_synth_io_override()
 69    NewFrame()
 70
 71    SetNextWindowPos(ImVec2(30.0f, 30.0f), ImGuiCond.FirstUseEver)
 72    SetNextWindowSize(ImVec2(640.0f, 460.0f), ImGuiCond.FirstUseEver)
 73    window(STATE_WIN, (text = "state & telemetry", closable = false,
 74                       flags = ImGuiWindowFlags.None)) {
 75
 76        // ---- Auto-emit + read-anywhere ----
 77        // No top-of-file `var SAVE_BTN : ClickState`. The macro emits the
 78        // global the first time it sees `button(SAVE_BTN, ...)`. The
 79        // struct's fields are then read directly:
 80        //   SAVE_BTN.click_count : cumulative (@live → preserved across reload)
 81        //   SAVE_BTN.clicked     : true on the frame the button fired
 82        text("Auto-emit: SAVE_BTN is the macro-emitted global.")
 83        if (button(SAVE_BTN, (text = "Save"))) {
 84            // `button(...)` returns bool — clicked-this-frame. Same info
 85            // as SAVE_BTN.clicked, just inline.
 86        }
 87        text("SAVE_BTN.click_count = {SAVE_BTN.click_count}")
 88
 89        separator(ST_SEP_1)
 90
 91        // ---- Dotted flags ----
 92        // SPEED.PUBLIC: emit the global with `variable public` instead of
 93        // the default `variable private`. Other modules requiring this one
 94        // can then read SPEED.value. Telemetry path stays "SPEED" — flags
 95        // don't leak into the registry path.
 96        //
 97        // VOLUME.NOTLIVE: skip @live on the emitted global. On live-reload
 98        // the source-side initial value wins (helps when you change bounds
 99        // and want them to take effect immediately, not be preserved).
100        text("Dotted flags tune visibility and live-reload behavior:")
101        slider_int(SPEED.PUBLIC,   (text = "Speed (int, PUBLIC)"))
102        slider_float(VOLUME.NOTLIVE, (text = "Volume (NOTLIVE)"))
103        text("SPEED.value = {SPEED.value}   VOLUME.value = {VOLUME.value}")
104
105        separator(ST_SEP_2)
106
107        // ---- text_show: app-side value mirror ----
108        // text_show is the read-only mirror of text_input. The state's
109        // .value string is what gets displayed; imgui_set can drive it
110        // from outside, and the snapshot exposes it under the standard
111        // payload.value field — so integration tests can assert against
112        // computed status strings the same way they assert slider values.
113        text("text_show — app-driven status reaches the snapshot:")
114        text_show(STATUS_TEXT)
115
116        // The bump button writes a computed string into STATUS_TEXT.value.
117        // Both `:= "..."` (clone-string) and external imgui_set work; the
118        // snapshot reflects whichever ran most recently.
119        if (button(BUMP_STATUS, (text = "bump status"))) {
120            STATUS_TEXT.value := "saved at frame {get_uptime()}"
121        }
122    }
123
124    end_of_frame()
125    Render()
126    var w, h : int
127    live_get_framebuffer_size(w, h)
128    glViewport(0, 0, w, h)
129    glClearColor(0.10f, 0.10f, 0.12f, 1.0f)
130    glClear(GL_COLOR_BUFFER_BIT)
131    ImGui_ImplOpenGL3_RenderDrawData(GetDrawData())
132
133    live_end_frame()
134}
135
136[export]
137def shutdown() {
138    live_imgui_shutdown()
139    live_destroy_window()
140}
141
142[export]
143def main() {
144    init()
145    while (!exit_requested()) {
146        update()
147    }
148    shutdown()
149}

Auto-emit

The first time button(SAVE_BTN, ...) is compiled, the macro emits the matching global at module scope:

// emitted automatically — no manual declaration
@live variable private SAVE_BTN : ClickState = ClickState()

That’s why there’s no var SAVE_BTN at the top of the file. The state struct is owned by daslang — visible to grep, walkable via RTTI, persistable through the standard serializer, preserved across daslang-live reloads thanks to @live.

Reading state

Once emitted, the global behaves like any other daslang global — SAVE_BTN.click_count is a plain field access:

if (button(SAVE_BTN, (text = "Save"))) { ... }
Text("SAVE_BTN.click_count = {SAVE_BTN.click_count}")

Two distinct value channels are available:

  • button(...) returns booltrue on the frame the click fired. Inline-friendly for the “do thing now” case.

  • SAVE_BTN.clicked is the same flag, surfaced as a field. Useful when the click handler is far from the call site, or in another module that requires this one.

Cumulative counters (click_count for buttons, changed for sliders, etc.) live alongside on the state struct. Walk imgui_boost_runtime.das for the full field list per state struct.

Dotted flags

A dot suffix on the identifier flips flags on the emitted global. The telemetry path uses only the bare identifier (STATE_WIN/SPEED, never STATE_WIN/SPEED.PUBLIC) — flags never leak into the path or the ImGui hash.

  • SPEED.PUBLIC — emit as variable public instead of the default variable private. Sibling modules requiring this one can then read SPEED.value directly.

  • VOLUME.NOTLIVE — skip the @live annotation on the emitted global. Useful when you change the slider bounds and want the source-side initial value to take effect immediately on reload rather than be preserved.

  • IDENT.PRIVATE — explicit default (same as no suffix). Lists cleanly when you grep for visibility intent.

Multiple flags compose: RPS.PUBLIC.NOTLIVE emits a public, non-@live global. New flags can land on demand without affecting the call syntax.

text_show — the app-driven mirror

text_show is the read-only counterpart to text_inputstate.value is what the widget renders, and the value can be written by the app (STATUS_TEXT.value := "...") or by an external driver (imgui_set with a string value). Either way the snapshot exposes the current value under the standard payload.value field, so integration tests can assert on computed status strings the same way they assert slider values:

text_show(STATUS_TEXT)
if (button(BUMP_STATUS, (text = "bump status"))) {
    STATUS_TEXT.value := "saved at frame {get_uptime()}"
}

The := clones the new string into the current context’s heap — required because daslang-live’s HTTP handler runs in a different context than the GLFW main loop. Plain = would assign a pointer that becomes invalid the moment the request returns.

Standalone vs live

Same convention as previous tutorials.

Driving from outside

The snapshot exposes the state structs as JSON:

curl -X POST -d '{"name":"imgui_snapshot"}' localhost:9090/command

# Excerpt of the response:
#   "globals": {
#     "STATE_WIN/SAVE_BTN":    { "kind": "button", "payload": {"click_count": 2}, ... },
#     "STATE_WIN/SPEED":       { "kind": "slider_int", "payload": {"value": 7, ...}, ... },
#     "STATE_WIN/STATUS_TEXT": { "kind": "text_show", "payload": {"value": "saved at frame 12.3"}, ... }
#   }

Drivers go through the same registry — imgui_set looks up the target, queues the pending value on the matching state struct, and the renderer consumes it next frame:

curl -X POST -d '{"name":"imgui_set","args":{"target":"STATE_WIN/SPEED","value":7}}' \
     localhost:9090/command
curl -X POST -d '{"name":"imgui_set","args":{"target":"STATE_WIN/STATUS_TEXT","value":"hello"}}' \
     localhost:9090/command
curl -X POST -d '{"name":"imgui_click","args":{"target":"STATE_WIN/SAVE_BTN"}}' \
     localhost:9090/command

Next steps

So far every tutorial has used a single window(...) container. Containers come next — modal dialogs, popups, tab bars, child windows, and menus, all sharing the same block-arg pattern.

See also

Full source: examples/tutorial/state_telemetry.das

Richer reference: examples/features/foundation.das — the features-side demo that established the auto-emit + dotted-flag surface plus the unified L2/L3 dispatch.

Snapshot contract: see imgui_boost_runtime.das for the per-kind state_struct definitions (ClickState, SliderStateInt, SliderStateFloat, ToggleState, TextShowState, …).

Previous tutorial: With id

Boost macros — the macro layer.