Jump to content
Stormmy1950

Codition that Checks distance between objects and return true or false

Recommended Posts

Ok i am currently trying to make simple spawning and despawning objects with a single ace interaction.

My currently poblem is how to make a codition that checks distance between 2 objects and returns true or false

 

This is what i have so far:

_objects = nearestObjects [Targets_50m,["Target_PopUp_Moving_90deg_F"],10];
_bool = false;
if(_bool) then
{
	{
		deletevehicle _x;
	}forEach _objects;
	systemChat "Targets at 50m are deleted";
	_bool = false;
}
else
{
	_Target1 = createVehicle ["Target_PopUp_Moving_90deg_F", Targets_50m modelToWorld [0, 2, 0], [], 0, "NONE"];
	_Target2 = createVehicle ["Target_PopUp_Moving_90deg_F", Targets_50m modelToWorld [0, -2, 0], [], 0, "NONE"];
	_Target3 = createVehicle ["Target_PopUp_Moving_90deg_F", Targets_50m modelToWorld [0, 6, 0], [], 0, "NONE"];
	_Target4 = createVehicle ["Target_PopUp_Moving_90deg_F", Targets_50m modelToWorld [0, -6, 0], [], 0, "NONE"];

	_Target1 setDir 270;
	_Target2 setDir 270; 
	_Target3 setDir 270; 
	_Target4 setDir 270;  
	_bool = true;

	systemChat "Targets at 50m Spawned";
};

 

Share this post


Link to post
Share on other sites

use distance. when i was making my weapon/zombie spawner/despawner, i got an array of all the players and all of the weaponholders/zombie units and simply had a control variable that determined if they should be deleted by looping through all of the objects and zombies and players with a nested foreach. so outer foreach loop was for an array of objects and zombies, the inner foreach loop was for players. i would do a check, if the object/zombie was less than 300 meters from the player, dont delete and break out of that foreach to the outter foreach. i have to go real quick but heres a quick example of what i mean

{
  _object = _x;
  _deleteObject = true;
  {
    if (_object distance _x < 300) then
    {
      _deleteObject = false;
      //break out of loop
    };
  } forEach _players;
  if (_deleteObject) then
  {
    deleteVehicle _object;
  };
} forEach _objects;

 

Share this post


Link to post
Share on other sites
43 minutes ago, gokitty1199 said:

 

 

Share this post


Link to post
Share on other sites
_fnc_deleteObjectsWithinRange = {
params ["_objetcs","_posOrObject"];
{
	if ((_x distance _posOrObject)< 300) then {deleteVehicle _x};
} forEach _objects;
};

[_myArrayOfObjects,_myDesiredPos] call  _fnc_deleteObjectsWithinRange; // will delete all objects from _myArraYOfObjects within 300m from _mydesiredPos

 

Share this post


Link to post
Share on other sites

@Mr H. Ty for that but how do i spawn them again so basicly i want to spawn and delete them X amout of times ? To sum it all up how to create a Toggle codition ?

Share this post


Link to post
Share on other sites

_objects will return an empty array if there are no targets nearby anyway. So use something like if (_objects isEqualTo []) then ...

Share this post


Link to post
Share on other sites

@Mr H. Sorry i am still haveing problems whit this can you tell me how do i get the empty array from your function that you gave me.

Share this post


Link to post
Share on other sites

You don't need it actually just modify yours _bool = !(_objects isEqualTo []);

Share this post


Link to post
Share on other sites
_objects = nearestObjects [Targets_50m,["Target_PopUp_Moving_90deg_F"],10];
_bool = !(_objects isEqualTo []);
if(_bool) then
{
	{
		deletevehicle _x;
	}forEach _objects;
	systemChat "Targets at 50m are deleted";
	//_bool = false;
}
else
{
	_Target1 = createVehicle ["Target_PopUp_Moving_90deg_F", Targets_50m modelToWorld [0, 2, 0], [], 0, "NONE"];
	_Target2 = createVehicle ["Target_PopUp_Moving_90deg_F", Targets_50m modelToWorld [0, -2, 0], [], 0, "NONE"];
	_Target3 = createVehicle ["Target_PopUp_Moving_90deg_F", Targets_50m modelToWorld [0, 6, 0], [], 0, "NONE"];
	_Target4 = createVehicle ["Target_PopUp_Moving_90deg_F", Targets_50m modelToWorld [0, -6, 0], [], 0, "NONE"];

	_Target1 setDir 270;
	_Target2 setDir 270; 
	_Target3 setDir 270; 
	_Target4 setDir 270;  
	//_bool = true;

	systemChat "Targets at 50m Spawned";
};

This is what i have now and I get the error on Line 2 Invalid number in Expression.

Share this post


Link to post
Share on other sites

Replace the condition itself eg if !(_objects isEqualTo []) then etc.

Share this post


Link to post
Share on other sites

I don't see anything wrong, I'm not at my computer to test it. Check for hidden characters that might be copied along with the code from this forum it's a known issue. Also if you're trying it from the console the commented parts will throw errors.

  • Thanks 1

Share this post


Link to post
Share on other sites

@Mr H. Apperently that was it i was copying from the forum and when i type it my self the it work Big thank you For helping me with this

 

Moderator you are clear to lock the Topic Now ;D

  • Haha 1

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

×