How do real time events interact with turns?

I was wondering what happens exactly when a Glulx real time event fires during a turn (after the player has entered a command but before the next prompt is displayed).

Let’s say that the NPCs perform actions on their own every N seconds. (And let’s not discuss whether it’s a good idea. I’m only experimenting and it’s just an example.) All the messages are displayed in a side window so that nothing interferes with the prompt.

What happens when an NPC does something during a player’s action? Do the current action pauses, to let the NPC do their thing, then resumes?

If that’s the case, can this scenario happen:

  • The player tries to take the apple. The check rules are considered but they all pass without stopping the action.
  • Just after that, the NPC also tries to take the apple. The player’s action pauses, the NPC takes the apple, a message is displayed saying so, and the NPC now carries it.
  • But then the player’s action resumes and the carry out taking rules are applied. The player now carries the apple, effectively stealing it to the NPC.

This case is quite simplistic, but I think bigger bugs can arise in more complicated situations.

Maybe it’s better, to avoid any problem, to set a flag after reading a command (and unset it before reading a command) and forbid any real time event when the flag is set?

Timer events are only received by the game when it’s in the main event loop (a glk_select call) waiting for events.

In your scenario, the player’s action would run to completion, the game would reenter the event loop and call glk_select, it would receive a glk timer expiration event, and, in response, it would process the pending NPC actions before returning to the event loop to read more player input.

See the glk spec for details. It might also help to google event loops if you’re unfamiliar with them.

Right. Timer events are treated as “just another kind of input”.


So basically I don’t need to worry then. I’ll try to read the specs first next time.