Friday, February 3, 2012

Presenting choices to the user


The most recent thing I've done in relation to the project is to create a method for presenting choices to the user, letting them select one of the choices, then allowing that choice to be used in a script.

Now, the engine already had a dialogue box interface. Essentially the dialogue box becomes visible, all input for the rest of the game is ignored, and text is drawn on the box. You'd press a button to continue through the text, or to close the box once it had run out of text to display.
This would be called using a Lua script that looked something like this:
ShowMessage("your text goes here")

That would call a method that activated the dialogue box and gave it the text.

A problem came in when I wanted to have a method that would return the user's choice.
Previously ShowMessage would simply add the text to a queue, and then instantly continue on to the next line in the script. However, presenting options would require waiting for user input, and since most of the Lua script calls were from the same thread as the game that would just lock the entire game, as there's no way to tell if you get input while the thread is waiting.

The solution was a lot simpler than what my paranoid mind was telling me it would be.
I changed the Lua script calls to run in new threads, created by the TaskFactory. While that thread was executing no input would be accepted from the user for the screen that called it. This prevents more than one script running simultaneously (although I'm sure it's technically possible). This way I could safely set the newly created thread to wait while still allowing input through the main game thread. When input is selected by the user the waiting thread would be reawoken and continue from where it stopped.



I made a method that can be registered in the Lua runtime which follows the pattern
ShowOption("message", {"choice1", "choice2", ... "choice n"})

Making the pretty interface for selecting input was relatively simple.
All in all this particular problem was more simple than I'd expected.

No comments:

Post a Comment