ludic/docs/language/ecs/fn-world_load.md

1.4 KiB

id name category kind tokens sig tip order
fn-world_load world_load ecs builtin world_load world_load(buf, len) Restore the whole world from a serialized byte buffer. 50

world_load restores the ECS world from a byte buffer previously produced by world_save, replacing the current world with the snapshot's instances and their fields. You pass the buffer and the number of valid bytes in it (the count world_save returned), and the world is rebuilt in place; it returns nothing. This is the low-level, buffer-based counterpart of world_save — the pair a mod or tool uses to move a whole world across the ABI or between storage. For a simple whole-game load from the default slot, the higher-level load() builtin is usually enough.

Parameters:

  • buf — the source byte buffer holding a snapshot
  • len — the number of valid bytes in buf
program SnapshotRoundTrip {
  property Position { column: int = 0, row: int = 0 }
  model Enemy { Position }

  state SnapshotRoundTripState {
    snapshot_length: int = 0
  }

  handler Boot phase Start {
    spawn Grunt { Position { column: 3, row: 3 } }
  }

  handler RoundTrip(snapshot_round_trip_st: mut SnapshotRoundTripState) phase Update {
    let buffer = bytes(4096)
    if Input.key() == 's' { snapshot_round_trip_st.snapshot_length = world_save(buffer) }
    if Input.key() == 'l' { world_load(buffer, snapshot_round_trip_st.snapshot_length) }
  }
}