2

I'm working on a scripting interface for a game server I'm developing. I'm mimicing a NPC chat using MessageBoxes. Here's my script:

var a = test("hey");

if (a == true) {
    send("yes");
} else {
    send("no");
}

Here's my code:

    private TaskCompletionSource<bool> result = new TaskCompletionSource<bool>();

    private void Form1_Load(object sender, EventArgs e)
    {
        var contents = File.ReadAllText(@"C:\test.js");
        var engine = new Engine();

        engine.SetValue("test", new Func<string, Task<bool>>(Test));
        engine.SetValue("send", new Action<string>(Send));

       //new Thread(new ThreadStart(() => engine.Execute(contents))).Start();
        engine.Execute(contents);
    }

    private async Task<bool> Test(string text)
    {
       await Task.Factory.StartNew(() =>
        {
            var a = MessageBox.Show(text, "", MessageBoxButtons.YesNo);

            if (a == DialogResult.Yes)
            {
                result.TrySetResult(true);
            } else
            {
                result.TrySetResult(false);
            }
        });

        return result.Task.Result;
    }

    private void Send(string text)
    {
        MessageBox.Show(text);
    }

Now, when I execute the program it does show the MessageBox, however after it sets the result nothing happens, the script doesn't continue. Does anyone know why? It should display the "yes" or "no" MessageBox but it doesn't.

Perhaps I'm doing something wrong or is there a better way to "halt" the script until input is given?

EDIT: Ok, I got it - I have to call await Test so it will wait. However, I can't call the await keyword from a Javascript file. How do I get around this?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.