Jump to content
Vostok7798

Trouble looping through allPlayers array

Recommended Posts

Hey, all.

 

Just wrote this event handler script the other day, and I hope to eventually implement it into a multiplayer mission. Now, I want the event handler to assign to all players on the server, so I thought I'd just create a forEach loop with the allPlayers array. The problem is, when I make reference to the _z variable within the loop it seems to mess with the commands and produce all kinds of compiling errors.

 

As a bit of a run down, the event handler activates when the player fires their weapon. If there is a civilian in a vehicle, within 20 meters, who possesses a detonator, as well as an explosive device attached to his car, there will be a 75% chance they will detonate it. In addition, if there is a civilian driver within 50 meters, who merely carries a gun, he will be assigned to an enemy side and made enemy to the player.

 

Now, the obvious issue is, throughout the code, the variable "player" is used, and from what I understand, it is no recommended to use it in multiplayer, hence the "forEach allPlayers" loop. Now, I thought it was merely a matter of substituting the player variable with _z, which represents a player from the allPlayers array. However, like mentioned above, it seems to cause quite a long list of errors.

 

To be clear, the code as depicted below works flawlessly in the editor environment.

 

Here is the code:

{	// All Players
	_z = _x;
	_z addEventHandler["Fired",{
		_obTemp = nearestObjects [player, ["Car"], 50];
		_manTemp = nearestObjects [player, ["Man"], 50];
		{	// All vehicles in a 50 meter radius of the player
			_y = _x;
			_hasWeap = false;
			if (! isnull (driver _y)) then {
				if ("ACE_M26_Clacker" in items driver _y && count (attachedObjects _y) > 0) then {
					if ((random 100) > 25 && (player distance _y < 20)) then {
						[(attachedObjects _y select 0), 1] call ace_explosives_fnc_scriptedExplosive; 
					};
				};
			};
			{	// The occupants of the aforementioned vehicles
				_w = _x; 
				{	// The weapons of the aforementioned occupants
					if (_x != "" && side (driver _y) == civilian) then {_hasWeap = true};
					[east, "HQ"] sideChat format ["%1", _x != ""];
				} forEach weapons _w;
			} forEach crew _y;
			if (_hasWeap) then {
				_agGroup = createGroup (selectRandom [WEST, INDEPENDENT]);
				crew _y joinSilent _agGroup;
				{	// Members of the new hostile group
					_x enableAI "ALL";
				} forEach units _agGroup;
				_agGroup leaveVehicle _y;
			};
		} forEach _ObTemp;
		{	// All AI units in a 50 meter radius of the player
			_y = _x;
			_hasWeap = false;
			_unitAr = [];
			{	// The weapons of the aforementioned AI
				if (side _y == civilian && _x != "") then {_hasWeap = true};
			} forEach weapons _y;
			if (_hasWeap) then {
				_x enableAI "ALL";
				_unitAr = _unitAr + [_y]
			};
		} forEach _manTemp;				
		_agGroup = createGroup (selectRandom [WEST, INDEPENDENT]);
		_unitAr joinSilent _agGroup;
	}];
} forEach allPlayers;

Fair warning: I know nearly nothing about all of the multiplayer scripting quirks (and am only a beginner at programming in general), and have very little knowledge on what should be executed on solely the server, or what should be executed globally, or what should be executed by the clients, etc.. So, and pointers are more than welcome. at this point, I just need to know if I am barking up the right or wrong tree with how I am doing things.

Share this post


Link to post
Share on other sites

Why are you looping this through allPlayers? Why don't you just use player and put this in initPlayerLocal.sqf? The way you're currently doing this (I ASSUME this is in initServer.sqf) won't give the event handler to JIPs. Not to mention the fact that this method is more expensive than the one I just described -- Especially for something that runs on mission init. (Reason being for this is that you're offloading the eventHandler addition from doing [X] players all on the server to just having client machines run it once for themselves. Think of it this way: Many hands makes for light work.)

 

Also, I should mention -- With my method described above you don't need the forEach loop. You can just add the eventHandler to the player like normal.

Edited by Vauun
Forgot to mention forEach
  • Like 1

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

×