ludic/examples/events/scoped.ludic
Orkuncakilkaya 19fcf60599 wip(0.S3): packages and examples migrated again from their pre-0.S sources in one run
ludic migrate state packages <every example program> packages/ludic.lab/example/plate.ludic
  1804 vars into 126 states, 64 into lets; 23498 edits in 460 files

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 15:31:34 +03:00

23 lines
942 B
Text

# scoped.ludic — a despawned entity drops out of subsequent event-driven work
# (replaces tests/mod_c/scoped_mod.c, which tested entity-scoped foreign callbacks
# swept on despawn). Here a @On(Tick) listener counts live Units each tick; after
# the Unit is despawned, a further tick finds none. Fires twice before the kill,
# not the third time. Prints 2.
program Scoped {
property Health { hp: int = 0 }
model Unit { Health }
state ScopedState {
fires: int = 0
}
event Tick { }
@On(Tick) handler OnTick(scoped_st: mut ScopedState) { for (Health) in query [Health, {Unit}] { scoped_st.fires += 1 } }
entry (scoped_st: ScopedState) {
spawn Unit { Health { hp: 1 } }
emit Tick() # 1 live Unit -> fires = 1
emit Tick() # fires = 2
for (Health) in query [Health, {Unit}] { despawn self() } # kill the Unit
emit Tick() # no live Units -> fires stays 2
print(scoped_st.fires) # 2
}
}