local sfx\_warble = audio.loadSound( "warble.wav" )
I want to be able to play the sound from a function by passing it a string of the handle name. Something like:
function playSoundFX(sound)
local sfxChannel = audio.play(sound)
end
and then calling it with a line like
playSoundFX("sfx\_warble")
The above doesn’t work since the string is not the sound. Is there some way to, I don’t know, cast the string as a handle? [import]uid: 9422 topic_id: 8248 reply_id: 308248[/import]
XenonBL, what is the advantage of what you want to achieve compared to using the sound handle itself? For me it looks just like a detour. [import]uid: 5712 topic_id: 8248 reply_id: 29469[/import]
I was wondering that at first too, but I figure that the code he posted is just a minimal example to demonstrate his point and that more will happen in the playSoundFX() function than just calling audio.play() For example, maybe he also has some graphic display when the sound plays, or maybe he has code in playSoundFX() that only plays new sounds if they aren’t already playing.
…
Y’know, another reason would be something I do in my own code and just forgot about. All my audio is handled in one module but can be played from several linked modules, so it’s better to call a public playSound() method and have everything within the module handled via local variables.
In fact, his declaring playSoundFX() as a global function would make sense if he was using it through a separate module. [import]uid: 12108 topic_id: 8248 reply_id: 29472[/import]
I’m using modules to structure my code. One module, “AudioController”, handles most of my audio requirements. Within it I define and preload all the sound FX, allocate specific channels for different types of sounds, define music streams, and have functions to actually play the sounds. Then, from anywhere else in the code I can simply call, for example, AudioController:playSoundFX(“warble”) to play that particular effect.
The issue I’m having is my current playSoundFX function is a huge mass of elseif statements, one for each sound effect. It seems kind of inelegant, and probably slow to do it that way. If I could pass to the playSoundFX function the name of the sound the function would be nearly as simple as I illustrated above.
@jhocking, thanks for the advice to use a table to define the sounds. I think it’s exactly the right way for me to do this.
[import]uid: 9422 topic_id: 8248 reply_id: 29487[/import]
Thanks, jmp909, that’s pretty much how I’m handling it now. I’m also passing in a few parameters to control which channel to play a sound on (so certain sounds don’t play on top of each other) as well as volume. [import]uid: 9422 topic_id: 8248 reply_id: 29769[/import]