Timed speech "Say ... for a duration" in Typescript

I would like to know what’s the equivalent of the Coblock function “say … for a duration/period of time” in Typescript.
It looks like speech is treated as property/attribute of an object, instead of a function. Currently I have not found a straightforward to let a character say something for a given period of time.
I have written a function like this using the Time.schedule method, it can achieve similar effect when used individually.

function timedSpeech (char, sentence: string, duration: number) {
    char.speech = sentence
    Time.schedule(() => {
        char.speech = null
        }, duration);
}

But when it is put inside a loop (because I want the character to say a list of lines), the problem emerged.

const speechlist = ["line A", "line B", "line C"]
let i = 0;
while (i < speechlist.length) {
    Debug.log("i=" + i)
    Debug.log("s=" + speechlist[i])
    timedSpeech(andy, speechlist[i], 5)
    i = i+1;
}

The loop will not wait for Time.schedule to be executed before proceeding to next iteration. What ends up happening is that the character would immediately jump to last line of the list.

Hope this is a clear question and that someone can share some insight.

1 Like

I find it easier sometimes to just get the thing working, then optimise.

This is what I got:

const speechlist = ["line A", "line B", "line C"]
let i = 0;

Time.scheduleRepeating(()=>{
    if (i < speechlist.length) {
        Debug.log("i=" + i)
        Debug.log("s=" + speechlist[i])
        andy.speech = speechlist[i];
        i++;
    }
},5)

In this case, I’m not sure adding a function will give much benefit, as you’d need to add it to a queue, which you’ve already got in the list.

Hope that helps!

Geoff @ TechLeap