Jump to content
Sign in to follow this  
sphoenix

Proper reaction to weapon sounds

Recommended Posts

Hey everybody!

I'm trying to properly recreate the effect of weapon sounds on the AI but I'm having trouble with the SQF syntax.

Here's my code:

_unit = _this select 0;

_weapon = _this select 1;

_muzzle = _this select 2;

_mode = _this select 3;

_ammo = _this select 4;

ArraySilencedWeap = ["M4A1SD","MP5SD","M9SD","AKS74UN","MakarovSD

"];

ArraySilencedWeapFactor = [0.5,0.05,0.7,0.5,0.6];

ArraySilencedAmmo = ["B_556x45_SD","B_9x19_SD","B_545x39_SD","B_9x18_SD&quo

t;];

if !(_weapon in ArraySilencedWeap) then {_AlertRadius = 500; _RandomFactor = 1};

if (_weapon in ArraySilencedWeap) && (_ammo in ArraySilencedAmmo) then

{

_AlertRadius = 15;

_number = ArraySilencedWeap find format ["%1",_weapon];

_RandomFactor = ArraySilencedWeapFactor select _number;

};

if (_weapon in ArraySilencedWeap) && !(_ammo in ArraySilencedAmmo) then

{

_AlertRadius = 70;

_number = ArraySilencedWeap find format ["%1",_weapon];

_RandomFactor = (ArraySilencedWeapFactor select _number) + 0.2;

};

_nearEnemies = position _unit nearObjects ["SoldierEB", _AlertRadius];

_cluelessEnemies = [];

_otherEnemies = list OPFOR - _nearEnemies;

{

_x setBehaviour "SAFE";

_x setTargetAge "120 MIN";

} forEach _otherEnemies;

{

_distance = _x distance _unit;

_Factor0 = 10*_RandomFactor + 1;

_RandomFactor3 = _Factor0 - _distance

_RandomFactor2 = 1 /_RandomFactor3;

_Randomized = ceil (random _RandomFactor2);

if (_Randomized == 0) then

{

while {_Randomized == 0} do

{

_Randomized = ceil (random _RandomFactor2);

};

};

if (_Randomized == 1) then

{

_x reveal _unit;

_x setBehaviour "COMBAT";

SoundAlarm = true;

publicVariable "SoundAlarm";

};

if !(_Randomized == 1) then

{

_x setBehaviour "AWARE";

_cluelessEnemies = _cluelessEnemies + [_x];

};

} forEach _nearEnemies;

{

_x setBehaviour "SAFE";

_x setTargetAge "120 MIN";

} forEach _cluelessEnemies;

It is activated by an Event Handler "fired".

I'm getting a "Division by zero" error with both "ArraySilencedWeapFactor select _number"  in the script, as well as a { missing for the last forEach.

I'm really clueless.

Could anyone help me?

Thank you very much.

SPhoenix

Share this post


Link to post
Share on other sites

Any local variable initially defined within {}, then referenced within separate {} or later on in the script, like _AlertRadius, _number and _RandomFactor. Need to be defined at the root of the script, to make them available to all the other scopes i.e {}.

For example:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_unit = _this select 0;

_weapon = _this select 1;

_muzzle = _this select 2;

_mode = _this select 3;

_ammo = _this select 4;

_AlertRadius=-1;

_number=-1;

_RandomFactor=-1;

Note, all the local enemy variables, _nearEnemies, _cluelessEnemies and _otherEnemies are fine. As they are defined in the root or parent scope of the script, so by default they are available to all the child scopes, contained in the following {}.

Do a search on the wiki for scope to get more info.

Scope is a odd concept to get your head around if you’re new to programming. But if you add this line to the end of your script before making any other changes, and run it:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">{_x setBehaviour "SAFE";

_x setTargetAge "120 MIN";

} forEach _cluelessEnemies;

Player SideChat Format ["_nearEnemies=%1 _cluelessEnemies=%2 _otherEnemies=%3 _AlertRadius=%4 _number=%5 _RandomFactor=%6",_nearEnemies,_cluelessEnemies,_otherEnemies,_AlertRadius,_number,_RandomFactor]

Then add the suggestion I made above and run it again, you should see the difference?

Share this post


Link to post
Share on other sites

I completely get the concept of scope don't worry wink_o.gif

I'm just not familiar with the SQF syntax.

Thank you very much for your help, I'll try that tomorrow.

Share this post


Link to post
Share on other sites

Well it's hard to read scripts posted as standard text without indents, so I can't say for sure it will help solve any problems. But I don't think scope is anything special to the sqf format, verses the sqs format, the same thing still applies.

I just stuck my neck out and made an assumption smile_o.gif

Share this post


Link to post
Share on other sites

Oh yeah sorry about the indentation, I do have it on my script, but when I copy pasted it I lost it.

I'll edit to make it visible.

I did declare the variables as you said I should; I still have that Division by zero error.  huh.gif

I'm completely stalled, I have no idea where it comes from.

EDIT: The indentation is there... The forum just doesn't allow for it. mad_o.gif

Share this post


Link to post
Share on other sites
Quote[/b] ]Oh yeah sorry about the indentation, I do have it on my script, but when I copy pasted it I lost it.

Yeah, it's a pain the forum code won't support tabs. I usually do a search and replace on my code with notepad, swapping each of the tabs for eight spaces.

Quote[/b] ]I still have that Division by zero error.

That error is caused by trying to select an array element that does not exist. Double check the results of your Find commands with sidechats, stored in the _number variable.

Your code looks ok, you’re checking to see if the weapon is in the array before using find. But perhaps the Find command is somehow failing and returning -1 instead of a valid index?

Share this post


Link to post
Share on other sites

Ok... after many tries, I've identified the problem.

The script enters the code block even when the condition to enter isn't true.

Which creates the error (because the weapon isn't in the silenced weapon array).

I've tried adding if not condition exitwith but it doesn't work.

If my weapon is silenced: it's all good... except it says, at the end, a { is missing for the forEach _cluelessEnemies.

Alright. I need your help UNN sad_o.gif

I've tried many things...

Share this post


Link to post
Share on other sites

Like I said in another thread, reduce the problem to something easier to handle. For example get this working first:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_unit = _this select 0;

_weapon = _this select 1;

_muzzle = _this select 2;

_mode = _this select 3;

_ammo = _this select 4;

_AlertRadius=-1;

_number=-1;

_RandomFactor=-1;

Player SideChat Format ["Weapon %1 Muzzle %2 Mode %3 Ammo %4",_Weapon,_Muzzle,_Mode,_Ammo];

ArraySilencedWeap=["M4A1SD","MP5SD","M9SD","AKS74UN","MakarovSD"];

ArraySilencedWeapFactor=[0.5,0.05,0.7,0.5,0.6];

ArraySilencedAmmo=["B_556x45_SD","B_9x19_SD","B_545x39_SD","B_9x18_SD"];

if !(_weapon in ArraySilencedWeap) then

       {

       _AlertRadius = 500;

       _RandomFactor=1;

       }

       Else

       {

       If (_ammo in ArraySilencedAmmo) Then

               {

               _AlertRadius = 15;

               _number = ArraySilencedWeap find format ["%1",_weapon];

               _RandomFactor = ArraySilencedWeapFactor select _number;

               }

               Else

               {

              _AlertRadius = 70;

               _number = ArraySilencedWeap find format ["%1",_weapon];

              _RandomFactor = (ArraySilencedWeapFactor select _number) + 0.2;

               };

        };

Player SideChat Format ["AlertRadius %1 number %2 RandomFactor %3",_AlertRadius,_number,_RandomFactor];

Once you're are happy that everything is working as expected, add the next bit:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_unit = _this select 0;

_weapon = _this select 1;

_muzzle = _this select 2;

_mode = _this select 3;

_ammo = _this select 4;

_AlertRadius=-1;

_number=-1;

_RandomFactor=-1;

Player SideChat Format ["Weapon %1 Muzzle %2 Mode %3 Ammo %4",_Weapon,_Muzzle,_Mode,_Ammo];

ArraySilencedWeap=["M4A1SD","MP5SD","M9SD","AKS74UN","MakarovSD"];

ArraySilencedWeapFactor=[0.5,0.05,0.7,0.5,0.6];

ArraySilencedAmmo=["B_556x45_SD","B_9x19_SD","B_545x39_SD","B_9x18_SD"];

if !(_weapon in ArraySilencedWeap) then

       {

       _AlertRadius = 500;

       _RandomFactor=1;

       }

       Else

       {

       If (_ammo in ArraySilencedAmmo) Then

               {

               _AlertRadius = 15;

               _number = ArraySilencedWeap find format ["%1",_weapon];

               _RandomFactor = ArraySilencedWeapFactor select _number;

              }

              Else

              {

              _AlertRadius = 70;

              _number = ArraySilencedWeap find format ["%1",_weapon];

              _RandomFactor = (ArraySilencedWeapFactor select _number) + 0.2;

               };

       };

Player SideChat Format ["AlertRadius %1 number %2 RandomFactor %3",_AlertRadius,_number,_RandomFactor];

_nearEnemies = position _unit nearObjects ["SoldierEB", _AlertRadius];

_cluelessEnemies = [];

_otherEnemies = list OPFOR - _nearEnemies;

{

_x setBehaviour "SAFE";

_x setTargetAge "120 MIN";

} forEach _otherEnemies;

{

_distance = _x distance _unit;

_Factor0 = 10*_RandomFactor + 1;

_RandomFactor3 = _Factor0 - _distance

_RandomFactor2 = 1 /_RandomFactor3;

_Randomized = ceil (random _RandomFactor2);

} forEach _nearEnemies;

Player SideChat Format ["nearEnemies %1 otherEnemies %2",_nearEnemies,_otherEnemies];

Once that’s working ok, add the rest of your If statements into the nearenemies foreach loop. From the error you mentioned you're probably missing, or have an extra curly bracket in them.

Edit: Removed a pointless bit of code from the example.

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  

×