Jump to content
Sign in to follow this  
CaptainBravo

How to attach sound to spawned animal classname

Recommended Posts

I need help with a mission where I need to attach sound file to all animals. So "fin" dogs will have a sound file. These animals will spawn half way through mission and was wondering if there was a way to attach the same sound file to all animals with same class name.

Thanks for your help.

Share this post


Link to post
Share on other sites

Hello and first of all I must clarify that it is good to be back after a sabbatical of almost 2 years (I hope not to have lost touch :cool:)

1 - Create an AI center for the civil side (for example)

_center = createcenter civilian;

2 - Create a group belonging to that side

_grp = creategroup civilian;

3 - Create an animal (in this example is a cow) in the location of a logical unit (UL_location) and a random distance of 10 meters of it.

_animal = _grp createUnit ["COW01_EP1", getpos UL_location, [], 10, "FORM"];

4 - We put the animal in the "safe" to ignore any type of training group it belonged to a group with more than one unit.

_animal setbehaviour "safe";

5 - Create a sound source such as "sound_cow" (see the list of cfgvehicles for other sounds: dogs, sheep, crows, etc)

_soundOn = createSoundSource ["Sound_Cow", position _animal, [], 0];

6 - We attach the source of sound to the animal to move along it.

_soundOn attachto [_animal, [0,2,1]];

I use it a lot in a script that generated their own groups of animals in dynamic form according to presence / absence of any side.

Herds Generator:

// *******************************************************************************
// **       Funcion: Herds.sqf
// **   Descripcion: Generation dynamics of groups of animals by the presence of side
// *******************************************************************************
// **         Autor: RAVEN
// ** Parameters: 1- Array of sides to detect
// ** Parameters: 2- Logic unit that serves the flock location
// ** Parameters: 3- array of values ​​to calculate minimum and maximum number of animals
// ** Parameters: 4- radius of the area of generation
// ** Parameters: 5- class name to generate animal
// **
// ** Example: nul = [[west,resistance], UL_location, [5,15], 30, "cow01_ep1"] execVM "Herds.sqf";
// *******************************************************************************

private ["_animal","_canMin","_canMax","_px","_py","_tope","_ani","_radares","_sonidos","_genera","_pos"];

_sides = _this select 0;
_donde = _this select 1;
_can = _this select 2;
_range = _this select 3;
_animal = _this select 4;

_centro = createcenter civilian;
_grp = creategroup civilian;

_canMin = _can select 0;
_canMax = _can select 1;
if (_canMin == _canMax) then {_tope = _canMax} else {_tope = (random(_canMax - _canMin) + _canMin)};

_pos = position _donde;
_px = _pos select 0;
_py = _pos select 1;

_radares = [];
{
_target = format ["%1", _x];
_radar = createTrigger ["EmptyDetector", _pos];
_radar setTriggerActivation [_Target, "present", true];
_radar setTriggerArea [500, 500, 0, true];
_radar setTriggerStatements ["this", "", ""];
_radar setTriggerTimeout [0, 0, 0, false];
_radar setTriggerType "SWITCH";

_radares = _radares + [_radar];
} foreach _sides;

_yaG = false; _aborta = false;
while {not _aborta} do
{
_genera = false; 
{
	_x setpos _pos;
	_detect = LIST _x;
	sleep 1;
	if (count _detect > 0) then {_genera = true};
} foreach _radares;
if (_tope < 1) then {_genera = false; _yaG = false};
sleep 10;

if (_genera and not _yaG) then
{
	_sonidos = []; _c = 0;
	for [{_i = 0}, {_i < _tope}, {_i = _i + 1}] do
	{
		_ani = _grp createUnit [_animal,_pos, [], _range, "FORM"];
		_ani setbehaviour "safe";
		if (_c == 7) then
		{
			_soundOn = createSoundSource ["Sound_Cow", position _ani, [], 0];
			_soundOn attachto [_ani, [0,2,1]];
			_sonidos = _sonidos + [_soundOn];
			_c = 0;
		};
		_c = _c +1;
	};
	_yaG = true;
};

if (not _genera and _yaG) then
{
	_tope = count units _grp;
	_pos = position (leader _grp);
	for [{_i = 0}, {_i < count _sonidos}, {_i = _i + 1}] do
	{
		deletevehicle (_sonidos select _i);
	};
	{deletevehicle _x} foreach (units _grp);
	_yaG = false; _sonidos = [];
};
if (_tope < 1) then {_aborta = true};
};

The generator checks every 10 seconds by the presence of any given side and generates the herd (the distance of detection is 500 meters but can be changed in the script to your taste). For absence, remove the animals, replacing the same amount he had when removed, dead animals do not regenerate.

I hope it is useful

Greetings from Argentina.

Edited by RAV_RAVEN

Share this post


Link to post
Share on other sites

Thanks guys for the help.

@BighotKing: Here is the spawn script used for dog patrol by Patrulla Perruna:

// Patrulla Perruna | Dog Patrol

// Version: 0.0.3

//----------------------------------------------------------------------------------------------------------

if (Isserver) then {

_unidad = _this select 0;

_RazaDeseada = _this select 1;

_cantidad = _this select 2;

_bando = side _unidad;

_grupo = creategroup _bando;

for "_i" from 1 to _cantidad do {

_raza = "Pastor";

switch (_RazaDeseada) do {

case "PASTOR":

{

_raza = "Pastor";

};

case "FIN":

{

_raza = "Fin";

};

default

{

_raza = "Pastor";

};

};

//----------------------------------------------------------------------------------------------------------

//SE CREA LA NUEVA UNIDAD EN EL GRUPO DE LA UNIDAD AMO. SE LE DA FORMATO.

//----------------------------------------------------------------------------------------------------------

_NombrePerro = format ["k9%1",round (random 1000)];

call compile format ['"%2" createUnit [getpos _unidad, _grupo,"%1=this; this setSpeedMode ""NORMAL""; this setbehaviour ""SAFE""",1]',_NombrePerro,_raza];

_perro = call compile format ["%1",_NombrePerro];

[_perro,_unidad] spawn {

_perro = _this select 0;

_unidad = _this select 1;

//----------------------------------------------------------------------------------------------------------

//INICIA LA IA DEL PERRO. SE DEFINE COMPORTAMIENTO SEGUN FACTORES Y SITUACION

//----------------------------------------------------------------------------------------------------------

while {alive _perro} do

{

//----------------------------------------------------------------------------------------------------------

//VERIFICACION DE ENTIDADES CERCANAS

//----------------------------------------------------------------------------------------------------------

_HumanosCerca = [];

_HumanosVivos = [];

_distancia = 1000;

_cercano = objNull;

_HumanosCerca = nearestObjects [position _perro, ["man"], 150];

{if ((side _perro)getFriend (side _x) <0.6) then {_HumanosVivos = _HumanosVivos + [_x];_perro knowsabout _x; _unidad knowsabout _x;}}forEach _HumanosCerca;

//----------------------------------------------------------------------------------------------------------

//ENEMIGOS CERCA SE PROCEDE A PERSEGUIRLOS. SITUACION MOVIMIENTO

//----------------------------------------------------------------------------------------------------------

if (count _HumanosVivos >0) then {

_HumanoCercano = _HumanosVivos select 0;

_distancia = (position _perro) distance (_HumanoCercano);

_perro setspeedmode "FULL";

if ((_distancia > 125) && (_distancia < 150)) then {

_perro dowatch _HumanoCercano;

_TipoLadrido = round(random 2);

switch (_TipoLadrido) do {

case 0:

{

[_perro, "ladrido1"] call CBA_fnc_globalSay3d;

};

case 1:

{

[_perro, "ladrido2"] call CBA_fnc_globalSay3d;

};

case 2:

{

[_perro, "ladrido3"] call CBA_fnc_globalSay3d;

};

};

};

if ((_distancia > 50) && (_distancia < 125)) then {

_perro domove position _HumanoCercano;

_TipoLadrido = round(random 2);

switch (_TipoLadrido) do {

case 0:

{

[_perro, "ladrido1"] call CBA_fnc_globalSay3d;

};

case 1:

{

[_perro, "ladrido2"] call CBA_fnc_globalSay3d;

};

case 2:

{

[_perro, "ladrido3"] call CBA_fnc_globalSay3d;

};

};

};

if ((_distancia > 25) && (_distancia < 50)) then {

_perro domove position _HumanoCercano;

_TipoLadrido = round(random 2);

switch (_TipoLadrido) do {

case 0:

{

[_perro, "ladrido1"] call CBA_fnc_globalSay3d;

};

case 1:

{

[_perro, "ladrido2"] call CBA_fnc_globalSay3d;

};

case 2:

{

[_perro, "ladrido3"] call CBA_fnc_globalSay3d;

};

};

};

if ((_distancia > 15) && (_distancia < 25)) then {

_perro domove position _HumanoCercano;

_TipoLadrido = round(random 2);

switch (_TipoLadrido) do {

case 0:

{

[_perro, "ladrido1"] call CBA_fnc_globalSay3d;

};

case 1:

{

[_perro, "ladrido2"] call CBA_fnc_globalSay3d;

};

case 2:

{

[_perro, "ladrido3"] call CBA_fnc_globalSay3d;

};

};

};

if ((_distancia > 5) && (_distancia < 15)) then {

_TipoLadrido = round(random 1);

switch (_TipoLadrido) do {

case 0:

{

[_perro, "ladrido2"] call CBA_fnc_globalSay3d;

};

case 1:

{

[_perro, "ronroneo1"] call CBA_fnc_globalSay3d;

};

};

_perro domove position _HumanoCercano;

};

if ((_distancia > 2) && (_distancia < 5)) then {

_TipoLadrido = round(random 1);

switch (_TipoLadrido) do {

case 0:

{

[_perro, "ladrido3"] call CBA_fnc_globalSay3d;

};

case 1:

{

[_perro, "ronroneo1"] call CBA_fnc_globalSay3d;

};

};

_perro domove position _HumanoCercano;

};

if (_distancia < 2) then {

_perro domove position _HumanoCercano;

//----------------------------------------------------------------------------------------------------------

//SITUACIÓN DE ATAQUE. SE DEFINE DAÑO Y EL PROCESO DEL DAÑO

//----------------------------------------------------------------------------------------------------------

if (vehicle _HumanoCercano isKindOf "CAManBase") then {

_ComprobarDanoHumano = getDammage _HumanoCercano;

_DanoMordisco = _ComprobarDanoHumano + 0.3;

[_perro, "mordisco"] call CBA_fnc_globalSay3d;

_HumanoCercano setdamage _DanoMordisco;

_HumanoCercano switchMove "AdthPercMstpSlowWrflDnon_8";

[_HumanoCercano, "grito"] call CBA_fnc_globalSay3d;

sleep 5;

_DanoMordisco = "";

_HumanoCercano switchMove "";

sleep 0.5;

};

//----------------------------------------------------------------------------------------------------------

//FIN DE LA SITUACION DE ATAQUE. SE CIERRA LOS OTROS PROCESOS

//----------------------------------------------------------------------------------------------------------

};

};

sleep 0.5;

//----------------------------------------------------------------------------------------------------------

//SITUACION PERRO ABATIDO EN EL ATAQUE. SE DEFINE UNA LOGICA DE JUEGO PARA REPRODUCIR SONIDOS

//----------------------------------------------------------------------------------------------------------

if (not alive _perro) then {

//Se crea el grupo en el sidelogic para crear el game logic debido a que el comando say no se ejecuta en unidades muertas.

_GrupoSideLogic = createGroup sideLogic;

//Se crea la unidad en el grupo sidelogic, en este caso la unidad es un gamelogic

_PerroMuerto = _GrupoSideLogic createUnit ["logic", position _perro, [], 0, "none"];

[_PerroMuerto, "llanto"] call CBA_fnc_globalSay3d;

sleep 5;

//Se borra el gamelogic

deleteVehicle _PerroMuerto

};

//----------------------------------------------------------------------------------------------------------

//SITUACION MOVIMIENTO CON Y HACIA EL AMO. SE DEFINE MATEMATICAMENTE LA POSICION A LA CUAL EL PERRO SE MOVERA

//----------------------------------------------------------------------------------------------------------

_perro domove [(getpos _unidad select 0) - ((random(10)+30)*sin(getdir _unidad - 180)), (getpos _unidad select 1) - ((random(10)+30)*cos(getdir _unidad - 180))];

_perro setspeedmode "NORMAL";

};

//----------------------------------------------------------------------------------------------------------

//SE CIERRAN TODAS LAS FUNCIONES, VARIABLES, PROCESOS Y SENTENCIAS.

//----------------------------------------------------------------------------------------------------------

};

};

};

};

//----------------------------------------------------------------------------------------------------------

//FIN DEL SCRIPT

//----------------------------------------------------------------------------------------------------------

@RAV RAVEN: Thanks or the script. How would I modify it to fit above script where sound attached to dogs?

Edited by CaptainBravo

Share this post


Link to post
Share on other sites

@CaptainBravo

To use a sound source dog just change this line of code that looks like this:

_soundOn = createSoundSource ["Sound_Dog", position _ani, [], 0];

Just keep in mind that the herds script creates a sound source each 7 cows. (Not one for each)

Sound Class
Sound_Owl
Sound_Stream
Sound_Frog
Sound_Frogs
Sound_Chicken
Sound_Cock
Sound_Cow
Sound_Crow
Sound_Wolf
Sound_Dog
Sound_BadDog
Sound_SorrowDog
Sound_LittleDog
Sound_Flies

Regards.

Share this post


Link to post
Share on other sites
@CaptainBravo

To use a sound source dog just change this line of code that looks like this:

_soundOn = createSoundSource ["Sound_Dog", position _ani, [], 0];

Just keep in mind that the herds script creates a sound source each 7 cows. (Not one for each

Regards.

I mean where do I place _soundOn = createSoundSource ["Sound_Dog", position _ani, [], 0] in the DogPatrol script. I have tried adding that line to script but did not work.

So not sure where/how to add?

Thanks.

Share this post


Link to post
Share on other sites

@CaptainBravo

Locate this line of code in your script:

_perro = call compile format ["%1", _NombrePerro];

And add these 2 lines below:

_soundOn = createSoundSource ["Sound_Dog", position _perro, [], 0];
_soundOn attachto [_perro, [0,1,0.5]];

That's enough to link the sound to the dog.

Something not mentioned is that if the dog dies, the sound must be removed (deletevehicle _soundOn).

Try and tell me if there is a problem. Greetings.

Edited by RAV_RAVEN

Share this post


Link to post
Share on other sites
@CaptainBravo

Locate this line of code in your script:

_perro = call compile format ["%1", _NombrePerro];

And add these 2 lines below:

createSoundSource _soundOn = ["Sound_Dog", position _perro, [], 0];
_soundOn attachto [_perro, [0,1,0.5]];

That's enough to link the sound to the dog.

Something not mentioned is that if the dog dies, the sound must be removed (deletevehicle _soundOn).

Try and tell me if there is a problem. Greetings.

Thanks Rav Raven for the help. For some odd reason when I add those 2 lines to the script, the dogs stop spawning :confused:

Not sure why ?

Share this post


Link to post
Share on other sites

Thanks Rav Raven.

It works well except when the dogs die, the sound is still there. It is almost like having ghosts of dogs running around :)

Anyway of disabling/stopping sound once the dogs are dead?

Share this post


Link to post
Share on other sites

Just add these 2 lines (color red) of code after creating the sound and link it to the dog.

_soundOn = createSoundSource ["Sound_Dog", position _perro, [], 0];

_soundOn attachto [_perro, [0,1,0.5]];

[color="#FF0000"]_perro setvariable ["soundDog", _soundOn];

_EHkilledIdx = _perro addEventHandler ["killed", {deletevehicle ((_this select 0) getvariable "soundDog")}];[/color]

The variable that refers to the sound of the dog is stored (SetVariable) in the memory space corresponding to the unit (dog) and when the dog dies activates the eventhandler that read this variable (GetVariable) and uses it to eliminate the sound (deletevehicle).

Greetings.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×