Jump to content
Sign in to follow this  
avibird 1

Help with addEventHandler to add back a script to respawn player and spawned AI

Recommended Posts

Really need help on this! addEventHandler to add back a script to respawn player or spawned AI units during game play.

I am using this great Unlimited Ammo Script by XxanimusxX it works great for players and AI placed via editor however after the player gets killed and respawns or if you spawn AI during the mission the script does not work.

UnlimitedAmmo.sqf

//Unlimited Ammo Script by XxanimusxX
_unit = _this select 0;
_ammoCount = _this select 1;
_primeWpn = "";
_prevWpn = "";
_magazineType = "";
_foundMags = 0;

while {alive _unit} do {
   waitUntil {sleep 0.5; currentWeapon _unit != ""};
   _primeWpn = currentWeapon _unit;

   if (_primeWpn != _prevWpn) then
   {
       _prevWpn = _primeWpn;
       _magazineType = getArray(configFile >> "cfgWeapons" >> _primeWpn >> "magazines") select 0;
   };

   _foundMags = 0;
   {
       if (_x == _magazineType) then
       {
           _foundMags = _foundMags + 1;
       };
   } forEach (magazines _unit);

   if (_foundMags < _ammoCount) then
   {
       for "_i" from 1 to (_ammoCount - _foundMags) do
       {
           _unit addMagazine _magazineType;
       };
   };

   sleep 1;
};  

this code goes in the mission.init.sqf

//Unlimited Ammo*****************************************************
grantUnlimitedAmmo = compile preprocessFileLineNumbers "unlimitedAmmo.sqf";

[player, 2] spawn grantUnlimitedAmmo;  //controls player ammo
{                                     
   if (!(isPlayer _x)) then 
   { 
       [_x, 2] spawn grantUnlimitedAmmo;  //controls AI ammo
   }; 
} foreach allUnits; 
//Unlimited Ammo*****************************************************

I have attempt multiple things to get the EH to work with no luck. My last attempt was this I put this in my mission init.sqf

if (isServer) then {
   {
       if (!(isPlayer _x)) then {
           _x addEventHandler ["Ammo", "(_this select 0) execVM 'UnlimitedAmmo.sqf'"];
       };
   } foreach allUnits;
} else {
   if (isDedicated) exitWith {};
   waitUntil {!(isNull player)};
   player addEventHandler ["Ammo", "(_this select 0) execVM 'UnlimitedAmmo.sqf'"]; 
};  

I know this can be done but I have never been good with EH and had issues with this with ARMA2. Anyfeed back or assist would be great. Avibird.

Share this post


Link to post
Share on other sites

create a file in your mission directory named

onPlayerRespawn.sqf

put in it:

[player, 1] spawn grantUnlimitedAmmo;

place this in init.sqf

[] spawn {
if !(isServer) exitWith {};  // exit if not server.
_units = [];  // empty array for check.
while {true} do {
	{
		if (!(_x in _units) AND !(isplayer _x)) then {
			[[_x, 1], "grantUnlimitedAmmo", _x] call BIS_fnc_MP;
			_units = _units + [_x];
		};
	} foreach allUnits;
	{
		if !(alive _x OR isNull _x) then {_units = _units - [_x]};
	} foreach _units;
	sleep 1;
};
};

this only handles the respawn part of player and also adds any place AI and spawned ai.

do the same as you did before with player, but do not add ai, they will all be auto added with the above script.

should work with SP and MP (dedi and host).

not tested.

Share this post


Link to post
Share on other sites

@ Demonized you are a scripting genius :yay: It works perfect following death and AI units that are spawned during the mission:cool: This will definitely help out with the whole rearm thing that ARMA3 is still lacking and never really addressed by BOHEMIA after all these years. This is now the perfect script that will give units (players and or AI Unlimited Ammo for any weapon they are currently using. It does not allow for multiple rounds to be fired at once like most Unlimited Ammo scripts and or codes that are currently around. You just get additional magazines added so you never have less then two however The script can easily be reduced to only one magazines and or can be adjusted so only the AI gets the magazines and not the Player. Thank you for your help like always brother. Good to see you somewhat back into the game:)

Do you mind if I put together a little example mission showing how this works on Armaholic. I will also ask XxanimusxX. I think there is no other script out there like this and I am sure a lot of people can use it. Avibird

Edited by AVIBIRD 1

Share this post


Link to post
Share on other sites

Since I can't create my own thread I'm just going to post here.

I've been trying to use addMPEventHandler to run a script upon the player respawning, because TFAR for some reason, when you respawn you spawn with a radio. It seems that the event handler isn't running the script and has failed at some point or another.

init.sqf:

true spawn {
   waitUntil {player == player};

   player addEventHandler ["Respawn", {
	_this execVM "fix.sqf"
   }];
};  

fix.sqf:

sleep 10;
removeBackpack player;
hint "test";

sorry for sort of derailing the thread

EDIT: did some research and apparently i'm dumb. sleep runs too long and doesn't take effect or something. thanks for the info guys

Edited by gr1D-

Share this post


Link to post
Share on other sites
Do you mind if I put together a little example mission showing how this works on Armaholic. I will also ask XxanimusxX. I think there is no other script out there like this and I am sure a lot of people can use it. Avibird

Sure, share freely, glad to help.

Thats what this forum is all about. :)

Share this post


Link to post
Share on other sites

@ Demonized It work great however after the last update it is not working on Respawn or AI units WTF bohemia one step forward with core content but two step backwards with her work ):

Share this post


Link to post
Share on other sites

not sure what was wrong but its working for me now, i changed it a bit so you dont need the onplayerrespawn.sqf, also it should be safe against multiple loops because of players joining, maybe that was the issue....

let me know if it works for you.

place this in init.sqf:

grantUnlimitedAmmo = compile preprocessFileLineNumbers "unlimitedAmmo.sqf";

[] spawn {
if (!isNil "UNLAMMO_ARR") exitWith {};
UNLAMMO_ARR = []; // empty global array for check, and to notify not to start a second loop on player connect if already running.
if !(isServer) exitWith {};  // exit if not server.
while {true} do {
	{
		if (!(_x in UNLAMMO_ARR) AND alive _x) then {
			[[_x, 1], "grantUnlimitedAmmo", _x] call BIS_fnc_MP;
			UNLAMMO_ARR = UNLAMMO_ARR + [_x];
		};
	} foreach allUnits;
	{
		if !(alive _x OR isNull _x) then {UNLAMMO_ARR = UNLAMMO_ARR - [_x]};
	} foreach UNLAMMO_ARR;
	sleep 1;
};
};

place the script named UnlimitedAmmo.sqf in your mission folder, good to go, at least in SP and hosted MP, not checked MP dedi, but think it will be ok there to.

//////////////////////////////////////////////////////////////////
// Function file for Armed Assault
// Created by: TODO: Author Name
//////////////////////////////////////////////////////////////////

//Unlimited Ammo Script by XxanimusxX
_unit = _this select 0;
_ammoCount = _this select 1;
_primeWpn = "";
_prevWpn = "";
_magazineType = "";
_foundMags = 0;
hint format["%1 is added",_unit];
diag_log format["%1 is added",_unit];

while {alive _unit} do {
   waitUntil {sleep 0.5; currentWeapon _unit != ""};
   _primeWpn = currentWeapon _unit;

   if (_primeWpn != _prevWpn) then
   {
       _prevWpn = _primeWpn;
       _magazineType = getArray(configFile >> "cfgWeapons" >> _primeWpn >> "magazines") select 0;
   };

   _foundMags = 0;
   {
       if (_x == _magazineType) then
       {
           _foundMags = _foundMags + 1;
       };
   } forEach (magazines _unit);

   if (_foundMags < _ammoCount) then
   {
       for "_i" from 1 to (_ammoCount - _foundMags) do
       {
           _unit addMagazine _magazineType;
       };
   };

   sleep 1;
};

dont use this part of your code, only what i described above will give all player and AI spawned or respawned or preplaced throughout the entire mission unlimited ammo.

// DO NOT USE //
[player, 2] spawn grantUnlimitedAmmo;  //controls player ammo
{                                     
   if (!(isPlayer _x)) then 
   { 
       [_x, 2] spawn grantUnlimitedAmmo;  //controls AI ammo
   }; 
} foreach allUnits; 
//Unlimited Ammo*****************************************************
//  DO NOT USE //

Share this post


Link to post
Share on other sites

@ Demonized WTF lol it could be me or just Bohemia with the game update. I have been working on a project using a lot of community scripts and MCC MOD. Both of the codes work fine in a test mission of just the codes. The first one and now your updated one however does not work in my current mission project ): There must be a issues with one of the other scripts in the project. I am going step by step to rebuild the mission to find out why it does not work. It was working up to yesterday until I started fooling around with standalone GAIA script which I was running into some errors with the whole CACHE SECTION of the script. I removed it but still the old code and your new code does not work in the mission project. So starts the rebuild lol. Thank you ones again for the help brother. Avibird.

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  

×