ludic/examples/events/mod_events.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

18 lines
806 B
Text

# mod_events.ludic — the event bus with two listeners on one event, driven from
# Ludic (replaces the former tests/mod_c/mod.c foreign-mod host). A public event
# `Damage` has two @On listeners: one prints the amount, one accumulates a total.
# Emitting it twice runs both listeners each time. Prints 10 / 32 / 42.
program ModEvents {
state ModEventsState {
total: int = 0
}
event Damage { amount: int = 0 }
@On(Damage) handler Native { print(amount) } # prints each hit
@On(Damage) handler Accum(mod_events_st: mut ModEventsState) { mod_events_st.total += amount } # sums them
entry (mod_events_st: ModEventsState) {
emit Damage(amount: 10) # Native prints 10
emit Damage(amount: 32) # Native prints 32
print(mod_events_st.total) # 42
}
}