Jump to content
Tory Xiao

How to addMPEventHandler for every unit in the mission?

Recommended Posts

I'm trying to add hitmarkers to my escape mission, but there are so many scripts that handle spawning AI infantry units, so I'll have to search every creatUnit and add  _unit addMPEventHandler ["MPHit",{call Eventhandler_fnc_MPHit}]; after it.

Is there any way to add eventhandler once for all of them?

Share this post


Link to post
Share on other sites

There is some wasted performance with this due to previous design choices but you can run a repeating loop over allUnits and set the event handler from 1 computer (server is probably best). Check against an object variable (get/setVariable) whether to add event handler or not. Adding MP event handler is already global so no need for remoteExec

  • Like 2

Share this post


Link to post
Share on other sites
On 5/13/2022 at 1:42 AM, Tory Xiao said:

Is there any way to add eventhandler once for all of them?

If you have CBA_A3 you can run this on initServer.sqf:

["CAManBase", "init", {
	(_this # 0) addMPEventHandler ["MPHit",{call Eventhandler_fnc_MPHit}];
}, true, [], true] call CBA_fnc_addClassEventHandler;

 

  • Like 1

Share this post


Link to post
Share on other sites

In initServer.sqf


 

[] spawn {
  while {true} do {
    {
      _x setVariable [MPhitPassed",TRUE];
      _x addMPEventHandler ["MPHit",{call Eventhandler_fnc_MPHit}];  // not sure calling this fnc will work as is but it's another problem
    } forEach (allUnits select {isNil {_x getvariable "MPhitPassed"}});
    sleep 1;
  };
};

 

  • Like 1

Share this post


Link to post
Share on other sites

The way I did it was, upon initPlayerLocal.sqf I remotely add an Score event handler on server which, based on the value of the score, remotely fires my hitmarker function back on player machine:

 

//initPlayerLocal.sqf

params ["_player", "_didJIP"];
[_player, "configScore.sqf"] remoteExec ["execVM", 2];

 

//configScore.sqf

params ["_player"];

_player addEventHandler ["HandleScore", {
    params ["_unit", "_object", "_score"];

    //Score == 1 means you killed one soldier enemy unit (be it in or out of a vehicle)
    if (_score isEqualTo 1) then {
        ["hitMarkerAnimation.sqf"] remoteExec ["execVM", _unit];
    };
}];

 

Keep in mind that if your mission has respawn of players, you'd have to re-assign the Score event handler to the respawned unit. So repeat the initPlayerLocal.sqf lines on onPlayerRespawn.sqf (changing _player for _newUnit):

 

//onPlayerRespawn.sqf

params ["_newUnit", "_oldUnit", "_respawn", "_respawnDelay"];
[_newUnit, "configScore.sqf"] remoteExec ["execVM", 2];

 

Whenever you kill an enemy unit, wherever it's spawned from, or how it was handled by scripts, you get your hitmarker and are free from tracking enemy unit creation process.

  • Like 1

Share this post


Link to post
Share on other sites
4 hours ago, NunesSergio said:

The way I did it was, upon initPlayerLocal.sqf I remotely add an Score event handler on server which, based on the value of the score, remotely fires my hitmarker function back on player machine:

 

//initPlayerLocal.sqf


params ["_player", "_didJIP"];
[_player, "configScore.sqf"] remoteExec ["execVM", 2];

 

//configScore.sqf


params ["_player"];

_player addEventHandler ["HandleScore", {
    params ["_unit", "_object", "_score"];

    //Score == 1 means you killed one soldier enemy unit (be it in or out of a vehicle)
    if (_score isEqualTo 1) then {
        ["hitMarkerAnimation.sqf"] remoteExec ["execVM", _unit];
    };
}];

 

Keep in mind that if your mission has respawn of players, you'd have to re-assign the Score event handler to the respawned unit. So repeat the initPlayerLocal.sqf lines on onPlayerRespawn.sqf (changing _player for _newUnit):

 

//onPlayerRespawn.sqf


params ["_newUnit", "_oldUnit", "_respawn", "_respawnDelay"];
[_newUnit, "configScore.sqf"] remoteExec ["execVM", 2];

 

Whenever you kill an enemy unit, wherever it's spawned from, or how it was handled by scripts, you get your hitmarker and are free from tracking enemy unit creation process.

No respawn for players. In my mission once a player is down he became invincible, and respawn button is disabled, he can only wait for teammates to revive or rejoin the server

Thanks a lot! I'll try the script

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

×