Make Lua.Run work after a Time.Delay

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
jksdts
Posts: 1
Joined: Wed Jan 24, 2024 2:32 am

Make Lua.Run work after a Time.Delay

Post by jksdts »

Hi all,

I'm currently working on implementing a quiz system in Unity using the Dialogue System. The quiz answers and questions are retrieved from a database which means I need to delay updating the conversation until after the data is retrieved.

The issue I'm running into is that after the delay, Lua.Run doesn't seem to function as intended. I've tried running Lua.Run with debug and and allowExceptions flagged as true, but this nothing displays in the console. It's as if running Lua.Run causes the entire Dialogue System to freeze and stop functioning. If anyone has any ideas as to how I might make the Lua function work, I would greatly appreciate it.

Thanks!
User avatar
Tony Li
Posts: 22112
Joined: Thu Jul 18, 2013 1:27 pm

Re: Make Lua.Run work after a Time.Delay

Post by Tony Li »

Hi,

Lua runs on the main thread. If you're blocking it to wait for a server response, the program will pause until it gets the response. I recommend using a UnityWebRequest to retrieve the quiz answers and questions. You can make the conversation wait on a dialogue entry by using the WaitForMessage() sequencer command or even a custom sequencer command. (See Sequences.) For example, your conversation might look like:

quiz.png
quiz.png (25.89 KiB) Viewed 349 times

It uses a custom sequencer command that might look something like:

Code: Select all

using System.Collections;
using UnityEngine;
using UnityEngine.Networking;

namespace PixelCrushers.DialogueSystem.SequencerCommands
{
    public class SequencerCommandDownloadQuiz : SequencerCommand
    {

        IEnumerator Start()
        {
            using (UnityWebRequest webRequest = UnityWebRequest.Get("YOUR URL"))
            {
                yield return webRequest.SendWebRequest();
                if (webRequest.result == UnityWebRequest.Result.Success)
                {
                    // Handle webRequest.downloadHandler.text
                }
                else
                {
                    Debug.LogError("Error downloading quiz.");
                }
                Stop();
            }
        }
    }
}
Post Reply