Jump to content
wyattwic

Restricting vehicle access by rank in EH

Recommended Posts

Hello!

 

I seem to be having issues getting the below code to work, and I am somewhat stumped.  I have attempted three different iterations of the below without luck.  No errors or effect are being produced.

 

The below EH is added to a vehicle from its respawn script.   The code's goal is to eject players who board an empty vehicle at base with insufficient rank.

 

Any help would be greatly appreciated!

//Notes:  This code checks to see if its in base, then checks if there is already someone in side, then the persons rank.  The first one to be OK ends the check.  Failing the rank check is the only thing that will eject you.
_eh = _veh addeventhandler ["GetIn", {
	if !((_this select 0) in list base) then { //If not in trigger "base".
		if (count crew (_this select 0) == 0) then { //If there is no one else in there
			//Get the vehicle's required rank.  Stored as a number.
			_reqlevel = (_this select 0) getvariable "VehReqRank";
			// and get the player's rank (and convert to a number).
			_rank = switch (rank (_this select 2)) do {
				case "PRIVATE" : {0}; 
				case "CORPORAL" : {1}; 
				case "SERGEANT" : {2}; 
				case "LIEUTENANT" : {3}; 
				case "CAPTAIN" : {4}; 
				case "MAJOR" : {5}; 
				case "COLONEL" : {6}; 
			};
			//If the players rank is lower than the required, eject him.
			if (_rank < _reqlevel) then {
				(_this select 2) action ["EngineOff",(_this select 0)];
				(_this select 2) action ["Eject",(_this select 0)];
				"You have insufficent rank to access this vehicle." remoteexec ["hint",owner (_this select 2)];
			};
		};
	};
}];

 

 

Share this post


Link to post
Share on other sites

Hi

the problem seems to be that when getIn event handler is triggered the crew count is at least one.

 

so change it:

from:

if (count crew (_this select 0) == 0) then {

to:

if (count crew (_this select 0) <= 1) then {

 

also there is rankid command so you can just do this:

 

_rank = (rankid (_this select 2));

(instead of switch)

  • Like 1

Share this post


Link to post
Share on other sites

Yesterday i get familiar with lazy evaluation. Your case is an good example for using it:


 

_eh = _veh addeventhandler ["GetIn", {
    params [
        ["_vehicle",objNull,[objNull]],
        ["_sit","",[""]],
        ["_unit",objNull,[objNull]]
    ];
    _reqlevel = _vehicle getvariable "VehReqRank";
    _rank = rankid _unit;

    if (!(_vehicle in (list base)) && {_sit == "driver"} && {_rank < _reqlevel}) then {

        [_unit,["getOut",_vehicle]] remoteExecCall ["action",_unit];
        "You have insufficent rank to access this vehicle." remoteExec ["hint",_unit];
    };
}];

If the vehicle is in base trigger list (first condition false) its just skip other conditions. The other conditions are evaluated only if first is true.
 

The eject action looks to be broken, if the sit occupied is pilot or driver.

 

  • Like 1

Share this post


Link to post
Share on other sites

Thanks GC and David!

 

I had no clue rankID or that version of the params command existed. No clue how I missed those (thats what I get for loitering in A2 for so long, lol).

 

I had to modify your code a bit in order for it to work the way I wanted it to however I still have a minor issue.

 

The below works perfectly, however I am having a hard time taking a rankID back into a rank to use in the hint.

 

Right now _reqlevel has the rankID the vehicle is restricted to.  When ran through a switch do, or that series of IF statements, I always end up with an empty variable.  Any ideas?

 

			_veh addeventhandler ["GetIn", {
		
				params ["_vehicle","_sit","_unit"];
		
				_reqlevel = _vehicle getvariable "VehReqRank";
				_rank = rankid _unit;

				//if the vehicle is at base		and there isn't someone else in already		check to see if he has enough rank
				if ((_vehicle in (list base)) && (({isplayer _x} count crew _vehicle) == 1) && (_rank < _reqlevel)) then {
					[_unit,["engineOff",_vehicle]] remoteExecCall ["action",_unit];
					[_unit,["getOut",_vehicle]] remoteExecCall ["action",_unit];
					
					_reqlevel = _vehicle getvariable "VehReqRank";
					if (_reqlevel == 0) then {_reqtext = "private";};
					if (_reqlevel == 1) then {_reqtext = "corporal";};
					if (_reqlevel == 2) then {_reqtext = "sergeant";};
					if (_reqlevel == 3) then {_reqtext = "lieutenant";};
					if (_reqlevel == 4) then {_reqtext = "captain";};
					if (_reqlevel == 5) then {_reqtext = "major";};
					if (_reqlevel == 6) then {_reqtext = "colonel";};
					
					_message = format ["A rank of %1 is required to use this vehicle. \n You are a %2.",_reqtext,tolower rank _unit];
					_message remoteExec ["hint",_unit];
				};
			}];

 

Share this post


Link to post
Share on other sites

@wyattwic

you must define _reqtext somewhere before the if statements.

 

Share this post


Link to post
Share on other sites

And you not need to remoteexec  "turn engine of" getout action do this automatically.

Share this post


Link to post
Share on other sites
3 hours ago, davidoss said:

And you not need to remoteexec  "turn engine of" getout action do this automatically.

Funny enough, if the gunner on a tank moves the turret, the engine turns on.  Only when the driver runs "getout" does the engine shut off.

 

 

3 hours ago, gc8 said:

@wyattwic

you must define _reqtext somewhere before the if statements.

 

Thanks!  That fixed it!

 

 

Here is the working code for anyone else who may need it.

 

_veh setvariable ["VehReqRank",_level];

			_veh addeventhandler ["GetIn", {
		
				params ["_vehicle","_sit","_unit"];
		
				_reqlevel = _vehicle getvariable "VehReqRank";
				_rank = rankid _unit;

				//if the vehicle is at base		and there isn't someone else in already		check to see if he has enough rank
				if ((_vehicle in (list base)) && (({isplayer _x} count crew _vehicle) == 1) && (_rank < _reqlevel)) then {
					[_unit,["engineOff",_vehicle]] remoteExecCall ["action",_unit];
					[_unit,["getOut",_vehicle]] remoteExecCall ["action",_unit];
					
					_reqtext = "";
					if (_reqlevel == 0) then {_reqtext = "private";};
					if (_reqlevel == 1) then {_reqtext = "corporal";};
					if (_reqlevel == 2) then {_reqtext = "sergeant";};
					if (_reqlevel == 3) then {_reqtext = "lieutenant";};
					if (_reqlevel == 4) then {_reqtext = "captain";};
					if (_reqlevel == 5) then {_reqtext = "major";};
					if (_reqlevel == 6) then {_reqtext = "colonel";};
					
					_message = format ["A rank of %1 is required to use this vehicle. \n You are a %2.",_reqtext,tolower rank _unit];
					_message remoteExec ["hint",_unit];
				};
			}];

 

  • Thanks 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

×