Jump to content

[Release] Lock/Unlock Vehicles from Inside. Key and Non-Key versions


Jossy

Recommended Posts

This is just a quick script to a follow up request, no ongoing support really, though it shouldn't need much..

 

Setup Instructions

 

You will need custom fn_selfActions.sqf and variables.sqf files for this, if you don't already have these then you will need to look up how to setup a custom compiles.sqf.

 

1. Save this code into a new file under your mission folder, preferably custom\vehicle_lockUnlock.sqf. (if you save it to a different folder be sure to rename the paths in step 4.)

private ["_action","_vehicle","_lock","_keyName"];

_action = _this select 3;
_vehicle = _action select 0;
_lock = _action select 1;
if (count _action > 2) then { _keyName = _action select 2; };
{_vehicle removeAction _x} forEach s_player_incarlockunlock;s_player_incarlockunlock = [];
s_player_inCarlockUnlock_crtl = -1;

if (_lock) then {
	_vehicle setVehicleLock "LOCKED";
	PVDZE_veh_Lock = [_vehicle,true];
	publicVariable "PVDZE_veh_Lock";
	if (_keyName != "") then { cutText [ format ["%1 key used to lock vehicle", _keyName], "PLAIN DOWN"]; } else
	{ cutText [ format ["You locked the vehicle", _keyName], "PLAIN DOWN"]; };
} else {
	_vehicle setVehicleLock "UNLOCKED";
	PVDZE_veh_Lock = [_vehicle,false];
	publicVariable "PVDZE_veh_Lock";
	if (_keyName != "") then { cutText [ format ["%1 key used to unlock vehicle", _keyName], "PLAIN DOWN"]; } else
	{ cutText [ format ["You unlocked the vehicle"], "PLAIN DOWN"]; };
};

s_player_inCarlockUnlock_crtl = -1;

2. Paste this in the dayz_resetSelfActions section of your custom variables.sqf file at roughly line 254.

	s_player_inCarlockUnlock_crtl = -1;
	s_player_incarlockunlock = [];

3. Also paste this in your custom variables.sqf file at roughly line 757

	dayz_myLockedVehicle = objNull;

4a. This part is for Locking/Unlocking vehicles with no key requirement, paste this code into your fn_selfActions.sqf file at roughly line 14.

// Lock car doors with no key
if (_inVehicle && driver _vehicle == player) then {
	if (s_player_inCarlockUnlock_crtl < 0) then {
		dayz_myLockedVehicle = _vehicle;
		_text = getText (configFile >> "CfgVehicles" >> typeOf(dayz_myLockedVehicle) >> "displayName");
		cutText [ format ["vehicle: %1", _text], "PLAIN DOWN"];
		if(locked dayz_myLockedVehicle) then {
			_Unlock = dayz_myLockedVehicle addAction [format[localize "STR_EPOCH_ACTIONS_UNLOCK",_text], "custom\vehicle_lockUnlock.sqf", [dayz_myLockedVehicle, false], 2, true, true, "", ""];
			s_player_incarlockunlock set [count s_player_incarlockunlock,_Unlock];
			s_player_inCarlockUnlock_crtl = 1;
		} else {
			_lock = dayz_myLockedVehicle addAction [format[localize "STR_EPOCH_ACTIONS_LOCK",_text], "custom\vehicle_lockUnlock.sqf", [dayz_myLockedVehicle, true], 1, false, true, "", ""];
			s_player_incarlockunlock set [count s_player_incarlockunlock,_lock];
			s_player_inCarlockUnlock_crtl = 1;
		};
	};
} else {
	{dayz_myLockedVehicle removeAction _x} count s_player_incarlockunlock;s_player_incarlockunlock = [];
	s_player_inCarlockUnlock_crtl = -1;
}; 

4b. This option is for having a matching key requirement and is slightly different, paste this section into your fn_selfActions.sqf anywhere after _inVehicle = (_vehicle != player); roughly line 14.

// Lock car doors with key
private ["_carID","_hasKeyinCar"];
if (_inVehicle && driver _vehicle == player) then {
	_itemsPlayer = items player;
	_temp_keys = [];
	_temp_keys_names = [];
	// find available keys
	_key_colors = ["ItemKeyYellow","ItemKeyBlue","ItemKeyRed","ItemKeyGreen","ItemKeyBlack"];
	{
		if (configName(inheritsFrom(configFile >> "CfgWeapons" >> _x)) in _key_colors) then {
			_ownerKeyId = getNumber(configFile >> "CfgWeapons" >> _x >> "keyid");
			_ownerKeyName = getText(configFile >> "CfgWeapons" >> _x >> "displayName");
			_temp_keys_names set [_ownerKeyId,_ownerKeyName];
			_temp_keys set [count _temp_keys,str(_ownerKeyId)];
		};
	} count _itemsPlayer;
	_carID = _vehicle getVariable ["CharacterID", "0"];
	_hasKeyinCar = _carID in _temp_keys;
	if (s_player_inCarlockUnlock_crtl < 0 && _hasKeyinCar) then {
		dayz_myLockedVehicle = _vehicle;
		_text = getText (configFile >> "CfgVehicles" >> typeOf(dayz_myLockedVehicle) >> "displayName");
		if(locked dayz_myLockedVehicle) then {
			_Unlock = dayz_myLockedVehicle addAction [format[localize "STR_EPOCH_ACTIONS_UNLOCK",_text], "custom\vehicle_lockUnlock.sqf",[dayz_myLockedVehicle, false, (_temp_keys_names select (parseNumber _carID))], 2, true, true, "", ""];
			s_player_incarlockunlock set [count s_player_incarlockunlock,_Unlock];
			s_player_inCarlockUnlock_crtl = 1;
		} else {
			_lock = dayz_myLockedVehicle addAction [format[localize "STR_EPOCH_ACTIONS_LOCK",_text], "custom\vehicle_lockUnlock.sqf",[dayz_myLockedVehicle, true, (_temp_keys_names select (parseNumber _carID))], 1, false, true, "", ""];
			s_player_incarlockunlock set [count s_player_incarlockunlock,_lock];
			s_player_inCarlockUnlock_crtl = 1;
		};
	};
} else {
	{dayz_myLockedVehicle removeAction _x} count s_player_incarlockunlock;s_player_incarlockunlock = [];
	s_player_inCarlockUnlock_crtl = -1;
};
Link to comment
Share on other sites

  • 5 weeks later...

That's amazing! I am assuming you can unlock the vehicle from within as well? This will help greatly with the trader lag and players getting into vehicles before they lock! Thank you

You can't get out once it's locked though if that's what you're thinking, it's more to stop people trolling you and getting into your vehicle while you're still

Link to comment
Share on other sites

Any config for Infistar???

 

b_560_95_1.png

 

edit

I try config on AHconfig.sqf

 

add in line

_dayzActions =  

 

 "s_player_inCarlockUnlock_crtl","s_player_incarlockunlock","dayz_myLockedVehicle"

 

im not sure is this needed, but for me works, nobody banned or kicked out

 

Is it possible to add something like, if player dont have key, he cant use driver sit?

Link to comment
Share on other sites

  • 1 month later...

Ok I see. Do you have a solution to lock again the vehicle ?

 When your sql unlocks the car it sets the char ID to 0 thus removing it ability to use a key. What you can do, if you have the key and know what the variables for that key mean (ie. color letters & numbers) as they all correspond to the char ID. You can find that vehicle in the data base and edit the char ID.(Im not positive how keys work but use a lock box as a reference .. for the code red42 the char ID will be 10042 the 100 is red and 42 is well... duh! and with a bit of testing im sure could figure it out.) OR (easier way) with infistar just find a locked vehicle in the DB and use its char ID for a reference. make a new char ID using the same format (obviously one that does not exist) then use infistar's generate key function.

 

However, most people that have left a vehicle abandoned for 2 or more weeks usually have no desire to repossess it and most likely have forgotten of its existence! 

Link to comment
Share on other sites

  • 1 month later...

@oSoDirty, little nugget of wisdom on keys for you :)

 

To find the corresponding character id for a key you can use this piece of code-

_keyid = getNumber(configFile/'cfgWeapons'/_thekey/'keyid');

Where _thekey is the class name of the target key.

And to find the corresponding key to a vehicle you can use this-

_vid = parseNumber (cursorTarget getVariable ['CharacterID','0']);
if ((_vid > 0) && (_vid <= 2500)) then {_key = format ['ItemKeyGreen%1',_vid];};
if ((_vid > 2500) && (_vid <= 5000)) then {_key = format ['ItemKeyRed%1',(_vid - 2500)];};
if ((_vid > 5000) && (_vid <= 7500)) then {_key = format ['ItemKeyBlue%1',(_vid - 5000)];};
if ((_vid > 7500) && (_vid <= 10000)) then {_key = format ['ItemKeyYellow%1',(_vid - 7500)];};
if ((_vid > 10000) && (_vid <= 12500)) then {_key = format ['ItemKeyBlack%1',(_vid - 10000)];};

That one is pulled straight from something I'm making and checks the cursorTarget but that's easy to change.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
  • Discord

×
×
  • Create New...