In working on AI stuff, I created a handy utility function to have AIPlayers make sounds based on a simple function call. Instead of remembering the names of SFXProfile datablocks and so on, I wanted to just call a function with a textual representation of what I wanted the character to say. I also wanted the system to be generic enough to handle different voice types.

The solution I came up with revolves around using an ArrayObject to define a character's 'voice'. This array maps text phrases like "ouch", "hello" or "must have been rats" to SFXProfile objects that contain the appropriate sound files for each phrase. Then, to make a Player say the appropriate phrase, you can use text rather than referring to the sound file directly.

Simply paste the following code into a new file 'game/scripts/server/speech.cs':

Note that I assume you're using the Full template, so the SFXProfiles PainCrySound and DeathCrySound already exist.

Then, in 'scriptExec.cs', add the following line somewhere:

Away you go! Try calling this in the console:

You can define a new voice object by simply copying the example at the bottom of the script. I haven't included this, but you might want to, for example, add a voice object to each PlayerData block. Then, for example, in a damage script, you could write this:

Now by giving different datablocks different voice objects, you can change the way each character sounds without having to change any scripts.