Jump to content
Sign in to follow this  
tachi

Help with nearestObjects

Recommended Posts

I need to find all the objects of type TimeBombCore in radius of 25m, and then by selecting the first object from array i do some actions based on it, playing some sound and creating some text. It theory code below should work, but it doesn't. I'm not using nearestObject for a reason, i will need to get more objects not only the ones with classname TimeBombCore in the future, thats why i'm using nearestObjects, i've tested the code with nearestObject and it works just fine and as i want, but it doesn't work when i'm trying to use nearestObjects. What am i doing wrong? :confused:

I have this code:

private["_unit","_posUnit","_mine","_posMine","_dis"];
_unit = _this select 0;
_posUnit = position _unit;



_mines = nearestObjects [_posUnit, ["TimeBombCore"], 25];
_mine = _mines select 0;


if (count _mines == 0) exitwith {};


_posMine = position _mine;


_dis = _posUnit vectorDistance _posMine;



if (_dis <= 25 && _dis > 20) exitWith {playSound "Mine_Detector_Sound_2";systemChat ("Distance to danger: "+str(_dis));};
if (_dis <= 20 && _dis > 15) exitWith {playSound "Mine_Detector_Sound_3";systemChat ("Distance to danger: "+str(_dis));};
if (_dis <= 15 && _dis > 7) exitWith {playSound "Mine_Detector_Sound_4";systemChat ("Distance to danger: "+str(_dis));};
if (_dis <= 7 && _dis > 1)  exitWith {playSound "Mine_Detector_Sound_5";systemChat ("Distance to danger: "+str(_dis));};

---------- Post added at 09:01 AM ---------- Previous post was at 08:43 AM ----------

That's how its working with neareastObject:

private["_unit","_posUnit","_mine","_posMine","_dis"];
_unit = _this select 0;
_posUnit = position _unit;
_mine = nearestObject [_posUnit, "TimeBombCore"];
if (isNull _mine) exitWith{
//systemChat ("Danger: 0");
};
_posMine = position _mine;


_dis = _posUnit vectorDistance _posMine;



if (_dis <= 25 && _dis > 20) exitWith {playSound "Mine_Detector_Sound_2";systemChat ("Distance to danger: "+str(_dis));};
if (_dis <= 20 && _dis > 15) exitWith {playSound "Mine_Detector_Sound_3";systemChat ("Distance to danger: "+str(_dis));};
if (_dis <= 15 && _dis > 7) exitWith {playSound "Mine_Detector_Sound_4";systemChat ("Distance to danger: "+str(_dis));};
if (_dis <= 7 && _dis > 0)  exitWith {playSound "Mine_Detector_Sound_5";systemChat ("Distance to danger: "+str(_dis));};

Share this post


Link to post
Share on other sites

I would try moving your _mines array count exception before the "_mine = _mines select 0;" line, and see if that helps any. And if you haven't already I would enable -showScriptErrors in your Steam startup parameters to see if any other errors pop up.

Edited by JShock

Share this post


Link to post
Share on other sites

I doubt moving the count will help, but I'll try. And I have show script errors parameters enabled, it doesn't show any errors, its justjust not working

Share this post


Link to post
Share on other sites

The first object in the returned array is***not***necessarily the closest one (like it is innearestObjects***for example).

I need so that closest object must be first in the array

Share this post


Link to post
Share on other sites

Thanks for the sorting funciotns, but it didnt help alot

private["_unit","_posUnit","_mine","_posMine","_dis"];
_unit = _this select 0;
_posUnit = position _unit;


_allmines = _posUnit nearEntities ["TimeBombCore", 25];
_nearestmines = [_allmines,[],{_posUnit distance 25},"ASCEND"] call BIS_fnc_sortBy;

_mine = _nearestmines select 0;


_posMine = position _mine;


_dis = _posUnit vectorDistance _posMine;



if (_dis <= 25 && _dis > 20) exitWith {playSound "Mine_Detector_Sound_2";systemChat ("Distance to danger: "+str(_dis));};
if (_dis <= 20 && _dis > 15) exitWith {playSound "Mine_Detector_Sound_3";systemChat ("Distance to danger: "+str(_dis));};
if (_dis <= 15 && _dis > 7) exitWith {playSound "Mine_Detector_Sound_4";systemChat ("Distance to danger: "+str(_dis));};
if (_dis <= 7 && _dis > 1)  exitWith {playSound "Mine_Detector_Sound_5";systemChat ("Distance to danger: "+str(_dis));};

Now it gives me next error: undefined variable in _mine

---------- Post added at 04:47 AM ---------- Previous post was at 04:45 AM ----------

Yep, you've got that one too : BIS_fnc_nearestPosition

I didn't understand how should i call this function?

Share this post


Link to post
Share on other sites

Your using the distance command incorrectly in the sortBy function, it compares two objects/position arrays, not a number and an object.

Share this post


Link to post
Share on other sites

Hey not sure if this will help or not but I was working on a dog script and one of the functions is that when you take control of the dog it will show you all explosive within 200m like the dog "smells" them.

	
_dog = group player createUnit
[
	"Alsatian_Random_F",
	[getpos player select 0,(getpos player select 1)+3,(getpos player select 2)+0.2],
	[], 0, "FORM"
];

veh = vehicle player;
_dogClose = nearestObject [player, "Alsatian_Base_F"];

if (_dogClose in units player) then
{
	dogVeh = _dogClose
};

_dMine1 = dogVeh nearObjects ["timebombcore",200];
_dMine2 = dogVeh nearObjects ["minebase",200];
dMine = _dMine1 + _dMine2;

	act1 = player addAction ["<t color=""#00ff33"">" + ("Dog Menu") + "</t>",
{
	if (!alive dogVeh) then
	{ player removeAction act1;
	hint "You're dog has died";}
	else {


		player removeAction act2;
		player removeAction act3;
		player removeAction act5;
		player removeAction act6;
		player removeAction act7;
		player removeAction act8;
		act2 = player addAction ["		Control Dog",
	{

				player removeAction act2;
				player removeAction act5;
				player removeAction act8;
				dogVeh switchCamera "GROUP";
				_dMote = player remoteControl dogVeh;
				if (count dMine > 0) then	
		{
					["minetrgt", "onEachFrame",
			{
				{


				drawIcon3D	[
							"\a3\ui_f\data\map\Markers\NATO\n_unknown.paa",
							[0.99,0,0,1],
							[(visiblePosition _x select 0),
							(visiblePosition _x select 1),
							((visiblePosition _x select 2)+0.5)],
							0.5,
							0.5,
							0,
							"Explosives",
							2,
							0.03,
							"PuristaMedium"
							];

				} forEach dMine;
			}
					]call BIS_fnc_addStackedEventHandler;
		}
				else {hint "No explosives nearby"};

			act3 = player addAction ["		Control Player",
			{
				player switchCamera "INTERNAL"; 
				player removeAction act3;
				if (count dMine > 0) then
		{ 

				act4 = player addAction ["		Remove Targets",
				{
					["minetrgt", "onEachFrame"] call BIS_fnc_removeStackedEventHandler;
					player removeAction act4;
				},"",0.01,false,false];																
		};
			},"",0.03,false,false];
	},"",0.03,false,false];

		act5 = player addAction ["		Put Vest On Dog",
	{
		player removeAction act2;
		player removeAction act5;
		player removeAction act8;
		bags = 
		[
			"Vest_V_TacVest_blk",
			"Vest_V_TacVest_khk",
			"Vest_V_TacVest_brn",
			"Vest_V_TacVest_camo",
			"Vest_V_TacVest_oli",
			"Vest_V_TacVest_blk_POLICE",
			""
		];
		pack = cursorTarget;
		if (typeOf(pack) in bags) then { 
		pack enableSimulation true;
		pack hideObject true;
		pack attachTo [dogVeh,[0,-0.4,0.415],"aimpoint",0,"",false];
		pack setDir 180;
		sleep 1;
		pack enableSimulation true;
		pack hideObject false;
		pack enableCollisionWith dogVeh}
		else {hintc "You need to be looking at a tactical vest for this to work"};


	},"",0.02,false,false];

		if((vehicle player isKindOf "LandVehicle")||
			(vehicle player isKindOf "Air")||
			(vehicle player isKindOf "Ship"))
			then
	{
						veh = vehicle player;
		act6 = player addAction ["		Load Dog",
		{

			if (veh isKindOf "LandVehicle") then
			{
				dogVeh attachto [veh,[0,-0.4,-1]];
			};
			if (veh isKindOf "Air") then
			{
				dogVeh attachto [veh,[0,2.6,1.9]];
			};
			dogveh switchmove "Dog_Sit_01";
			dogVeh disableAI "MOVE";
			dogVeh disableAI "ANIM";
			dogVeh disableAI "FSM";
			dogVeh disableAI "target";
			player removeAction act2;
			player removeAction act3;
			player removeAction act5;
			player removeAction act6;
			player removeAction act7;
			player removeAction act8;

			act7 = player addAction ["		Dog Out",
			{												
				player removeAction act7;
				detach dogVeh;
				if ((getPos dogVeh select 2) > 100) then
				{
					dogVeh allowDammage false;
					dogveh switchmove "Dog_Stop";
					sleep 3;
					_chute = createVehicle 
					[
						"NonSteerable_Parachute_F", 
						position dogVeh, [], 0, "CAN_COLLIDE"
					];
					_chute setPos getPos dogVeh;
					dogVeh attachTo [_chute,[0,-0.1,0.2]]; 
					if ((getPos dogVeh select 2) < 5) then
					{	detach _chute;
						dogVeh allowDammage true;
						dogVeh enableAI "MOVE";
						dogVeh enableAI "ANIM";
						dogVeh enableAI "FSM";
						dogVeh enableAI "target";
					}; 
				}
				else
				{
					dogVeh enableAI "MOVE";
					dogVeh enableAI "ANIM";
					dogVeh enableAI "FSM";
					dogVeh enableAI "target";
				};
			},"",0.02,false,false];	

		},"",0.02,false,false];
	};

		act8 = player addAction ["<t color=""#ff3300"">" + ("Dog Menu (close)") + "</t>",
		{
			player removeAction act2;
			player removeAction act5;
			player removeAction act8;
		},"",0.01,false,false];};
},"",0.04,false,false,"",""];

Share this post


Link to post
Share on other sites

So what do you mean when you say it "doesn't work"? What is it doing?

Diag_log out a count on _mines. Is it an empty array? What happens if you bump up the radius? Are you completely positive that there is a mine in the area? Bump up the radius to 50 and then place a mine right next to your player and run it. Zero mines?

One thing that comes to my mind right away is the sometimes unexpected behavior of positions. position, which is a synonym for GetPos, is a terrible command and should be avoided. It will give you inaccurate and slow information. Change that to a getPosATL. Also, sometimes commands like these will have issues deciding whether or not the item -actually is- within the radius. That can be determined by a difference in an objects local [0,0,0] position relative to the world and the world positionATL. I've found that I can stand right next to an object and a nearestObjects at 10 meters won't catch it but 15 or 20 will.

For something as simple as this, if you find it isn't behaving, use what works [in this case the nearObjects]. The sortBy functions are overblown for your needs. I would do something as simple as:

_pos = getPosATL player; //or whatever position you need
_dist = 30;
_mine = objNull;
{
  _d = _x distance _pos;
  if (_d < _dist) then {
    _mine = _x;
    _dist = _d;
  };
}forEach _mines;

Finally, I don't see why you are using the vectorDistance command for a distance decision. Use distance or distanceSqr (if you're REAAALLLLY pinching pennies).

Edited by Tiskahar

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  

×