Jump to content

check if any vehicle is in an area.

Recommended Posts

just trying to figure out a simple check.. im a novice scripter so im probably missing a function or syntax error or something.

basically i have some spawned in compositions and vehicle spawn scripts. they all work. but ideally i need to check that any vehicle isnt near before my script fires.. (i've covered objects just not vehicles)
i've been playing about doing simple stuff such as using  vehicles distance _myobject and looking at inarea/inarea arrays. i've tried a range of stuff but im a little lost.



_isclear = code to check if a vehicle is in a 100m of an object.. store that information eg presence of a vehicle within 100m returns false.

if (_isclear = true)

	//my code if the area is clear (returned true)
	["TaskSucceeded",["","spawned the composition"]] call BIS_fnc_showNotification; //lazy test notification

	//alternative code if a vehicle is present unsafe to spawn composition
	["TaskFailed",["","a Vehicle is too close"]] call BIS_fnc_showNotification; //lazy test notification


hope some one could give me a quick example. with a little explanation to learn from!

Share this post

Link to post
Share on other sites

_isClear  is a local variable. It must be defined when/where you are calling it.
if _isClear is the result of a code, (so this code ends by: _isClear;   // simple line returning a boolean), you can check by something like:

systemChat str _isClear;

if this local variable is defined and return false or true.



you code must be:

if (_isClear) then {....}  //  condition is a boolean. _isClear = true is a code, furthermore, = pass a value to a variable, == is for comparison (except array)



Share this post

Link to post
Share on other sites

Thanks for that. this is quick code i've put together. just to show where i'm a little lost!! Thanks for the futher details. so ideally i should be using  if (_isClear) or if (_isClear == true). depending on the way i need to go about doing my actual check!

on to running my check..
I'll be honest i have no idea what im looking for or doing....  I'm not sure if using inArea/inAreaArrays is the way to go. I've tried some research, not finding much of an example of its use for my situation. since im checking for any vehicles its more likely inAreaArrays

_mytrigger = createTrigger ["EmptyDetector", getPos my_object]; //create a trigger area created at object with variable name my_object
_mytrigger setTriggerArea [50, 50, 0, false]; // trigger area with a radius of 100m.

// this is where im completely baffled with no knowledge.. 

_isClear = vehicles inAreaArrays _mytrigger; //vehicles (all vehicles) inAreaArray (Returns list of Objects or Positions that are in the area _mytrigger.)   

should i store this in a variable ? could i remove the _isClear variable completely in one sense if the return value of vehicles returns empty. then my IF statement can move on to " then{} " and if a vehicle is in the area then run "else{}"
I literally dont know how to get that value. or put it into the right syntax to use.

is it simple enough to just drop an exclamition mark in there and do something like...

If !vehicles inAreaArrays _mytrigger;  then{}

i have issues where i make matters overly complexed than i need too!

Share this post

Link to post
Share on other sites

If you have a trigger, why are you not using on act /deact field?
The condition for no vehicles is:

vehicles findIf {_x inArea thisTrigger} == -1

(for any vehicles. You can filter by side if needed) After that, without any clue on what you want to do with that, the trigger can fire at start as well.

Share this post

Link to post
Share on other sites

just using triggers as an example on here. Ideally i would like to do it through an If statement.. EG if i decided to use a marker/object/position instead. it's something i could use on a few things i've learnt.
eg spawning vehicles or compositions. if i spawn more than one vehicle in a tight space then it can lead to some explosions. or a moderately sized composition may spawn on a vehicle and destroy it. using BIS_fnc_findSafePos helps with certain things. but having a check before spawning things would just be cleaner.

Share this post

Link to post
Share on other sites

Without any trigger, you can add a while {} do {} loop, looking for a convenient place.

I'll not develop that here. I spent days and weeks before making a spawn group module far better than BI one (try to spawn tanks in cities!).


Anyway, as step by step process, if you are only looking for other vehicles' presence, (so no house,wall,tree...), the system looks like:

private _pos0 = aReferencePosition; // randomized or not, this is the position as reference

private _pos = _pos0; // will be the position to spawn your vehicle
private _radius = 50; //  max radius for a new position around _pos0
private _safeDistance = 20;  // or what you want as minimal distance between 2 vehicles

while { vehicles findIf {_x distance _pos < _safeDistance} > -1} do {

_pos =  _pos0 getPos [_radius * sqrt random 1, random 360];

Now you can use _pos for spawning the vehicle.

Notes: That's the very very basic! You can add a condition with bis_fnc_findSafePos. Personally, I'm using straight isFlatEmpty command.

The process can fail (infinite loop) if you have no place found, due to wide safe distance and/or tiny radius and/or too many vehicles... As said, it's a basic loop. You can add a counter for exiting this loop... after 100 attempts.

  • Thanks 1

Share this post

Link to post
Share on other sites

Yeah since most of my compositions are quite basic. Simply _x hideobject true does the trick. Its rare that i'll use spawned compositions in populated areas As I'd hand craft it. (Hide it if required)


That example is much better than what i expected! The whileloop would of been enough. Thank you for your time. Just needed something open ended as a basis to learn and work with.


Will let you know how i get on! 

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