Wednesday, March 20, 2013

[TUT] Playing native sounds and external sounds

One interesting feature in scripts its the sounds, some effects become incomplete without the proper sound effect.

*** Check new method to play external sounds simultaneously here :) ***

  • Playing game sounds

Basic requirements:

-One of the play sound methods:

  • PLAY_SOUND
  • PLAY_SOUND_FROM_OBJECT
  • PLAY_SOUND_FROM_PED
  • PLAY_SOUND_FROM_POSITION
  • PLAY_SOUND_FROM_VEHICLE
  • PLAY_SOUND_FRONTEND
-Sound ID
-Sound name
-Method to stop sound (optional)
-Method to release sound ID (optional)

the PLAY_SOUND i don't know exactly how to use, never worked in my tests, so i will show only the other five methods.

* Edit - 31/03/2013
To play "non looped" sounds we can use -1 as sound ID because the sound will end automatically. So, to play sounds of weapons shooting, bullet impact, etc., we can use -1 as sound ID and we don't need to get an sound ID :)
*

Before we be able to play an looped sound, we need to get an sound ID to be able to control the sound with this ID in future, to get this ID we use the method GET_SOUND_ID and store the ID in an int32 variable:




Now we can proceed :)


Download the sample project here, let's use this project as the base for this tutorial.


PLAY_SOUND_FROM_OBJECT


As the name says, this will play an sound in an object, if the object moves, the sound moves too, but if the object its deleted the sound keeps in last object position :P

The parameters to use this method are:
  1. The sound ID
  2. The name of the sound
  3. The object where the sound will be played
Let's create an simple example? Ok, i will get all objects surrounding player, get the first that is with less than 5 units of distance and play the sound on it, lets use the sound GENERAL_WEAPONS_ROCKET_LOOP that is the looped sound of rocket flight:



Simple? Yes, it is.



PLAY_SOUND_FROM_PED
  1. The sound ID
  2. The name of the sound
  3. The pedestrian where the sound will be played






PLAY_SOUND_FROM_POSITION
  1. The sound ID
  2. The name of the sound
  3. The position (x, y and z) where the sound will be played







PLAY_SOUND_FROM_VEHICLE
  1. The sound ID
  2. The name of the sound
  3. The vehicle where the sound will be played









PLAY_SOUND_FRONTEND

This one will play an sound like an normal/external sound, without 3D positioning, the sound will always be the same. It needs:
  1. The sound ID
  2. The name of the sound






Ok, now we know how to play an sound, but how we stop it? Its simple too:


STOP_SOUND

just needs the sound ID:








We also can stop the sound playing other sound using same sound ID.


RELEASE_SOUND_ID

When the sound its no more needed, we can release the ID using RELEASE_SOUND_ID:










One thing that we cant do its release an sound ID without stopping it, for an loop sound like the one from the example this will result in an uncontrollable sound, its annoying.

After releasing the sound ID you must use GET_SOUND_ID to get an new sound ID to use.


_____________________________________________________________________________________________________
  • Playing audio event
We can play audio events too, its very similar to play_audio_ methods:


PLAY_AUDIO_EVENT - needs just the sound name

PLAY_AUDIO_EVENT_FROM_OBJECT - needs sound name and object

PLAY_AUDIO_EVENT_FROM_PED - needs sound name and ped

PLAY_AUDIO_EVENT_FROM_VEHICLE - needs sound name and vehicle



The harder part will be find the correct sound name to use, if we take a look into decompiled SCOCL files we will see some examples like:



"MOBILE_PHONE_SMS_RECIEVE"

 "WEAPON_PICKUP_HANDGUN"
 "WEAPON_PICKUP_SHOTGUN"
 "WEAPON_PICKUP_BARETTA"
 "WEAPON_PICKUP_MP5K"
 "WEAPON_PICKUP_UZI"
 "WEAPON_PICKUP_SNIPER_RIFLE"
 "BODY_ARMOUR_BUY"


_____________________________________________________________________________________________________
  • Playing external sounds

To play external sounds we can use an object of type SoundPlayer:




after creating the object we need to set the sound location and then load the sound:





now its just use the Play method to play the sound:





The bad side of using this method is that we can only play one sound at time, if we start playing an sound, then try to play another sound, the previous will be stopped, don't matter if we are using different sound files or different sound player objects.

*Always check the sound location to be sure that the sound file its available to be loaded, otherwise you will get an error in your script:







Download the script of this tutorial here




_____________________________________________________________________________________________________
  • The Sounds demo script and the sounds list

Maybe you are asking yourself now: Where i can find the name of the game sounds to use? Here.

Press Control+S to save the file in your Scripts folder, save with the name sounds.txt, why? Because you will use this txt file with my Sound Demo script, so, download the script and the sounds.txt file and put in your Scripts folder, in the game press " to see the console window, type the command "sd" to enable the script:



Now you will see the help menu:


This sound list i get from an GTA file using OpenIV, great part of them don't result in anything :(, so to help us find an usable sound i created an filter method, in the console window type "sfilter filter_string"

for example, lets filter sounds with string explosion:


Now close the console window and press Left/Right to change between filtered sounds, the name of the sound appears in the bottom of the screen and the sound should be played, you will easily find the sound "PIGEON_EXPLOSION" that is the sound of an poor pigeon dieing when shot ^^


If no sound is being played restart the game (restart not reload), this may happen after a lot of reloadscripts and GET_SOUND_ID calls :(

Some names are with extra characters from the original file, you can try to remove this extra chars using backspace, in great part of cases, remove the last char its enough, but remember, a lot of those sounds can't be played.

Need support using my mods? Contact me via my facebook page

Receive link to new posts in your Email

Recent Posts