Skip to content

world.act(name, args?) ​

On behalf of the person, asks the rules of the world to run one of the actions they offer at this moment.

act(name: string, args?: Record<string, unknown>): Promise<'requested' | 'refused' | 'not-allowed'>
js
const result = await world.act('vote');

Parameters ​

ParameterWhat it is
nameThe name of an action in now.actions.
argsOptional arguments for an action that takes them. Each action in now.actions lists its arguments in its own args.

Returns ​

A Promise that resolves to one of these results:

ResultWhat it means
'requested'The rules accepted the request. Its effect arrives later in the world as a new now, a message or new data.
'refused'The rules turned the request down (the action takes no requests now) or did not answer within 10 seconds.
'not-allowed'Nothing was asked because the action is not among the current now.actions.

Usage ​

Offer the actions as buttons ​

now.actions holds the actions currently offered by the rules. Render a button for each action whose canChoose is not false and call act when the button is clicked:

js
world.subscribe((node) => {
  const actions = (node.now?.actions ?? []).filter((action) => action.canChoose !== false);
  buttons.replaceChildren(...actions.map((action) => {
    const button = document.createElement('button');
    button.textContent = action.text ?? action.name;
    button.disabled = action.isRequested;
    button.onclick = () => world.act(action.name);
    return button;
  }));
});

Try it with a fake world ​

A fake world with now.actions and an onAction callback answers every request:

js
const world = await connectToHost({
  fake: {
    world: {
      name: 'Council',
      now: { text: 'Voting is open', actions: [{ name: 'vote', text: 'Vote now' }] },
      onAction: (name) => `You chose ${name}`,
    },
  },
});

await world.act('vote'); // 'requested'; then the message "You chose vote" arrives

Caveats ​

  • 'requested' is not "done". The rules decide what happens and when. Render the effect only when it reaches your subscribe listener.
  • It can take up to 10 seconds to resolve because it waits for the answer of the rules.
  • Offer only what the person may choose. An action with canChoose: false happens on its own and one with isRequested: true has already been asked for by somebody.
  • It acts on behalf of the person. The rules treat every request as a choice made by the person. Call act only when the person chooses the action.
  • A fake world never refuses. act resolves 'requested' for every action in its now.actions and its now never changes from what you wrote. Keep the state that changes in data (Fake worlds).

Troubleshooting ​

CodeWhyWhat to do
'failed'The webapp could not run the call (rare).Show an error and let the person try again.

'refused' and 'not-allowed' are not errors. The Promise resolves with them as results. Either one usually means that the world changed after your view rendered its buttons. Render them again from the newest now.actions.

See also ​

The UNaIVERSE GUI kit, released under the Apache License 2.0. Copyright 2024-2026 UNaIVERSE S.r.l.