Jump to content
Sign in to follow this  
nebulazerz

Spawning a car if a car is not already on a marker

Recommended Posts

I am trying to create a vehicle spawn that only works if another vehicle is not present on the marker that I am creating in the same script. Right now it just spawns the vehicle with no errors but does like it always does and still spawns infinite vehicles

//Create Marker
createMarker ["Vehicle_Spawn",[2926,13069]];
"Vehicle_Spawn" setMarkerType "Empty";
"Vehicle_Spawn" setMarkerSize [20, 20];
//Check if the marker has any cars near it
_position = getMarkerPos "Vehicle_Spawn";
_scanArea = 20;
_nearestTargets = nearestObjects [_position, ["CAR","TANK"], _scanArea];
_validNearestTargets = [];
{if (alive _x) then {_validNearestTargets set [(count _validNearestTargets),_x];};} forEach _nearestTargets;
//if the marker has no cars near it then spawn the new car with nothing in inventory
if (_validNearestTargets in _position) then {
	hint "Not enough space";
	}else{
		_veh = createVehicle ["B_T_LSV_01_unarmed_F", _position, [], 0, "NONE"];
		_veh setVariable ["BIS_enableRandomization", false];
		clearWeaponCargoGlobal _veh;
		clearMagazineCargoGlobal _veh;
};

F6339B21F881D50101047C432CC3C8B99E587709

  • Like 1

Share this post


Link to post
Share on other sites

You could check the code in Tonic's Virtual Vehicle Spawner as it has this feature.

 

https://forums.bistudio.com/topic/157077-virtual-vehicle-spawner-vvs/

Thank you very much, his was deleting vehicles and replacing them  but I got it to work perfectly by changing the function and a few other things and I would like to share it so anyone who searches this issue can use this.

disableSerialization;
private["_position","_direction","_nearestTargets","_scanArea"];
//Create Marker
createMarker ["Vehicle_Spawn",[2926,13069]];
"Vehicle_Spawn" setMarkerType "Empty";
"Vehicle_Spawn" setMarkerSize [20, 20];
//Check if the marker has any cars near it
_position = getMarkerPos "Vehicle_Spawn";
_scanArea = 20;
_direction = 45;
_nearestTargets = nearestObjects[_position,["landVehicle","Air","Ship"],12] select 0;
//if the marker has no cars near it then spawn the new car with nothing in inventory
if (!isNil "_nearestTargets") then {
	hint "Not enough space";
	}else{
		_veh = createVehicle ["B_T_LSV_01_unarmed_F", _position, [], 0, "NONE"];
		_veh setVariable ["BIS_enableRandomization", false];
		_veh setDir _direction;
		_veh setPos _position;
		clearWeaponCargoGlobal _veh;
		clearMagazineCargoGlobal _veh;
		clearItemCargoGlobal _veh;
};

Share this post


Link to post
Share on other sites

I don't see the problem, is that not what a vehicle spawn marker is supposed to look like?  Looks like everyone one I've ever used.  <_<

  • Like 3

Share this post


Link to post
Share on other sites

I don't see the problem, is that not what a vehicle spawn marker is supposed to look like?  Looks like everyone one I've ever used.  <_<

wasnt the marker, it was the way i was trying to check to see if there was an object in the marker. you can see the changes I made above through reverse engineering Tonics script.

 

Edit, messing around with it a bit. Is it safe, performance wise that is, to use something like this to spawn a vehicle in a spot every 60 seconds and use cleanup scripts to clean up empty vehicles or use another MUCH bigger marker that restricts the amount of vehicles that can be spawned total in my game area?

while{true} do{
	disableSerialization;
	private["_position","_direction","_nearestTargets","_scanArea"];
//Create Marker
	createMarker ["Vehicle_Spawn",[2926,13069]];
	"Vehicle_Spawn" setMarkerType "Empty";
	"Vehicle_Spawn" setMarkerSize [20, 20];
//Check if the marker has any cars near it
	_position = getMarkerPos "Vehicle_Spawn";
	_scanArea = 20;
	_direction = 45;
	_nearestTargets = nearestObjects[_position,["landVehicle","Air","Ship"],12] select 0;
//if the marker has no cars near it then spawn the new car with nothing in inventory
if (!isNil "_nearestTargets") then {
		sleep 60;
	}else{
			_veh = createVehicle ["B_T_LSV_01_unarmed_F", _position, [], 0, "NONE"];
			_veh setVariable ["BIS_enableRandomization", false];
			_veh setDir _direction;
			_veh setPos _position;
			clearWeaponCargoGlobal _veh;
			clearMagazineCargoGlobal _veh;
			clearItemCargoGlobal _veh;
			sleep 120;
	};
};

 

Share this post


Link to post
Share on other sites

I don't see the problem, is that not what a vehicle spawn marker is supposed to look like?  Looks like everyone one I've ever used.  <_<

 

 

:lol:  :lol:  :lol:

  • Like 1

Share this post


Link to post
Share on other sites

Why spawn it every 60 seconds?  Personally I'd make them spawn on demand and if they happen to spawn one while another is on the marker, that's on them.  Even better if they have limited spawns and just wasted one for not paying attention.   :banghead:

  • Like 1

Share this post


Link to post
Share on other sites

Why spawn it every 60 seconds?  Personally I'd make them spawn on demand and if they happen to spawn one while another is on the marker, that's on them.  Even better if they have limited spawns and just wasted one for not paying attention.   :banghead:

this is for a few basic vehicles that can be used for free to get into battle quick. I was getting this part down so I can add the vehicles to the shop properly next ;)

 

I have been testing it like this for at least 30 mins in an online test server running it from the initServer.sqf seems no issues yet with performace, tho my machine is a beast so it might take a while to tell if its building up on the ram.  ^_^  ^_^

Share this post


Link to post
Share on other sites

this is for a few basic vehicles that can be used for free to get into battle quick. I was getting this part down so I can add the vehicles to the shop properly next ;)

 

For those just use the simple vehicle respawn module and like a 30 second empty/dead timer or something.  A line of Prowlers/Quadbikes off to the side or something.

Share this post


Link to post
Share on other sites

For those just use the simple vehicle respawn module and like a 30 second empty/dead timer or something.  A line of Prowlers/Quadbikes off to the side or something.

i started writing this because i couldn't get them to spawn empty so i started resorting to spawn them in scripts. would like to take as much out of the editor as I can anyway, if not just for learning.

 

Edit: I have rewritten it a little bit so that the vehicle deletes itself and respawns every 200 seconds to not run into any problems of having too many.

while{true} do{
	if (isServer) then {
		disableSerialization;
		private["_position","_direction","_nearestTargets","_scanArea"];
//Create Marker
		createMarker ["LSV1",[2926,13069]];
		"LSV1" setMarkerType "Empty";
		"LSV1" setMarkerSize [2, 2];
//Check if the marker has any cars near it
		_position = getMarkerPos "LSV1";
		_scanArea = 5;
		_direction = 45;
		_nearestTargets = nearestObjects[_position,["landVehicle","Air","Ship"],_scanArea] select 0;
//if the marker has no cars near it then spawn the new car with nothing in inventory
if (!isNil "_nearestTargets") then {
			sleep 200;
		}else{
				_veh = createVehicle ["B_T_LSV_01_unarmed_F", _position, [], 0, "NONE"];
				_veh setVariable ["BIS_enableRandomization", false];
				_veh setDir _direction;
				_veh setPos _position;
				clearWeaponCargoGlobal _veh;
				clearMagazineCargoGlobal _veh;
				clearItemCargoGlobal _veh;
				_veh setVehicleVarName "BluLSV1";
				BluLSV1 = _veh;
				execVM "Classes\Blu\Vehicles\BluLSV1Cleanup.sqf";
				sleep 200;
		};
	};
};

BluLSV1Cleanup.sqf

while{true} do{
		sleep 199;
		deleteVehicle BluLSV1;
	};

Would there be any way to make it so every vehicle made this way gets its own variable that is assigned the same way but changes by number like BluLSV1, BluLSV2,BluLSV3, ect..?

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  

×