Using playSound3D (sound from an object/position)

Workshop for all Mission Engineer Comrades. Home of the FA Mission Making Template.
Post Reply
User avatar
SuicideKing
Host
Posts: 312
Joined: Wed Nov 27, 2013 1:29 pm
Location: India/US West
Contact:

Using playSound3D (sound from an object/position)

Post by SuicideKing »

Привет товарищи!

I wanted to hear music playing in towns while we invaded/liberated/defended them and looted all the muffins from the local bakery. So I set about doing this:



STEPS ARE:
  1. Place a sound file in the mission root, or relative to it ("Music\Track1.ogg").
  2. You need to have a description.ext - if you're using F3 then you'll have it. Otherwise, create it (it can be blank too).
  3. Assuming you have an object from which you wish you want to play a sound from, you can put the following code in the init field:

    Code: Select all

    soundPath = [(str missionConfigFile), 0, -15] call BIS_fnc_trimString;
    soundToPlay = soundPath + "TRACKA.ogg";
    playSound3D [soundToPlay, this];
    Optionally, if you don't want music playing as soon as the mission starts (or the object spawns) then put the code in a trigger's activation field.
  4. You're done! Launch the mission and have fun.
If you're new to Arma scripting like me, you'll probably want some explanation for what I just made you do here. Fear not comrade! :eng101:
  1. playSound3D inexplicably wants absolute paths. This makes it very tricky to make it run on a dedicated server, for example. So we resort to hackery.
  2. missionConfigFile is the absolute path to descriptions.ext.
  3. We pass the full path to the trimString function, asking it to trim the last 15 characters (i.e. "description.ext"). We now have the path to the root mission folder, which is copied into the variable soundPath.
  4. soundToPlay is the path to the file that we wish to play. In the video, I have the sound (TRACKA.ogg) in the root folder. If it was in a subfolder, I'd write:

    Code: Select all

    soundToPlay = soundPath + "subfolder\TRACKA.ogg";
  5. soundToPlay is obtained by concatenating two strings: soundPath and the file name.
  6. The final line is the function call. It can take more parameters (see below), but I've only used the path to the file and the object name.
  7. Since I put the code in the object's init field, I used "this" in the call. If I would have used a trigger, the function call would have been:

    Code: Select all

    playSound3D [soundToPlay, speakerA];
    Where speakerA is the name of the object that has to play the sound.
Misc. Notes:
  • If the object moves, the sound doesn't move with it.
  • There's another command, say3D, that does let the sound move with the object. However, it doesn't have a global effect, so the sound may not play for everyone on the server.
  • dwarden had said on the Skype chat that they're only using relative paths going forward, so maybe in future this hackery won't be required for what is essentially just a single function call.
WHAT I DON'T KNOW YET:
  • Not sure how it'll behave on the server. I'm using global variables apparently (when I used _soundPath then the editor complained about local and global vars together), so I don't know how many times it'll trigger. If the object and triggers are server side only then I suppose it'll only trigger once. Will test and update.
  • I don't know how to run this in a separate script with a track list (as described here) or how to force it to run server side. I tried modifying Wolfenswan's GarrisonControl.sqf to no avail.
Further Info:
https://community.bistudio.com/wiki/playSound3D
http://killzonekid.com/arma-scripting-t ... sion-root/

Cheers. :science101:
themiddlevoid.wordpress.com

Post Reply