Jump to content
Sign in to follow this  
ringoray

Init on respawning vehicle

Recommended Posts

Hey guys I am trying to get a script to run on a re-spawning vehicle, to keep the vehicle only accessible to certain players using the play GUID. I am using the Tophe simple vehicle re-spawn script for re-spawning the vehicle. The script I am using to lock the vehicle is this.

_vehicle = _this select 0;  // i added this line.

_vehicle addEventHandler ["GetIn", 
{
private ["_veh", "_pos", "_unit", "_namesArray"];
_veh = _this select 0;
_pos = _this select 1;
_unit = _this select 2;
_namesArray = ["76561198015510613"];

if !(name _unit in _namesArray) then
{
_unit action ["Eject", vehicle _unit];
};
}];  

This vehiclelock.sqf works great, but how do I get it to execute again when the vehicle is destroyed and re-spawns again?

Here is the lines I have in the vehicles Init box.

veh = [this] execVM "vehicle.sqf"; _null = [this] execVM "vehiclelock.sqf"; this addMPEventHandler ["MPRespawn",{_null = [_this select 0] execVM "vehiclelock.sqf";}];

Share this post


Link to post
Share on other sites

I think you need modify vehicle.sqf. Add these strings to file after vehicle creation command:

[_unit] execVM "vehiclelock.sqf";
_unit addMPEventHandler ["MPRespawn", {[_this select 0] execVM "vehiclelock.sqf"}];

And in vehicle's init leave only this string:

veh = [this] execVM "vehicle.sqf";

Share this post


Link to post
Share on other sites

Cheers pal, will give it a test an let you know the results. Just to clarify, I'll add the first code to the vehicle.sqf after the vehicle creation command like this?

=========================================================
*/

if (!isServer) exitWith {};

// Define variables
_unit = _this select 0;
_delay = if (count _this > 1) then {_this select 1} else {30};
_deserted = if (count _this > 2) then {_this select 2} else {120};
_respawns = if (count _this > 3) then {_this select 3} else {0};
_explode = if (count _this > 4) then {_this select 4} else {false};
_dynamic = if (count _this > 5) then {_this select 5} else {false};
_unitinit = if (count _this > 6) then {_this select 6} else {};
_haveinit = if (count _this > 6) then {true} else {false};

_hasname = false;
_unitname = vehicleVarName _unit;
if (isNil _unitname) then {_hasname = false;} else {_hasname = true;};
_noend = true;
_run = true;
_rounds = 0;

if (_delay < 0) then {_delay = 0};
if (_deserted < 0) then {_deserted = 0};
if (_respawns <= 0) then {_respawns= 0; _noend = true;};
if (_respawns > 0) then {_noend = false};

_dir = getDir _unit;
_position = getPosASL _unit;
_type = typeOf _unit;
_dead = false;
_nodelay = false;


// Start monitoring the vehicle
while {_run} do 
{	
sleep (2 + random 10);
     if ((getDammage _unit > 0.8) and ({alive _x} count crew _unit == 0)) then {_dead = true};

// Check if the vehicle is deserted.
if (_deserted > 0) then
{
	if ((getPosASL _unit distance _position > 10) and ({alive _x} count crew _unit == 0) and (getDammage _unit < 0.8)) then 
	{
		_timeout = time + _deserted;
		sleep 0.1;
	 	waitUntil {_timeout < time or !alive _unit or {alive _x} count crew _unit > 0};
		if ({alive _x} count crew _unit > 0) then {_dead = false}; 
		if ({alive _x} count crew _unit == 0) then {_dead = true; _nodelay =true}; 
		if !(alive _unit) then {_dead = true; _nodelay = false}; 
	};
};

// Respawn vehicle
     if (_dead) then 
{	
	if (_nodelay) then {sleep 0.1; _nodelay = false;} else {sleep _delay;};
	if (_dynamic) then {_position = getPosASL _unit; _dir = getDir _unit;};
	if (_explode) then {_effect = "M_TOW_AT" createVehicle getPosASL _unit; _effect setPosASL getPosASL _unit;};
	sleep 0.1;

	deleteVehicle _unit;
	sleep 2;
	_unit = _type createVehicle _position;
	_unit setPosASL _position;
	_unit setDir _dir;

	[_unit] execVM "vehiclelock.sqf";
        _unit addMPEventHandler ["MPRespawn", {[_this select 0] execVM "vehiclelock.sqf"}];

	if (_haveinit) then 
				{_unit setVehicleInit format ["%1;", _unitinit];
				processInitCommands;};
	if (_hasname) then 
				{_unit setVehicleInit format ["%1 = this; this setVehicleVarName ""%1""",_unitname];
				processInitCommands;};
	_dead = false;

	// Check respawn amount
	if !(_noend) then {_rounds = _rounds + 1};
	if ((_rounds == _respawns) and !(_noend)) then {_run = false;};
};
};

Edited by ringoray

Share this post


Link to post
Share on other sites

I made mistake. Leave only this string in vehicle.sqf:

[_unit] execVM "vehiclelock.sqf";

Also make sure that vehiclelock.sqf is in the same folder with vehicle.sqf.

Share this post


Link to post
Share on other sites

Don't edit Tophe's script. Use it as intended (and as documented inside the vehicle.sqf itself).

In the vehicle init line:

nul = [this] execVM "vehiclelock.sqf"; veh = [this, 30, 120, 0, FALSE, FALSE, "nul = [this] execVM 'vehiclelock.sqf'"] execVM "vehicle.sqf";

I used the default param values as you didn't seem to be changing them.

Share this post


Link to post
Share on other sites

Thanks both of you for your input. Schatten, the first method you told me to use worked just fine as I am sure your second method will. Harzach, since I overlooked that Tophe does not want that script edited, I shall also try the method you suggest. If indeed it does work then I think it is only right that I use that method to respect Tophe's wishes.

Again, thanks guys.

Share this post


Link to post
Share on other sites

It does work, as that's the way the script is intended to be used. It's not that he doesn't want it to be edited (though maybe he doesn't, I don't know). It's that the script has setVehicleInit functionality built into it. As I said, it is all documented inside the script itself.

Share this post


Link to post
Share on other sites

I agree, it does work. One thing I have noticed much to my frustration is in the vehiclelock.sqf I have followed the instruction concerning the allowed names or UIDs which are allowed to enter the vehicles. It seems that even with my UID in the array, I still get locked out of the vehicle.

_vehicle = _this select 0;  // i added this line.

_vehicle addEventHandler ["GetIn", 
{
private ["_veh", "_pos", "_unit", "_namesArray"];
_veh = _this select 0;
_pos = _this select 1;
_unit = _this select 2;
_namesArray = ["myUIDhere"];

if !(name _unit in _namesArray) then
{
_unit action ["Eject", vehicle _unit];
};
}];  

Would changing namesArray to uidArray or something help solve this problem? Sorry scripting is something I am not very good at at all:(

Share this post


Link to post
Share on other sites

If You want to use UIDs instead of names, do it this Way:

_vehicle addEventHandler["GetIn", {	private ["_veh", "_unit", "_uidArray"]; 
_veh = _this select 0; 
_unit = _this select 2;
_uidArray = [
	"7656119796",
	"0987654321",
	"9346578623"
]; 

if !((getPlayerUID _unit) in _uidArray)then{
	_unit action ["Eject", _veh];
};
}];

Changing the local Variable "_namesArray" to "_uidarray" would not change anything about the functionality of this script.

The important part is to replace "name _unit" with "getPlayerUID _unit" if You want to use UIDs instead of Names.

Renaming "_namesArray" to "_uidArray" is just for better understanding, but actually you can call it like you want.

Example:

_UIDArrayOfPlayersThatAreAllowedToGetIn = ["985267", "5436874", "23547"];

The line in the Script would then look like this:

if !(getPlayerUID _unit in _UIDArrayOfPlayersThatAreAllowedToGetIn) then

Greez KiloSwiss

Edited by KiloSwiss
Updated Code inside EventHandler

Share this post


Link to post
Share on other sites

Thanks for your response KiloSwiss, much appreciated. I will try this out and see what happens. Thanks again.

UPDATE: I did as you said and used your method Swiss. I put my UID in there and indeed I could enter the vehicle. I took out my UID again and entered a friends UID but I could also enter the vehicle, which should not have happened, I should have been kicked from the vehicle. Strange.

Edited by ringoray

Share this post


Link to post
Share on other sites

That happens when I post Code I did not test by myself.

I will start up the Editor this evening and then Post You a working solution.

Sorry for that!

/Edit

I have now edited my previous post.

This EH is testet in a local hosted Mission, and not in a dedicated environment!

Greez KiloSwiss

Edited by KiloSwiss

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  

×