Jump to content
  • 0

Looking for feedback on a script


C.Peck

Question

So I'm relatively new to arma scripting, about a month in. I feel like I'm starting to get a pretty good grasp on everything, I'm looking for some feedback/improvements to a script I've been working on for a day or two. The idea is to allow players to remotely open or close garage doors from vehicles. I was already running Zupa's door management, so I modified some of that script to also look for keys in the player inventory when generating the nearby player list, which allows you to add the keys just like you would a player. Then to the addaction, I set it up to check doors within 75meters and if both the player is friendly to the door and he has a key that is friendly to the door, it will give them the action to open or close the door. As is,  the script works fine, as far as I've tested they're aren't any issues with it. Being newer to the scene I wanted to ask for some help and see if I broke any unwritten rules or did things that would put unnecessary load on my server. Is it a bad idea to use nested foreach statements inside of fn_selfactions? is there a cleaner more efficient way to do all these checks?

 

Possible update I was thinking about but wasn't quite sure how to do it, make it so for every key you have in your inventory that is friend'y to a door it would add a different action for that door with the name of the key in the action, so if you had 3 keys all assigned to different doors you could chose which door to open. The way I have it now It would open all 3 at once.

 

I tried to explain my logic the best I could.

 

playerkeys is defined earlier in my selfactions  - playerkeys = playerkeys + [[_ownerKeyIdtemp,_ownerKeyName]];

//remote door open
if (_inVehicle && driver _vehicle == player) then { //if in vehicle and driver
	systemchat "driver of vehicle"; //debug message
	if (_vehicle isKindOf "Land") then { //only works for land vehicles
		if((count playerkeys)>0) then { //No point in running the rest of the script unless the player has atleast 1 key
			systemchat "key detected"; //debug message
			_nearDoors = []; //reset array
			_nearDoors = nearestObjects[_vehicle, DZE_DoorsLocked, 75]; //find all locked doors within 75 meters 
			if((count _nearDoors)>0) then { // if a locked door is detected
				systemchat "door detected"; //debug message
				friendlyDoors = [];  //reset friendlydoor array
				{													//start foreach neardoors
					if ((_x animationPhase "Open_latch" == 0) or (_x animationPhase "Open_latch" == 1)) then { // make sure door isn't in the process of opening or closing
						_allowed = [];
						_tempneardoor = _x;
						_doorownerID = _x getVariable ["ownerPUID","-1"];	//code straight from zupas script to retrieve friends of doors
						_doorfriends = _x getVariable ["doorfriends",[]];			
						{														
							_friendUID = _x select 0;
							_allowed  =  _allowed  + [_friendUID];
						} forEach _doorfriends;
						_allowed = _allowed + [_doorownerID];
						if ((getPlayerUID player) in _allowed ) then {		//if player is friendly to door, check keys
							{
								_tempkey = _x select 0;
								if (_tempkey in _allowed) then {	//if keyname is friendly to door
						
									if (_tempneardoor animationPhase "Open_latch" == 0) then { //if door is locked
									doorState = "locked";
									}
									else {
										doorState = "unlocked";
									};					
									friendlyDoors = friendlyDoors + [[_tempneardoor,doorState]];			//add door + status of door to friendly list						
								};
							} foreach playerkeys;
						};	
					};
				} foreach _nearDoors;
				doorOpen = false;	//default to open doors
				if ((count friendlyDoors) > 0) then {		//if friendly doors were detected
					{
						if ((_x select 1) == "unlocked") then {		//Detects if any of the friendly doors are unlocked, if any of them are, default to remote close doors.
						doorOpen = true;
						};
					} foreach friendlyDoors;
					if (s_player_remoteDoor_ctrl<0) then {  //if an action hasn't been added yet
						myVehicle = _vehicle;		//save current vehicle to gloabl variable for remove action later
						if (doorOpen) then {
							_RemoteClose = myVehicle addAction ["Remote Close Door(s)", "doorManagement\remotecontrol.sqf",doorOpen,5,false,true,"",""];
							s_player_remoteDoor set [count s_player_remoteDoor,_RemoteClose];		//copied from lock/unlock vehicle from inside script
							s_player_remoteDoor_ctrl = 1;
						}
						else {
							_RemoteOpen = myVehicle addAction ["Remote Open Door(s)", "doorManagement\remotecontrol.sqf",doorOpen,5,false,true,"",""];
							s_player_remoteDoor set [count s_player_remoteDoor,_RemoteOpen];
							s_player_remoteDoor_ctrl = 1;
						};		
					};
				}
				else { //if no friendlydoors remove all actions
					{myVehicle removeAction _x} count s_player_remoteDoor;s_player_remoteDoor = [];
					s_player_remoteDoor_ctrl = -1;
				};
			}	
			else { //if no near doors remove all actions
				{myVehicle removeAction _x} count s_player_remoteDoor;s_player_remoteDoor = [];
				s_player_remoteDoor_ctrl = -1;
			};
		}
		else { //if player has no key remove all actions
			{myVehicle removeAction _x} count s_player_remoteDoor;s_player_remoteDoor = [];
			s_player_remoteDoor_ctrl = -1;
		};
	};
}
else { //if not in vehicle and driver remove all actions
	{myVehicle removeAction _x} count s_player_remoteDoor;s_player_remoteDoor = [];
	s_player_remoteDoor_ctrl = -1;
};

This is the remotecontrol.sqf

_doorOpen = _this select 3;

{
	if(_doorOpen) then {
		cutText ["Closing and Locking Door.", "PLAIN DOWN"];
		(_x select 0) animate ["Open_door", 0];
		(_x select 0) animate ["Open_latch", 0];
	}
	else {
		cutText ["Unlocking and Opening Door.", "PLAIN DOWN"];		
		(_x select 0) animate ["Open_latch", 1];
		(_x select 0) animate ["Open_door", 1];
		
	};
}foreach friendlyDoors;

{myVehicle removeAction _x} count s_player_remoteDoor;s_player_remoteDoor = [];
s_player_remoteDoor_ctrl = -1;

I was having some issues with undefined variables when using local variables so I may have some global that don't need to be, I guess I don't fully understand variables and how to make local ones persistent through an entire script.

Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
  • Advertisement
  • Discord

×
×
  • Create New...