key lock/unlock vehicle

lakerlol

New Member
How to create lock/unlock vehicle?
My idea: If you want to become a master of the open car then you need to sit in it and wait 1 minute.
 
I will take a look at this and see if i can find all the files that it uses to move over into the Dayz_code folder in my mission file. Its probably about time i make a tutorial anyway since i have received so much help from other people. Give me a night, i will look into it.

*edit*

Not sure, but looking at this line of code here looks like you need to be the owner before you can lock it, and to be the owner i think you have to buy it from a vendor. Anyone else have an opinion about it?

Code:
// Allow Owner to lock and unlock vehicle
    if(_isVehicle and !_isMan and _canDo and _ownerID != "0") then {
         
        if (s_player_lockUnlock_crtl < 0) then {
 
            if(locked cursorTarget) then {
                if(_ownerID == dayz_playerUID) then {
                    _Unlock = player addAction [format["Unlock %1",_text], "\z\addons\dayz_code\actions\unlock_veh.sqf",cursorTarget, 2, true, true, "", ""];
                    s_player_lockunlock set [count s_player_lockunlock,_Unlock];
                    s_player_lockUnlock_crtl = 1;
                } else {
                    _Unlock = player addAction ["<t color='#ff0000'>Vehicle Locked</t>", "",cursorTarget, 2, true, true, "", ""];
                    s_player_lockunlock set [count s_player_lockunlock,_Unlock];
                    s_player_lockUnlock_crtl = 1;
                };
            } else {
                if(_ownerID == dayz_playerUID) then {
                    _lock = player addAction [format["Lock %1",_text], "\z\addons\dayz_code\actions\lock_veh.sqf",cursorTarget, 1, true, true, "", ""];
                    s_player_lockunlock set [count s_player_lockunlock,_lock];
                    s_player_lockUnlock_crtl = 1;
                };
            };
         
        };

The best solution i can think of to replace this is to use a new variable like _hasKey and make all your players spawn with 1 key that can be consumed to gain possesion of 1 vehicle that you choose. Otherwise hackers will just teleport around and lock every vehicle.
 
You will have to do it like this when I'm right:

Option a)
-do a custom Hive Query with CHILD:999 and add the possibility with a scroll menu to get the owner of the vehicle
-you have to write in the playerUID into the characterID field
possible troubles:
not every Hive has the custom CHILD:999 so not for everyone and everyone Mod/Serverbuild this will work

Option b)
-try to do it like in publishVehicle2.sqf at DayZEpoch mod, that will add a new vehicle where you can add the new owner -> delete the old one before you publish the new one (thats the way I try to edit code of buildables in BaseBuilding)
possible troubles:
will kill player if inside vehicle (expectation) and the vehicle will short disappear and then appear again maybe it shifts a little bit
 
So i either have to read/write from the database or delete all vehicles and remake them with owner id's? which will kill players inside their vehicles... nice :/
 
If you want to that the owner status to be permanent you always have to write something to the database. If you won't everything will be lost after restart.

If you want it only till next restart you could set owner status via setVariable and look for it with getVariable. Should work too

Not delete all vehicles, delete just the vehicle you are standing in front of. If the player sit in the vehicle he would be killed yes. So you have to forbid this in the script. Add something like this to the fn_selfactions

if (!_is_in_vehicle) then
 
Nope that script is only to publish a Vehicle. The delete function is another. Also you would have to write a new script to set everything right up.

If I'm ready with my other projects and this isn't made yet I will maybe give it a try.
 
It would be quite easy to accomplish using a while {true} loop on every player that checks for vehicles within 10 m and changes player actions to add a lock unlock thing - like you said, kikyou2 using setVariable and getVariable. No need to delete/create a vehicle at least.

Of course over server restart requires hivewrite, there is another thread where they are trying to use child 999. This will be a pain, most likely, and yes it will only function with the same or similar hive extensions.

If you add this, you should probably give an option to break in to the car (if the windows are broken, lock doesn't work?) or something similar to that - maybe have a chance to break the car when 'hotwiring'.

Final thought - if you went the full route and added in game 'keys' items - you could even have the keys for a particular car spawn on a zombie nearby, and could have all vehicles be locked when they spawn in - so the players have to hunt zeds in order to get the keys. This is more for a full addon though (cfgMagazines and cfgLoot probably would have to be altered)...and I'm rambling now.
 
mmmyum could you explain the {true} loop a little more for me? I would love to code this myself (or at least give it a try). my coding skills are decent, however seems how i only started teaching myself sqf about 3 weeks ago i still have a lot to learn. Though my background in other coding languages helps a little lol!

I don't want to go through the whole create / delete process. I'd rather go a route like get/set variable. However i want to find a way to make it a little more permanent than a database write if that makes sense. I do like the idea of keys though i don't believe i'd have the knowledge to implement something that advanced. im at more of a duck tape rig it in kinda stage lol.

The get/set variable route wouldn't be too hard simply have a self action that locks, one that unlocks, and one that claims the vehicle (sets you as it's owner [via set/getvariable] until server restart). Simple enough i've even already coded this just not tested or implemented for the fact that it's pointless because if i can't get on for a few hours after a restart then someone can still steal my car even though i locked it before i got off the night before! Once again i want to try and achieve this without database writing. MAYBE (if possible not sure) make another script that checks if a vehicle is owned BEFORE the restart and relocks it after? Sounds good but as i think about it after saying it doesn't really make sense without the database write :/ Going to finish writing the script such that players can unlock/lock and claim up to server restart and then go from there i guess.
 
Actually the database read / write would be fine if we can figure out a database read/write line(s) of code that work for all hives.
 
Go from there. Add me to steam if you like (mmmyum) maybe I can help you if you are going to actually write it (not really interested in locking vehicles myself).

There is no way to have persistence over server reboot (cars staying locked after reboot) without writing to the DB. That's the whole point of the DB.

I am interested in eventually getting a custom table in my DB to write vars to (would be great for what cramps and I just wrote for depleting fuel tanks). Find the threads about child 999 and hive write on opendayz and read through them - it should definitely be doable at least.

while true: exec for all players (from init.sqf in if (!isDedicated))

while {true} do {
_count = nearestObjects //figure out the right syntax here, to check for nearby vehicles
if (_count > 0) then {
_obj = _count select 0;
_isOwned = _obj getVariable ["Owned",false];
if (!_isOwned) then {
//add player action to own
} else {
_isOwner = (_obj getVariable["Owner",0000] == playeruid)//player uid or whatever
if (_isOwner) then {
_isLocked = _obj getVariable["Locked",false]
//blah blah more ifs to determine if locked already, add action to unlock, if unlocked, add enter action
} else {
//remove all player actions for vehicles so player can't get in or change owner or anything
};
};
sleep 2; or whatever.

Basically make it loop on every player and change the player actions based on conditions, setting variables by executing the actions that are added

Anyways, I'm rambling again, that's a basic framework for the code and while loop.
 
Back
Top