This project has moved and is read-only. For the latest updates, please go here.

Waiting for input

Jun 24, 2016 at 9:52 AM
Hey everyone.

I'm developing a game server for a game client and I needed a scripting interface to use for NPCs, so I tried using ClearScript, however I came into an issue.

First, here's an example of a NPC script:
character.SendNext("Welcome, press next to proceed.");
character.SendNext("Good! Now, press next again.");
bool yes = character.SendYesNo("Do you like it?");

if (yes) {
} else {
When you click on a NPC in the game, the client sends a packet with the NPC id, and then the server knows which script to execute. Once it executes the script, I want the script to be able to wait for the user input. So, after it executes "character.SendNext", I want to script to halt and wait until an input has been given.

Once the user clicks the next button, the client sends another packet. Once that packet is received, I want to resume the script depending on the packet's value.

How can that be achieved?
Jun 24, 2016 at 6:55 PM
Hi Fraysa,

Like any other code, script code can invoke modal or blocking APIs. Your SendYesNo function could be made to await user input before returning to its (script-based) caller; in fact, its signature implies that it does just that, as it returns the user's selection. You could also expose a function that just waits for input. It all depends on your application's architecture.

Script code can also use asynchronous programming techniques to avoid tying up threads while waiting for input. This might be a good idea on the server if you expect to have many scripts in flight concurrently, but heavy use of callbacks will make your script code less straightforward.

Unfortunately there's no way to suspend script execution externally and save its state for subsequent resumption. The operating system can do that at the thread level, but here again script code is no different from other code.