Page 1 of 1
Randomizing voice audio files
Posted: Tue Mar 25, 2025 7:40 am
by Vielfalt
Hi,
In my game, a character greets the player when he passes by, via bark dialogue, which works well. I'd like to randomly shuffle the voice audio files, to prevent the player from hearing the same intonation all the time. So I added this Sequence, but it always plays the first file:
Code: Select all
RandomizeNextEntry(true);
AudioWait(Voice/Receptionist - Good day sir 1);
AudioWait(Voice/Receptionist - Good day sir 2)
I probably misunderstood how RandomizeNextEntry works...?
Furthermore, I'd like there to be a minimum interval between this specific bark, so that the player isn't greeted more often than once per minute for example. What's the simplest way to implement this? Can it be done with a variable (lastBarkTime) and Lua script in Dialogue System, or do I need to write C# scripts?
Re: Randomizing voice audio files
Posted: Tue Mar 25, 2025 9:20 am
by Tony Li
Hi,
If you wanted to randomly choose between 2 voice files, you could use a sequencer command like this:
Code: Select all
AudioWait(Voice/Receptionist - Good day sir [lua(RandomElement("1|2"))])
It uses the RandomElement() Lua function to randomly choose between "1" and "2".
However, if you have the added restriction of not playing a voice file if less than a minute has passed, it's probably best to write a custom sequencer command. Please see part 6 of
Cutscene Sequence Tutorials.
Re: Randomizing voice audio files
Posted: Tue Mar 25, 2025 5:45 pm
by Vielfalt
Thanks a lot, RandomElement() works like a charm here!
Regarding the minimum time interval between barks, I managed to implement it with Lua in the Dialogue System Trigger:
- Lua condition:
Code: Select all
os.clock() > Variable["PrevGreetingTime"] + 60
- Actions / Run Lua code:
Code: Select all
Variable["PrevGreetingTime"] = os.clock()
Coming from C++ and Swift, Lua is new to me, but I'm starting to like it

Re: Randomizing voice audio files
Posted: Tue Mar 25, 2025 6:52 pm
by Tony Li
Glad to help! Nice solution for the bark timer.
Re: Randomizing voice audio files
Posted: Fri Mar 28, 2025 5:46 am
by Vielfalt
I just discovered that it doesn't work on iOS. I haven't debugged it yet, but could it be that os.clock() doesn't work on iOS / iPadOS? Is there an alternative Lua call for fetching any kind of system time?
The manual chapter
"Lua Under the Hood" says that
os: functions defined but disabled (to prevent malicious code insertion)
I wonder why it worked on macOS?
Re: Randomizing voice audio files
Posted: Fri Mar 28, 2025 8:07 am
by Tony Li
os.clock(), os.time(), and os.date() are the only OS library functions that aren't disabled since they're safe in the sandbox Lua environment. However, I do see a note that the .NET function it uses, Environment.TickCount, isn't accurate on iOS. I'll update this in 2.2.52 so it uses an iOS-accurate function, and I'll post a patch here.
Re: Randomizing voice audio files
Posted: Sat Mar 29, 2025 2:26 pm
by Tony Li
This patch should correct os.clock() for iOS:
DS_LuaClockPatch_2025-03-29.unitypackage
Re: Randomizing voice audio files
Posted: Mon Mar 31, 2025 3:03 am
by Vielfalt
Your fix works perfectly on iPad and iPhone, thanks a lot!

Re: Randomizing voice audio files
Posted: Mon Mar 31, 2025 7:49 am
by Tony Li
Glad to help!