examples: net_sync's raw buffer inside unsafe, now that a query's body is checked
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
253d8d3632
commit
4919c9c2fc
1 changed files with 16 additions and 14 deletions
|
|
@ -21,20 +21,22 @@ program NetSync {
|
||||||
|
|
||||||
entry {
|
entry {
|
||||||
spawn Player { Position { x: 3, y: 4 }, Health { hp: 50, max: 100 } }
|
spawn Player { Position { x: 3, y: 4 }, Health { hp: 50, max: 100 } }
|
||||||
for (Position, Health) in query [Position, Health, {Player}] {
|
unsafe { # a raw snapshot buffer on purpose: built with --unsafe (L7)
|
||||||
let e = self()
|
for (Position, Health) in query [Position, Health, {Player}] {
|
||||||
let buf = bytes(64)
|
let e = self()
|
||||||
print(sync_size(e)) # 12 = Position(x,y)=8 + Health.hp=4
|
let buf = bytes(64)
|
||||||
let n = serialize(e, buf) # snapshot the synced fields
|
print(sync_size(e)) # 12 = Position(x,y)=8 + Health.hp=4
|
||||||
Position.x = 99 # mutate everything
|
let n = serialize(e, buf) # snapshot the synced fields
|
||||||
Position.y = 88
|
Position.x = 99 # mutate everything
|
||||||
Health.hp = 7
|
Position.y = 88
|
||||||
Health.max = 999 # max is NOT synced
|
Health.hp = 7
|
||||||
apply(e, buf, n) # restore from the snapshot
|
Health.max = 999 # max is NOT synced
|
||||||
print(Position.x) # 3 — restored
|
apply(e, buf, n) # restore from the snapshot
|
||||||
print(Position.y) # 4 — restored
|
print(Position.x) # 3 — restored
|
||||||
print(Health.hp) # 50 — restored
|
print(Position.y) # 4 — restored
|
||||||
print(Health.max) # 999 — kept (unsynced)
|
print(Health.hp) # 50 — restored
|
||||||
|
print(Health.max) # 999 — kept (unsynced)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue