Vorple and Glk timers

It seems the Vorple interpreter doesn’t support timers, but I’d like to use the Glulx Real Time extension by Erik Temple. Would there be an easy way to have a timer via JavaScript (or maybe to add support to Glk timers in Vorple)?

Ideally, it should behave as the Glk timer. The JavaScript timer should therefore not fire during a turn, only when the game is waiting for a command, as I would like it to change the game state.

Thanks!

You can do it quite easily with Javascript timers:

To execute parser command (cmd - text) in (s - real number) seconds:
	execute JavaScript command "setTimeout(function() {vorple.prompt.queueCommand('[escaped cmd]', true);}, [s times 1000])".
	
When play begins:
	execute parser command "x me" in 5 seconds.

This will put the command in the queue so that it’ll trigger only when the game is ready for a command. The command input is hidden so that when it triggers the player only sees the response and the command itself isn’t printed on the screen. You can have as many timers as you like running concurrently, they don’t interfere with each other.

For now you’ll also have to manually remove the command from the command history so that it doesn’t pop up when the player pushes the up key (Include Vorple Command Prompt Control and “remove the last command from the command history” when the timer triggers.) The behavior is changed in the next version of Vorple so that hidden commands are automatically removed from the history. You’ll then have to remember to remove that piece of the code if you update later.

Thank you!

The only drawback is that I have to create a custom action for the timed event. If the player discovers the secret command that triggers it, they’ll be able to fire it whenever they want. But I guess I can live with it.

There isn’t much that can be done to prevent “cheating” – even if the timer could trigger with some other mechanism, a determined player can always run the timer code manually. It shouldn’t be hard to come up with a command that’s practically impossible to stumble upon by accident. The loose convention has been to prepend custom commands with two underscores (__trigger timer1). At some point the prompt might start rejecting manually entered double-underscore commands which would be relatively easy to circumvent but it would prevent casual cheating and remove any possibility of accidentally typing the command.

I have another problem: when the timer ends and the special command fires, the command the player was typing during the timer is cleared.

Is there a way to restore the command that was being typed? I know it’s possible to prefill the command after the timer, but I don’t know if it’s possible to store somehow a command before it’s submitted.

That should also get fixed later, but here’s a workaround:

To execute parser command (cmd - text) in (s - real number) seconds:
	execute JavaScript command "setTimeout(function() {var userCmd=$('#lineinput-field').val(); vorple.prompt.queueCommand('[escaped cmd]', true); setTimeout(function() {$('#lineinput-field').val(userCmd);}, 200 );}, [s times 1000])".

I see you restore the command after a delay with

setTimeout(function() {$('#lineinput-field').val(userCmd);}, 200 )

What if you replace it with the code from the “prefill the player’s command” phrase:

vorple.prompt.setValue(userCmd);

With the latter, there is no delay. (I seems the only issue is the bug on Firefox where the cursor is placed at the beginning of the prefilled command.)

The delay is needed because the command queue takes a while to resolve. If you set the value without the timer it happens before the command is actually executed. 200 ms is too fast for a human to notice so the visual effect should be the same.