Live reload
Every earlier tutorial’s source banner had two run modes:
daslang.exe <script>— standalone; runsmain()which loopsinit/update/shutdownuntilexit_requested().daslang-live <script>— same script, hosted inside a wrapper process that watches the file, reruns the typer on save, and swaps the new program in without restarting the GLFW window. ImGui context, registered widget state, dock layout, slider values — anything@liveor restored via a hook — carry across the reload.
This tutorial walks through the seams the live-reload framework exposes:
live_create_window/live_imgui_init— idempotent so they no-op on reload (the preserved ImGui context is reused).live_begin_frame/live_end_frame— per-frame gate / commit.live_begin_framereturnsfalsewhile the host is paused or in the middle of swapping programs; skip the frame.@liveannotation — preserves a global (or struct field) across reload via auto-generated[before_reload]/[after_reload]serializers in thelive/live_varsmodule.[live_command]— registers an HTTP endpoint that the running process exposes; called fromcurlor any other client. Theimgui_set/imgui_click/imgui_snapshotsurface is built from[live_command]declarations.[before_reload]/[after_reload]— manual save/restore hooks for state@livecan’t track (raw pointers, GL textures, C-owned resources).
Source: examples/tutorial/live_reload.das.
Walkthrough
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
19require daslib/json public
20require daslib/json_boost public
21
22// =============================================================================
23// TUTORIAL: live_reload — the daslang-live workflow that every earlier
24// tutorial implicitly relied on.
25//
26// Every tutorial's STANDALONE / LIVE blocks pointed at the same shape:
27//
28// STANDALONE: daslang.exe <script>
29// LIVE: daslang-live <script>
30//
31// In the live mode, the script is hosted inside daslang-live: a wrapper
32// process that runs your `init` / `update` / `shutdown` exports, watches
33// the source file for edits, and exposes an HTTP server for `imgui_set` /
34// `imgui_click` / `imgui_snapshot` and any user-defined `[live_command]`.
35// Save the file and daslang-live re-runs the typer/codegen, calls
36// `[before_reload]` hooks to stash anything that needs surviving, swaps in
37// the new program, then calls `[after_reload]` to restore. The GLFW
38// window stays open; the ImGui context, registered widget state, dock
39// layout, slider values — anything @live or restored via a hook — all
40// carry across the gap.
41//
42// This tutorial demonstrates each piece:
43//
44// 1. `live_create_window` / `live_imgui_init` — idempotent, reload-safe
45// 2. `live_begin_frame()` — per-frame gate; returns false while paused
46// or reloading. Skip the frame if it does.
47// 3. `@live` on a state struct — value preserved across reload
48// 4. `[live_command]` — custom HTTP endpoint that the running process
49// exposes; driven from curl just like imgui_set
50// 5. `[before_reload]` / `[after_reload]` — explicit save/restore hooks
51// for state the framework
52// doesn't track automatically
53//
54// STANDALONE: daslang.exe modules/dasImgui/examples/tutorial/live_reload.das
55// LIVE: daslang-live modules/dasImgui/examples/tutorial/live_reload.das
56//
57// DRIVE (when running live):
58// curl -X POST -d '{"name":"imgui_snapshot"}' localhost:9090/command
59// curl -X POST -d '{"name":"bump_counter"}' localhost:9090/command
60// curl -X POST -d '{"name":"reset_counter"}' localhost:9090/command
61// curl -X POST localhost:9090/reload
62// =============================================================================
63
64// ---- Custom counter that survives reload via @live ----
65// Plain module-scope var, marked @live so daslang-live's serializer hauls
66// it across the reload boundary. Non-`@live` globals reinitialise on
67// reload — see the demo_string field below for that contrast.
68var private @live g_custom_counter : int = 0
69// NOT @live — gets reset on each reload (intentional for the demo).
70var private g_session_string : string = "fresh session"
71
72// ---- Custom live_command — externally callable HTTP endpoint ----
73// imgui_set / imgui_click / imgui_snapshot are all `[live_command]`
74// internally. Users can add their own:
75struct BumpArgs {
76 @optional by : int = 1
77}
78
79[live_command(description = "Bump the custom counter by N (default 1).")]
80def bump_counter(input : JsonValue?) : JsonValue? {
81 let args = from_JV(input, type<BumpArgs>)
82 g_custom_counter += args.by
83 return JV((ok = true, counter = g_custom_counter))
84}
85
86[live_command(description = "Reset the custom counter to 0.")]
87def reset_counter(input : JsonValue?) : JsonValue? {
88 g_custom_counter = 0
89 return JV((ok = true, counter = 0))
90}
91
92// ---- Reload hooks — fire on reload boundary ----
93// [before_reload] runs in the OLD program after the file edit is detected
94// but before the new program loads. Use it to stash non-`@live` data that
95// the new program will pick up in `[after_reload]`. Frequently it's
96// enough to mark the relevant var `@live` instead, but hooks are the
97// escape hatch when serialization isn't possible (raw pointers, file
98// handles, GL textures...).
99[before_reload]
100def private on_before_reload() {
101 print("[live_reload tutorial] before_reload fired. g_custom_counter = {g_custom_counter}\n")
102}
103
104[after_reload]
105def private on_after_reload() {
106 print("[live_reload tutorial] after_reload fired. g_custom_counter = {g_custom_counter} (preserved by @live)\n")
107}
108
109[export]
110def init() {
111 // Both functions are idempotent on reload — cold start creates the
112 // window + ImGui context, reload re-uses the preserved ImGui context
113 // and skips the duplicate CreateContext call (see live_imgui_init
114 // body in imgui_live.das).
115 live_create_window("dasImgui live_reload tutorial", 720, 540)
116 live_imgui_init(live_window)
117 var io & = unsafe(GetIO())
118 io.FontGlobalScale = 1.5
119
120 // Re-running init resets g_session_string each reload. (Initial-value
121 // assignments at module scope run once at program load, but `init` is
122 // called both on cold-start AND on reload.)
123 g_session_string = "fresh session"
124}
125
126[export]
127def update() {
128 // The frame-gate. Returns false while daslang-live is paused, in the
129 // middle of swapping programs, or in any other state where rendering
130 // would crash or produce garbage. Always early-out on false.
131 if (!live_begin_frame()) return
132
133 begin_frame()
134
135 ImGui_ImplOpenGL3_NewFrame()
136 ImGui_ImplGlfw_NewFrame()
137 apply_synth_io_override()
138 NewFrame()
139
140 SetNextWindowPos(ImVec2(30.0f, 30.0f), ImGuiCond.FirstUseEver)
141 SetNextWindowSize(ImVec2(640.0f, 460.0f), ImGuiCond.FirstUseEver)
142 window(LIVE_WIN, (text = "live_reload", closable = false,
143 flags = ImGuiWindowFlags.None)) {
144
145 // ---- A slider whose value survives reload (@live in SliderStateFloat) ----
146 text("VOLUME.value is @live - survives reload.")
147 slider_float(VOLUME, (text = "Volume"))
148
149 separator(LR_SEP_1)
150
151 // ---- The custom counter, driven from outside via [live_command] ----
152 text("g_custom_counter = {g_custom_counter}")
153 text(" - @live, preserved across reload")
154 text(" - mutated externally by `bump_counter` / `reset_counter`")
155
156 separator(LR_SEP_2)
157
158 // ---- Contrast: session string resets each reload ----
159 text("g_session_string = \"{g_session_string}\"")
160 text(" - NOT @live; init() rewrites it each reload")
161
162 separator(LR_SEP_3)
163
164 // ---- A button whose click count is preserved (ClickState is @live) ----
165 if (button(PING_BTN, (text = "Ping (click_count survives reload)"))) {}
166 text("PING_BTN.click_count = {PING_BTN.click_count}")
167 }
168
169 end_of_frame()
170 Render()
171 var w, h : int
172 live_get_framebuffer_size(w, h)
173 glViewport(0, 0, w, h)
174 glClearColor(0.10f, 0.10f, 0.12f, 1.0f)
175 glClear(GL_COLOR_BUFFER_BIT)
176 ImGui_ImplOpenGL3_RenderDrawData(GetDrawData())
177
178 // Always match live_begin_frame with live_end_frame to keep the
179 // frame pump alive on the daslang-live side.
180 live_end_frame()
181}
182
183[export]
184def shutdown() {
185 // Idempotent on reload - live_imgui_shutdown skips during reload so
186 // the ImGui context is preserved; only the cold process exit runs
187 // the full teardown.
188 live_imgui_shutdown()
189 live_destroy_window()
190}
191
192[export]
193def main() {
194 // Standalone entrypoint. daslang-live drives init/update/shutdown
195 // directly and ignores main(); plain `daslang.exe <file>` runs this
196 // loop.
197 init()
198 while (!exit_requested()) {
199 update()
200 }
201 shutdown()
202}
The reload boundary
A daslang-live reload runs in this order:
File watcher notices a source-tree edit (or an HTTP
POST /reloadrequest arrives).The HOST collects every
[before_reload]function and runs them in the OLD program. Thelive/live_varsmodule auto-generates one of these per@liveglobal; the user can register more.Typer + codegen run against the new source. If they fail, the reload aborts and the old program keeps running —
live_get_errorsurfaces the diagnostic.The new program is loaded.
[after_reload]hooks run, restoring the saved state (@livefirst, then user hooks).The next
update()call seeslive_begin_frame() == trueand normal rendering resumes.
The GLFW window and the OS-level ImGui context survive the swap — only the daslang program is replaced.
@live preservation
The simplest way to keep a value across reload is the @live
annotation on the global (or on individual struct fields). The
live/live_vars module synthesizes the matching save/restore hooks
at compile time:
var private @live g_custom_counter : int = 0
The serializer uses daslib/archive; primitives, arrays, tables,
strings, and any struct whose fields are themselves @live-friendly
all work out of the box. Each @live target gets its own storage
key, and the saved data carries a hash of the initialization
expression — change the initializer in source, and the stale value
is discarded automatically. (No “I changed the default to 10 and now
my old value of 0 is wrong” foot-gun.)
Boost widget state types (ClickState, SliderStateFloat,
ToggleState, WindowState, …) are already structured this
way — their value-carrying fields are @live, their pending-flags
fields are not. That’s why a slider’s value survives reload but
pending_value doesn’t.
The frame gate
live_begin_frame() is the only way the host signals “do not render
this frame”:
def update() {
if (!live_begin_frame()) return
// ... NewFrame, your draw calls, Render
live_end_frame()
}
States that return false:
The host is paused (
POST /pausefromdaslang-liveormcp__daslang__live_pause).A reload is in progress (between
[before_reload]and[after_reload]).The most recent typer pass failed and the program is “frozen” on the prior version — the next save that compiles will revive it.
Always early-out on false and always pair with live_end_frame()
on the success branch.
[live_command] — user-defined HTTP endpoints
The same [live_command] annotation that registers imgui_set /
imgui_click / imgui_snapshot works for user functions. The
function takes a JsonValue? (the request body’s args field)
and returns JsonValue? (echoed back to the caller):
struct BumpArgs {
@optional by : int = 1
}
[live_command(description = "Bump the custom counter by N (default 1).")]
def bump_counter(input : JsonValue?) : JsonValue? {
let args = from_JV(input, type<BumpArgs>)
g_custom_counter += args.by
return JV((ok = true, counter = g_custom_counter))
}
The endpoint name (bump_counter) is the function name; the HTTP
surface routes POST /command requests with {"name":"bump_counter"}
to this handler. The handler runs on the GLFW main thread between
frames, so it can safely touch daslang globals and ImGui state
without locks.
Manual reload hooks
When @live doesn’t fit — typically because the state is a pointer
to a C-owned resource that the new program won’t recognize — declare
a pair of hooks explicitly:
[before_reload]
def private on_before_reload() {
// Stash whatever ``@live`` can't serialize.
// live_store_bytes / live_store_string store under a string key.
}
[after_reload]
def private on_after_reload() {
// Re-read the stash and rebuild the in-memory state.
// live_load_bytes / live_load_string read by the same key.
}
imgui_live.das itself is the canonical example: it serializes the
live_imgui_ctx pointer as a uint64 in [before_reload] and
re-binds it with SetCurrentContext in [after_reload].
Init / shutdown idempotence
init runs on both cold-start AND reload. shutdown runs on
reload AND process exit. The framework helpers
(live_imgui_init / live_imgui_shutdown) are idempotent — they
detect the reload case and no-op accordingly — so the script’s
init / shutdown exports can be written once without
distinguishing cold-start from reload.
User globals initialized at module scope (var x = 0) only run their
initializer once at program load — reload starts a NEW program, so
that initializer runs again. Use @live (or a [before_reload]
hook) for anything you want to preserve. Anything reset INSIDE
init rebuilds each reload — the demo’s g_session_string shows
that pattern.
Standalone vs live
Run standalone with daslang.exe — every part of this tutorial
still works EXCEPT the [live_command] HTTP endpoints, which need
the daslang-live host. The reload hooks are silent in standalone
mode (they only fire on the reload boundary, which never happens).
Driving from outside
Standard live-command shape; the user-defined endpoints sit next to the built-in ones:
# built-in: snapshot every registered widget
curl -X POST -d '{"name":"imgui_snapshot"}' localhost:9090/command
# user-defined: bump the custom counter by 3
curl -X POST -d '{"name":"bump_counter","args":{"by":3}}' localhost:9090/command
# user-defined: reset
curl -X POST -d '{"name":"reset_counter"}' localhost:9090/command
# framework: trigger a reload (file edit also triggers this)
curl -X POST localhost:9090/reload
The imgui_snapshot payload reflects whatever the most recent
bump_counter did — daslang globals and live-command results share
one in-memory model.
Next steps
Now that the live-command surface is explicit, next is the
driving-from-outside view: the JSON command set the boost layer
ships (imgui_set / imgui_click / imgui_open / …) treated
as its own programming model — a UI that responds to scripted
external events the same way it responds to mouse clicks.
See also
Full source: examples/tutorial/live_reload.das
Framework module: live/live_host (the host itself),
live/live_commands (the [live_command] annotation), and
live/live_vars (the @live serializer).
ImGui-specific lifecycle: imgui/imgui_live.das — the
[before_reload] / [after_reload] pair that preserves the
ImGui context pointer is the canonical example of a manual hook.
Previous tutorial: Containers
Boost macros — the macro layer.