Jump to content
Sign in to follow this  
ADuke

Script to lock/unlock vehicle

Recommended Posts

Hello,

I am on the lookout for a script to execute on a vehicle (via addaction) that would allow the player to unlock the vehicle, then when the crew count of the vehicle is equal to 0, the vehicle locks and the action to lock the vehicle is added again.

Does anyone know of such a script that exists already?

or...

Does anyone know how to script such a thing?

I want something like this because I like to use UPSMON in missions but don't want my groups vehicles to be stolen.

Thanks for your help!

-AD

Share this post


Link to post
Share on other sites

OK, so after some experimenting I seem to have answered my own question.

For those of you wondering how to do something similar, and searching these forums like I did, here is the answer:

I placed a vehicle, Vehicle Lock set to "LOCKED"

The vehicle's init...

unlock = this addaction ["UNLOCK","vehicleunlock.sqf"]

vehicleunlock.sqf:

_obj = _this select 0;

_obj setvehiclelock "unlocked";

_obj removeaction unlock;

lock = _obj addaction ["LOCK", "vehiclelock.sqf"];

exit;

vehiclelock.sqf:

_obj = _this select 0;

_obj setvehiclelock "locked";

_obj removeaction lock;

unlock = _obj addaction ["UNLOCK", "vehicleunlock.sqf"];

exit;

Seems to work great so far.

Share this post


Link to post
Share on other sites

This is very simialr to what i am looking for, but would like the addaction to be applied to an object thats not with the vehicle, and also that I can use more than one addaction on this object to lock and unlock named vehicles. Any Idea how to change it for that purpose?

Edited by Tay-uk

Share this post


Link to post
Share on other sites

Here is a working solution for players, servers and dedicated servers

Add the following to your init.sqf:

box_fnc_locks=compile preprocessFileLineNumbers "init_locks.sqf";
call box_fnc_locks;

Add the following to vehiclelock.sqf

box_lock_command=[_this select 0,"locked"];
publicVariable "box_lock_command";

Add the following to vehicleunlock.sqf

box_lock_command=[_this select 0,"unlocked"];
publicVariable "box_lock_command";

Add the following to init_locks.sqf:

private ["_obj","_command"];
while {true} do
{
 if !isNil("box_lock_command") then
 {
   _obj=box_lock_command select 0;
   _command=box_lock_command select 1;
   if (local _obj) then
   {
       box_lock_command=nil;
       publicVariable "box_lock_command";
       _obj setvehiclelock _command;
   };
 };
 sleep 1;
};

Add the following to your locks.sqf:

0 = _this addaction ["<t color='#ffff00'>Unlock vehicle</t>","vehicleunlock.sqf",[],0,false,false,"","((locked _target) and (leader _this==_this))"];
0 = _this addaction ["<t color='#ffff00'>Lock vehicle</t>","vehiclelock.sqf",[],0,false,false,"","((!locked _target) and (leader _this==_this))"]; 

Then Add the following to the Initialization field of the vehicle(s) you want to be able to Lock/Unlock by walking up to it and use the mouse menu:

0 = this execVM "locks.sqf"

Important: Only Group Leaders can Lock/Unlock vehicles in this version.

Edited by Boxter.dk
Use execVM for the vehicles Initialization field

Share this post


Link to post
Share on other sites

thanks Aduke (and Boxter.dk)! this is very useful, and comes in handy! btw good to see you back around ADuke!

Share this post


Link to post
Share on other sites

Then Add the following to the Initialize field of the vehicle(s) you want to be able to Lock/Unlock by walking up to it and use the mouse menu:

this exec "locks.sqf"

Important: Only Group Leaders can Lock/Unlock vehicles in this version.

Very neat. However this one should be with

execVM

If you get an error message from adding that to the init line, try something like:

nul = [this] execVM "locks.sqf"

Share this post


Link to post
Share on other sites
Very neat. However this one should be with
execVM

If you get an error message from adding that to the init line, try something like:

nul = [this] execVM "locks.sqf"

You are absolutely right. Exec is for sqs. But the example worked fine since the content of the locks.sqf file is both sqs and sqf syntax. ;)

I have edited the example to use execVM.

Edited by Boxter.dk

Share this post


Link to post
Share on other sites

Hello! I have some problems with this script because it only works properly only in host mode, but on a dedicated server no longer works, ie works partly, I must first unlock the vehicle as admin and enter as a driver, only then can I lock / unlock the vehicle, I've added to your script detection UID and now I do not know how to eliminate the error, so that work properly

init.sqf

...
UIDarray=["uid1","uid2","etc"];
...
box_fnc_locks=compile preprocessFileLineNumbers "init_locks.sqf";
call box_fnc_locks;

mission.sqm

...
init="0 = this execVM ""locks.sqf"";";
...
//added to any vehicle that wants to lock / unlock

locks.sqf

WaitUntil{not isNull player};
waitUntil {(getPlayerUID player) != ""};
_uid 	= getPlayerUID player;


if (_uid in UIDArray) then
{
0 = _this addaction ["<t color='#ffff00'>Unlock vehicle</t>","vehicleunlock.sqf",[],0,false,false,"","(locked _target)"];
0 = _this addaction ["<t color='#ffff00'>Lock vehicle</t>","vehiclelock.sqf",[],0,false,false,"","(!locked _target)"];
} else {};

the rest of the files (vehiclelock.sqf, vehicleunlock.sqf) no change

CAN YOU HELP ME?

Share this post


Link to post
Share on other sites

Can the distance that you need to be away from the vehicle be reduced before the add action shows ?

Share this post


Link to post
Share on other sites

unlock / lock properly displayed on each vehicle, but after selecting the option from scrollmenu, there is no response to the command

Share this post


Link to post
Share on other sites
Here is a working solution for players, servers and dedicated servers

Add the following to your init.sqf:

box_fnc_locks=compile preprocessFileLineNumbers "init_locks.sqf";
call box_fnc_locks;

Add the following to vehiclelock.sqf

box_lock_command=[_this select 0,"locked"];
publicVariable "box_lock_command";

Add the following to vehicleunlock.sqf

box_lock_command=[_this select 0,"unlocked"];
publicVariable "box_lock_command";

Add the following to init_locks.sqf:

private ["_obj","_command"];
while {true} do
{
 if !isNil("box_lock_command") then
 {
   _obj=box_lock_command select 0;
   _command=box_lock_command select 1;
   if (local _obj) then
   {
       box_lock_command=nil;
       publicVariable "box_lock_command";
       _obj setvehiclelock _command;
   };
 };
 sleep 1;
};

Add the following to your locks.sqf:

0 = _this addaction ["<t color='#ffff00'>Unlock vehicle</t>","vehicleunlock.sqf",[],0,false,false,"","((locked _target) and (leader _this==_this))"];
0 = _this addaction ["<t color='#ffff00'>Lock vehicle</t>","vehiclelock.sqf",[],0,false,false,"","((!locked _target) and (leader _this==_this))"]; 

Then Add the following to the Initialization field of the vehicle(s) you want to be able to Lock/Unlock by walking up to it and use the mouse menu:

0 = this execVM "locks.sqf"

Important: Only Group Leaders can Lock/Unlock vehicles in this version.

How do I modify to make it so anyone can lock/unlock the vehicle?

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  

×