Unlock/Lock script

Unlock/Lock script

« posted: Jul 30, 2014, 11:40 PM »
I have tried some variants of unlocking and locking vehicles on my server with more or less acceptable result, the method i use right now is pretty crude and use in essence the script below but in two separate files which seem to work quite well in MP but i would like to have this in one script and i also ask so i can learn more and better ways.

However when i run this on my local computer as a MP game it works as intended but when i run it on a dedicated machine it doesn't really work, it seems that its only running this part(except for "_vehicle lock false;") of the script further down.

And every time i try to call the function again it only seems to run this bit over and over again(saying "unlocked" and flashing the lights and giving the sound.

Code: [Select]
else
{
_vehicle lock false;
//_vehicle lockCargo false;
_vehicle setVariable ["objectLocked", false, true];
_vehicle setVariable ["R3F_LOG_disabled",false,true];

_vehicle say3D "carlock";
player action ["lightOn", _vehicle];
sleep 0.5;
player action ["lightOff", _vehicle];
titleText ["Vehicle unlocked!","PLAIN DOWN"]; titleFadeOut 2;
};
};

This is the whole script.

Code: [Select]
private ["_vehicle","_lockState","_nearvehicle"];
_nearvehicle = nearestObjects [player, ["LandVehicle", "Ship", "Air"], 5];
_vehicle = _nearvehicle select 0;
_lockState = locked _vehicle;

if (_vehicle getVariable "ownerUID" == getPlayerUID player) then {
if(_lockState == 1) then
{
_vehicle lock true;
//_vehicle lockCargo true;
_vehicle setVariable ["objectLocked", true, true];
_vehicle setVariable ["R3F_LOG_disabled",true,true];

_vehicle say3D "carlock";
_vehicle engineOn false;
player action ["lightOn", _vehicle];
sleep 0.5;
player action ["lightOff", _vehicle];
titleText ["Vehicle Locked!","PLAIN DOWN"]; titleFadeOut 2;
}
else
{
_vehicle lock false;
//_vehicle lockCargo false;
_vehicle setVariable ["objectLocked", false, true];
_vehicle setVariable ["R3F_LOG_disabled",false,true];

_vehicle say3D "carlock";
player action ["lightOn", _vehicle];
sleep 0.5;
player action ["lightOff", _vehicle];
titleText ["Vehicle unlocked!","PLAIN DOWN"]; titleFadeOut 2;
};
};


if (_vehicle getVariable "ownerUID"!= getPlayerUID player) then {
if(_lockState == 1) then
{
_vehicle lock true;
//_vehicle lockCargo true;
_vehicle setVariable ["objectLocked", true, true];
_vehicle setVariable ["R3F_LOG_disabled",true,true];
_vehicle say3D "carlock";
_vehicle engineOn false;
player action ["lightOn", _vehicle];
sleep 0.5;
player action ["lightOff", _vehicle];
titleText ["Vehicle Locked!","PLAIN DOWN"]; titleFadeOut 2;

{
if (isPlayer _x) then
{
_x action ["eject", _vehicle];
};
} forEach (crew _vehicle);


}
else
{
titleText ["You are not the owner of this vehicle","PLAIN DOWN"]; titleFadeOut 5;
_vehicle say3D "caralarm";
player action ["lightOn", _vehicle];
sleep 0.5;
player action ["lightOff", _vehicle];
sleep 0.5;
player action ["lightOn", _vehicle];
sleep 0.5;
player action ["lightOff", _vehicle];
sleep 0.5;
player action ["lightOn", _vehicle];
sleep 0.5;
player action ["lightOff", _vehicle];
sleep 0.5;
player action ["lightOn", _vehicle];
sleep 0.5;
player action ["lightOff", _vehicle];
sleep 0.5;
};
};

called with
Code: [Select]
player addAction ["<img image='client\icons\r3f_unlock.paa'/>Unlock\Lock", "client\actions\unlockLock.sqf", [], 1, false, false, "", "count nearestObjects [player, ['LandVehicle', 'Ship', 'Air'], 5] > 0"],
As im really new to scripting i would appreciate if someone could tell me what im doing wrong here and why it works in an mp game on my own machine and not on a dedi.

Also if someone could show a better way of doing this in an Dedicated MP environment. Maybe switches are the way to go?

Re: Unlock/Lock script

« Reply #1 posted: Aug 09, 2014, 02:16 AM »
Tried with a switch type of scripts instead but no luck, it seems that i have to stick with the separate unlock and lock scripts which work just fine.

But now i added another script that uses a custom item (lockpick) and i need to figure out how to remove one (you can carry two). This script works fine as is but doesn't remove one lockpick set for me

Code: [Select]
#define ITEM_COUNT(ITEMID) ITEMID call mf_inventory_count
private ["_vehicle","_lockStatus","_nearvehicle"];

_nearvehicle = nearestObjects [player, ["LandVehicle", "Ship", "Air"], 5];
_vehicle = _nearvehicle select 0;
_lockStatus = locked _vehicle;



if (alive _vehicle && _vehicle getVariable "ownerUID" != getPlayerUID player && (ITEM_COUNT(MF_ITEMS_LOCKPICK) >= 1)) then
{
_vehicle lock false;
_vehicle setVariable ["objectLocked", false, true];
_vehicle setVariable ["R3F_LOG_disabled",false,true];
_vehicle say3D "carlock";
titleText ["Vehicle unlocked with lockpick!","PLAIN DOWN"]; titleFadeOut 2;
};

if (ITEM_COUNT(MF_ITEMS_LOCKPICK) <= 0) then
{
hint "You need lockpicks to perform this action";
};

i also tried using a "switch" type of control structure here as i want more checks and variants of effect. and it works if i do multiplayer -> new and start my mission from there but in a "real" dedicated eviroment it doesnt work =(. I get exactly the same result as the if/else locking script(which also works in mp on my own machine but not as "real dedicated".

I also would like to add a % chance of failure in there so i can lower the price on this kit (its 5000 now as it always unlocks). Any ideas?

Re: Unlock/Lock script

« Reply #2 posted: Aug 09, 2014, 03:29 AM »
I can't test it now but a simple

Quote
player removeItem "MF_ITEMS_LOCKPICK";

might work if the script is running in the context of the client.

for the percentage thing you can use the random function
20% chance:
Quote
20 > random 100

about the locality issues I can't really help you as this kind of problem - this stuff not well documented and hard to understand, for me a nightmare to troubleshoot but maybe someone more competent may help

i suppose you would have to run it on the server as the vehicle is not local on the client using the unlock script.
or something like that ...
  • Offline AgentRev
  • Developer
  • Veteran
  • ******
  • Posts: 2544

Re: Unlock/Lock script

« Reply #3 posted: Aug 10, 2014, 08:39 PM »
Script commands like removeItem only work with vanilla items, WL player items are custom entities handled with our own set of functions (ex: ["lockpick", 1] call mf_inventory_remove)

Plus, variables in all caps like MF_ITEMS_LOCKPICK are stuff that's only available in scripts from client\items since they are preprocessor defines that only work if the appropriate #define in at the top of that script, and the define script itself must be located in the same directory as the script that's calling it, hence why it's infinitely better to just use the static item ID across the mission ("water", "cannedfood", "repairkit", etc.)
  • Offline AgentRev
  • Developer
  • Veteran
  • ******
  • Posts: 2544

Re: Unlock/Lock script

« Reply #4 posted: Aug 10, 2014, 08:52 PM »
However when i run this on my local computer as a MP game it works as intended but when i run it on a dedicated machine it doesn't really work, it seems that its only running this part(except for "_vehicle lock false;") of the script further down.

If you look at the wiki page for "lock", it shows "Arguments local" at the top, which means that it has to be called where the vehicle is considered local. Also, use a number instead of a boolean, as mentioned by Druid in the wiki comments.

Here:

Code: [Select]
[[[netId _vehicle], {(objectFromNetId (_this select 0)) lock 2}], "BIS_fnc_spawn", _vehicle] call TPG_fnc_MP;

Re: Unlock/Lock script

« Reply #5 posted: Aug 11, 2014, 06:13 PM »
Thank you AgentRev and wiking, ill try to work on this some more =). I tried with numbers instead of boolean
but then i got almost no function at all, but i didn't use it like you suggested with
Code: [Select]
[[[netId _vehicle], {(objectFromNetId (_this select 0)) lock 2}], "BIS_fnc_spawn", _vehicle] call TPG_fnc_MP;  :D


Just so i got this, you mean i can replace  this
Code: [Select]
(ITEM_COUNT(MF_ITEMS_LOCKPICK)
with, in my case.
Code: [Select]
(ITEM_COUNT(lockpickset)
as i used your repairkit as a template just to get a custom item
Code: [Select]
MF_ITEMS_LOCKPICK = "lockpickset";
_lockpick = "client\actions\lockPick.sqf" call mf_compile;
_icon = "client\icons\r3f_lock.paa";
[MF_ITEMS_LOCKPICK, "Lockpick set", _lockpick, "Land_SuitCase_F",_icon,2] call mf_inventory_create;

BTW, i think i read somewhere that "locked" and maybe even "lock" is being reworked in the upcoming patches.

thanks again.
  • Offline lodac
  • Hardened
  • ****
  • Posts: 220
  • TOP Arma Server Admin

Re: Unlock/Lock script

« Reply #6 posted: Aug 12, 2014, 04:00 PM »
Great work!

Would it be easy to condition the unlock or lock addaction to only display what would be the next step?

If the vehicle is not locked, it should show lock and not both unlock and lock.

I tried a couple different configurations, but was never able to get it to work correctly.



TOPArma Mission - Share your mission on GitHub

Re: Unlock/Lock script

« Reply #7 posted: Aug 15, 2014, 11:28 PM »
Funny, i have been trying to get something like that to work but i cannot figure out how to evaluate the last two requirements, is it or is it not, "locked" and "ownerUID" and im trying to add this in in the playerActions.sqf together with the other actions that are there.

I realize that im using and executing the "cursorTarget getVariable" wrong somehow but i cannot figure out why and how i should do it, if even possible. any ideas? and can i get the vehicle types into an array? if possible i really like to keep the same formatting as the other actions.

Code: [Select]
["<img image='client\icons\r3f_unlock.paa'/>Unlock Vehicle", "client\actions\unlock.sqf", [], 1, false, false, "", "!isNull cursorTarget && cursorTarget isKindOf 'LandVehicle' || cursorTarget isKindOf 'Ship' || cursorTarget isKindOf 'Air' && player distance cursorTarget < 7 && cursorTarget getVariable 'locked' >= 2 && cursorTarget getVariable 'ownerUID' == getPlayerUID player"],
["<img image='client\icons\r3f_unlock.paa'/>Lock Vehicle", "client\actions\lock.sqf", [], 1, false, false, "", "!isNull cursorTarget && cursorTarget isKindOf 'LandVehicle' || cursorTarget isKindOf 'Ship' || cursorTarget isKindOf 'Air' && player distance cursorTarget < 7 && cursorTarget getVariable 'locked' != 0 && cursorTarget getVariable 'ownerUID' == getPlayerUID player"],
["<img image='client\icons\r3f_unlock.paa'/>Pick the vehicles lock", "client\actions\lockPick.sqf", [], 1, false, false, "", "!isNull cursorTarget && cursorTarget isKindOf 'LandVehicle' || cursorTarget isKindOf 'Ship' || cursorTarget isKindOf 'Air' && player distance cursorTarget < 7 && cursorTarget getVariable 'locked' >= 2 && cursorTarget getVariable 'ownerUID' != getPlayerUID player"],

With AgentRevs help i got the locking to work really nice and the lockpick script removed one lockpick by chance ~20% if you used it on a vehicle that belongs to someone else.

Unfortunately something goes wrong after a while and i don't know if it wan because i was using a "switch" control structure, anyway i decided i wanted to split the scripts for locking and unlocking so that might not be a problem in the future.

Maybe its better to add this to the vehicles instead of the players, less taxing on the server?