Jump to content

Event handler


Nemaconz

Recommended Posts

Since there is no more RE and I cannot use BIS_fnc_MP because it has been friggin disabled, I am trying to get my server side AI to make some sounds using eventhandlers which I have limited experience with, the last time I messed with one was several months ago. I am trying to get the sounds to play on the clients since that is the closest thing I can get to having the sounds sync'd and am not having too much success. in my init.sqf for the mission file I have this:

if (isServer) then {

_aispawn = [[4019.26,8378.12,0],3000,100,1,0,0] execVM "addons\AI\test.sqf";

Noise = 0;
"Noise" addPublicVariableEventHandler {[_this select 1] execVM "test\sound.sqf";};

};

In my AI script I have this after the unit is created:

    Noise = 1;
    publicVariable "Noise";
    [Noise] execVM "test\sound.sqf";

and in my sound.sqf I have

if (!isDedicated) then {

    if (_this select 0 == 1) then
    {
      playSound "test";
      sleep 10;
      playSound "test";
      sleep 10;

      playSound "test";
      sleep 10;

      playSound "test";
      sleep 10;

      playSound "test";
      sleep 10;

      playSound "test";
      sleep 10;

      playSound "test";
      sleep 10;

      playSound "test";
      sleep 10;

      playSound "test";
      sleep 10;

      playSound "test";
      sleep 10;

      playSound "test";
      sleep 10;
      Noise = 0;
    };
    
    };
           
My AI spawns fine and does what I tell him to otherwise, but I still cannot get the sound to come across on my client, can anyone see a flaw in what I am doing? I have my sound file listed in my description.ext under CfgSounds and I have verified that the sound will play in game so I know that is not the problem. Thank you!
 

Link to comment
Share on other sites

You are adding the event handler to the server, it should be on the client which is where you want the event handler to fire.

if (isServer) then {

_aispawn = [[4019.26,8378.12,0],3000,100,1,0,0] execVM "addons\AI\test.sqf";

};

In mission init.sqf

if(hasInterface)then{
	"Noise" addPublicVariableEventHandler {[_this select 1] execVM "test\sound.sqf";};
};

Also, remove  [Noise] execVM "test\sound.sqf"; from your AI script, it is of no use.

I would also recommend using uiSleep instead of sleep, sleep is an older arma command, it can get stuck or even stop working and break your script, specially on server side if server is lagging or low performance/fps.

Link to comment
Share on other sites

Yea I gave that a go, still nothing, god I friggin hate these event handlers. I know once I get them down there will be nothing to them but until then I'm struggling to understand them, reading the wiki's and looking at other forums helps but it still doesn't put it together for me. I need to see a clear cut example of one from start to finish that isn't apart of a mega script. Thanks for the suggestion brother, I'm a few steps closer I'm sure. I'll get this eventually.

Link to comment
Share on other sites

interesting, then there is something else going on there, no way to know without seeing all of the scripts, but another thing I just now noticed:

 

 if (_this select 0 == 1)

 

You are sending a single value, not an array,  Noise = 1;

So when the client receives, [_this select 1] execVM "test\sound.sqf";  it is going to run as [1] execVM "test\sound.sqf";

Which will make _this = 1 in your sound.sqf,

Try:   if (_this == 1)

 

Other than that, check the rpt logs for errors and also use diag_log to log things from your script into the rpt log so you can see what is going on when the script runs.

Link to comment
Share on other sites

Ok I switched it up, in the AI file I have:

        while {alive _aiunit} do
{
    if ((_player distance _aiunit) < 500) then {
        SoundBroadcaster = [_aiunit,'_sound'];
        (owner _player) publicVariableClient 'SoundBroadcaster';
        uisleep 5;
    };
} forEach playableUnits;

with _player previously defined as: _player = playableunits;

I have this in my init:

 

if (!isDedicated) then {

Code = {
    'SoundBroadcaster' addPublicVariableEventHandler {
        _args = _this select 1;
        _aiunit = _args select 0;
        _soundClass = _args select 1;
        _aiunit say3D _soundClass;
    };
};

 

So I'm getting this error over and over again because obviously I have it looping:

while {alive _aiunit} do
{
if (_player distance _aiunit < 500) then {
SoundBr>
 0:03:16   Error position: <distance _aiunit < 500) then {
SoundBr>
 0:03:16   Error 0 elements provided, 3 expected
 0:03:16 File mpmissions\__cur_mp.Bornholm\addons\AI\Unit.sqf, line 26
 0:03:16 Error in expression <

I think it is expecting the position of the aiunit to be defined with a coordinated array, like [x,y,z], it's odd because in Arma 2 I could just use _player distance _unit with no problem. Even if I change _aiunit to_position and define it as _position = getPos _aiunit; I still get the error. Not sure how to define the variable so that it can get the elements it is expecting.

Link to comment
Share on other sites

Sorry looking at it I forgot a line at the bottom of the init PV, it is:

if (!isDedicated) then {

Code = {
    'SoundBroadcaster' addPublicVariableEventHandler {
        _args = _this select 1;
        _aiunit = _args select 0;
        _soundClass = _args select 1;
        _aiunit say3D _soundClass;
    };
};
publicVariable 'Code';

Link to comment
Share on other sites

Don't know if I am missing something, but there is no need for code = or publicVariable 'Code',  Code is not a public variable event handler so it will never run.

if (!isDedicated) then {
    'SoundBroadcaster' addPublicVariableEventHandler {
        _args = _this select 1;
        _aiunit = _args select 0;
        _soundClass = _args select 1;
        _aiunit say3D _soundClass;
    };
};

On your AI file, the error is probably due to a couple of things:

defining _player = playableUnits, playableUnits returns an array of every playable unit.

You are sending an array of units into (_player distance _aiunit), when it is expecting a single object.

forEach playableUnits is already iterating through each unit and each of them will be _x when they are checked, if that makes sense.

while {alive _aiunit} do{
	{		
		if ((_x distance _aiunit) < 500) then {
			SoundBroadcaster = [_aiunit,'_sound'];
			(owner _x) publicVariableClient 'SoundBroadcaster';			
		};		
	} forEach playableUnits;
	uisleep 5;
};
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
  • Discord

×
×
  • Create New...