Like the title stated, I’d assuming I would have a bunch of .WAV files for the voices inside of a folder, let’s call it voices, and then I’d have a game object that would have a sound component, that would be the voice, and inside of the script that would also be in the game object that would call something like
sound.play("go#sound", {delay = 1, gain = 0.5, pan = -1.0, speed = 1.25})
in the documentation. But how would I randomly get the voices files
You would have one sound component for each voice line and give them IDs like voice1, voice2, etc. Then all you need is a bit of code to pick one of them:
local VOICE_LINES_AMOUNT = 5
-- This will be between 1 and 5, inclusive
local n = math.random(VOICE_LINES_AMOUNT)
local id = "go#voice" .. n
sound.play(id, {delay = 1, gain = 0.5, pan = -1.0, speed = 1.25})
You’ll also want to make sure to call math.randomseed() somewhere to initialize Lua’s RNG:
-- In some init() function
math.randomseed(socket.gettime() * 10000)
-- Some people recommend throwing out the first few random numbers
math.random()
math.random()
math.random()
If you also don’t mind explaining the logic, why would you need a sound component for each voice line instead of one sound component that references .wav files? I’m still unclear on that part. Thank you.
The sound component itself can only refer to one sound resource at a time.
You can use a script resource property to refer to multiple sound resources, then set the sound resource on the sound component before playing the sound.
Note: There is an issue with this approach (it may crash) that we’re fixing in the next beta release, which will be released on monday.
What would be the syntax for writing that? I’m reading Script properties and am looking at the examples for the documentation. I do see there being a function for resource called
This will not work. It has to be a single string without string concatenation or any pieces of Lua code. The string will be evaluated at build-time not run-time.
Thank you. Is it suppose to be appearing in the editor because it’s not for some reason. I tried giving them different names but they also don’t appear in the editor.
with #sliding-sound’s the name of my sound component. However I don’t know how that would work during runtime. For the project idea, I have a game object that’s intended to be spawned via factory, and it’ll eventually get destroyed and be respawned again, so I was hoping to randomise the sound and set it everytime it ‘respawns’. I should probably retitle the thread but I don’t know what to call it.
I think we’re overcomplicating things. Here is my recommendation:
Create a game object with id sounds and add it to your “main” collection. Add your sound components to this game object.
NOTE: This does not have to be the factory spawned game object. It is very useful to have your sound components in a central game object as it makes it easier to add things such as sound gating, randomizations of the same sound, easy muting of all sounds of a certain type etc
If the sounds are shared between the mini games then sure, you can put the game object in the collection where you are loading and unloading different mini games. You just need to make sure that you are using an absolute url to the game object, for instance “main:/sounds#sliding-sound1” etc (assuming sounds are in a collection with id “main” and in a game object with id “sounds”.
Having the sounds in a central place and in memory all the time will save you from loading the sounds each time you start a mini-game. But this is an optimization that you don’t necessarily have to make now. And it is easy to change later on.