world.get()
Returns the newest world at the moment of the call.
get(): GuiNode
js
const now = world.get();Parameters
None.
Returns
The newest world. As long as nothing changes, every call of get() returns the same object.
Usage
Read the world in a click handler
The world object returned by connectToHost keeps the values its fields had at connection time. A handler that runs later reads the newest world with get():
js
const world = await connectToHost();
moveButton.onclick = async () => {
const now = world.get();
if (!now.canSend) return; // not the person's turn any more
await world.send('left');
};Know whether something changed
Because every change produces a new object, comparing two worlds by identity tells whether anything changed:
js
let drawn = null;
function drawIfChanged() {
const now = world.get();
if (now === drawn) return;
drawn = now;
draw(now);
}Caveats
- The world is frozen. Never write into it and keep your own state in your own variables.
- The methods work from any world.
world.send(...),world.act(...)and the other methods act on the current world even when you call them on the first world instead of the newest. Only the fields (name,data,messages, ...) keep the values they had when the object was created. - Do not poll it because
subscribealready calls your listener whenever the world changes.
Troubleshooting
get() never fails and has no error code of its own.
| Problem | Why | What to do |
|---|---|---|
world.name (or data, messages, ...) never changes. | world is the first world and its fields keep their initial values. | Read world.get().name or render in subscribe. |
TypeError: Cannot assign to read only property. | You wrote into the world. | Keep your own values in your own variables. |
See also
world.subscribe(listener): a listener called at every change.- The world: every field of the world.