Jump to content
aie-boshell

Move Unit to NearestObjects of Certain Type, Select 0

Recommended Posts

Hello, I need a script to allow me to move a unit to the nearest object of a certain type.  I believe the answer is 

 

nameofunit move {nearestobject [position nameofunit, "land_pencilred_f"]}

I tried this with "land_pencilred_f" being the classname of the objects that I'd like my unit to move to the nearest pencil to his current location.  No luck so far but any help is much appreciated :-)

Share this post


Link to post
Share on other sites

nameofunit move getpos (nearestobject [nameofunit, "land_pencilred_f"])

 

but do not expect fine movement.

Share this post


Link to post
Share on other sites

In this example, the object the unit (Variable name is set to "myGuy") will move to is an invisible Helipad placed in the editor:

  1. Place an invisible helipad (Classname is "Land_EmptyHelipad_F")
  2. Place a unit and name him myGuy
  3. Place the below code into his initialization box:

_myGuyObj=nearestObject [myGuy, "Land_EmptyHelipad_F"];
_myGuyObjPos=getPosATL _myGuyObj;


myGuy doMove _myGuyObjPos;

 

If you just want him to teleport to the object, then use setPos instead of move or doMove:
myGuy setPos _myGuyObjPos;

Share this post


Link to post
Share on other sites
1 hour ago, pierremgi said:

nameofunit move getpos (nearestobject [nameofunit, "land_pencilred_f"])

 

but do not expect fine movement.

Looks fantastic, pierremgi.  I'm going to test it very soon on my end.  When you say don't expect fine movement, is there a better command that will give better movement?  Addwaypoint, perhaps?  I couldn't get that one to work at all but you've been awesome in helping me so I'm putting tons of weight into any information you choose to share.  Thanks very much, Pierre :-)

Share this post


Link to post
Share on other sites
1 hour ago, phronk said:

In this example, the object the unit (Variable name is set to "myGuy") will move to is an invisible Helipad placed in the editor:

  1. Place an invisible helipad (Classname is "Land_EmptyHelipad_F")
  2. Place a unit and name him myGuy
  3. Place the below code into his initialization box:

_myGuyObj=nearestObject [myGuy, "Land_EmptyHelipad_F"];
_myGuyObjPos=getPosATL _myGuyObj;


myGuy doMove _myGuyObjPos;

 

If you just want him to teleport to the object, then use setPos instead of move or doMove:
myGuy setPos _myGuyObjPos;

I do just want to give certain units a waypoint so that they as AI will move to certain areas on the battlefield where I'll have these objects.  The invisible helipad would be a great idea.  I will need two ideas though, one for each side.  If I used the invisible helipad for the Blufor for example can I use the invisible wall as well or does the invisible wall make up some type of invisible boundary?  There's also protection zone invisible.  Are all 3 of these sufficient objects to use for nearestobject?  Also, might there be a way to set the move point to a certain radius like we can with setwaypoint?  So much good information, the possibilities are endless, this is exciting stuff.  Thank you, Phronk.

Share this post


Link to post
Share on other sites

For the record, you can just make things invisible with the hideObjectGlobal command.  Is this what you're trying to do?

 

//Reserves the _myGuyObj variable

_myGuyObj=objNull;

 

//Chooses a the nearest object of a specific type,  object type chosen is based on myGuy's side

switch(side myGuy)do{

     case WEST : {_myGuyObj=nearestObject [myGuy, "Land_EmptyHelipad_F"];};

     case EAST : {_myGuyObj=nearestObject [myGuy, "Land_JumpTarget_F"];};

     case INDEPENDENT : {_myGuyObj=nearestObject [myGuy, "Land_ClutterCutter_small_F"];}

     case CIVILIAN : {_myGuyObj=nearestObject [myGuy, "Land_ClutterCutter_medium_F"];}

};

 

//If object not found/doesn't exist, end script here

if(isNull _myGuyObj)exitWith{};

 

//Stores the nearest object found in a variable

_myGuyObjPos=getPosATL _myGuyObj;

 

//Deletes all of myGuy's current waypoints, so he doesn't try to move to any pre-existing waypoints prior to this code
if(count waypoints myGuy>0)then{{deleteWaypoint((waypoints myGuy)select 0);}forEach waypoints myGuy;};

 

//Code specific to myGuy's side will execute

switch(side myGuy)do{

     case WEST : {myGuy doMove _myGuyObjPos; systemChat "Im blufor!";};

     case EAST : {myGuy doMove _myGuyObjPos; systemChat "Im opfor!";};

     case INDEPENDENT : {myGuy doMove _myGuyObjPos; systemChat "Im AAF!";}

     case CIVILIAN : {myGuy doMove _myGuyObjPos; systemChat "Im a civ!";}

};

Share this post


Link to post
Share on other sites

Oh man, guys.  I made a mistake.  I thought nearestobject would work for me but that only detects objects up to 50 meters away.  I need to detect objects all over the map so I will need to use NEARESTOBJECTS and select 0 to get the nearest object to the unit of the given type.  I've been using many combinations of this and have had no luck.  If you can help me with nearestobjects select 0 that would be fantastic.  

Share this post


Link to post
Share on other sites

This code was written by sc and optimized by me, I use it in my radio script.  This will sort the elements of an array from nearest to farthest and then select the first (nearest) element from that array:

 

Script Name: sort.sqf

Function Name: sortD

Code:

private["_current","_nearest","_nearestD","_obj","_objs","_sorted"];
_obj=_this select 0;_objs=_this select 1;_sorted=[];
{_nearest=objNull;_nearestD=20000;_i=0;
{_current=_objs select _i;_distance=_current distance _obj;
if(_distance<_nearestD)then{_nearest=_current;_nearestD=_distance};
_i=_i+1;
true;
}count _objs;
_sorted pushBack _nearest;_objs=_objs-[_nearest];
true;
}count _objs;
_sorted

 

Paste this in your init.sqf:

sortD=compileFinal preprocessFile "sort.sqf";

 

 

Using the sortD function:

 

//Finds all "Land_EmptyHelipad_F" objects within 20,000 meters of myGuy

_myGuyObjects=nearestObjects[myGuy,["Land_EmptyHelipad_F"],20000];

 

//Sorts all objects found and selects the nearest one (_myGuyObj is the variable that contains the nearest element)

private _myGuyObj=([myGuy,_myGuyObjects]call sortD)select 0;

Share this post


Link to post
Share on other sites

You'll probably be better off using the allMissionObjects command instead of nearestObjects with massive range.

 

Here is a quicker and shorter variant:

fnc_nearestMO = {
    params["_guy","_class"];
    private ["_objs","_i","_dist"];
    _objs = allMissionObjects _class;
    _dist = _objs apply {_guy distance2D _x};
    _i = _dist find (selectMin _dist);
    _objs select _i
};
myObject = [myGuy,"Land_EmptyHelipad_F"] call fnc_nearestMO;

 

 

ps.: Don't use distance for comparison, use either distanceSqr or distance2D (if height difference doesn't matter)

  • Like 1
  • Sad 1

Share this post


Link to post
Share on other sites

You guys are amazing!  All three of you have helped me immensely.  Great information for me, Tajin.  I think I will use that in my mission.  You guys have a great day :-)

Share this post


Link to post
Share on other sites
On 02/11/2017 at 1:04 AM, aie-boshell said:

Hello, I need a script to allow me to move a unit to the nearest object of a certain type.  I believe the answer is 

 


nameofunit move {nearestobject [position nameofunit, "land_pencilred_f"]}

I tried this with "land_pencilred_f" being the classname of the objects that I'd like my unit to move to the nearest pencil to his current location.  No luck so far but any help is much appreciated :-)

Is this for SP or MP?

16 hours ago, Tajin said:

You'll probably be better off using the allMissionObjects command instead of nearestObjects with massive range.

 

Here is a quicker and shorter variant:


fnc_nearestMO = {
    params["_guy","_class"];
    private ["_objs","_i","_dist"];
    _objs = allMissionObjects _class;
    _dist = _objs apply {_guy distance2D _x};
    _i = _dist find (selectMin _dist);
    _objs select _i
};
myObject = [myGuy,"Land_EmptyHelipad_F"] call fnc_nearestMO;

 

 

ps.: Don't use distance for comparison, use either distanceSqr or distance2D (if height difference doesn't matter)

I read allMissionObjects command can be very demanding. I haven't tried this myself in A3, purely for the reason of the comment on the Wiki.

https://community.bistudio.com/wiki/allMissionObjects

Quote

Rocket - Be VERY careful with the use of this command. It is very demanding as it must iterate through all mission created objects. Particular care should be taken exercising this often on dedicated servers.

 

  • Like 1

Share this post


Link to post
Share on other sites

Thank you for the input, HazJ.  This is for a multiplayer game mode on dedicated server that will be  extremely demanding by design.  As a matter of fact, I'm expecting to push it beyond what would allow good performance and then cut corners where necessary to get acceptable performance out of the game mode on servers and clients with high end systems.  With that being said, your input on allMissionObjects could be very useful.  I will make note of that in case I suffer performance issues when everything is complete and once again I appreciate very much the information you've shared with me and the community.

Share this post


Link to post
Share on other sites

I think allMissionObjects isn't as taxing when you're only looking for one type of object, I use it for a garbage collection script I wrote and I haven't noticed any FPS impact at all.  Anyway, for future reference when you feel like learning which commands run faster than the other, this wiki page is a godsend.

  • Like 1

Share this post


Link to post
Share on other sites

Yes, I know of the link you posted. I assume your clean-up is run on the server? Also, it shouldn't hurt if used carefully: one or few types, not in very fast loop, etc

Share this post


Link to post
Share on other sites

Yes the command is demanding but that depends on how you use it and nearestObjects with a huge radius is certainly much worse than that.

Any command that has to iterate over a lot of objects is demanding, so take that wiki-comment with a grain of salt.

 

The main difference is that allMissionObjects only goes through all editor, script or zeus placed objects in a mission, whereas nearestObjects has to take everything on the map into account.

Share this post


Link to post
Share on other sites
3 hours ago, aie-boshell said:

I'm expecting to push it beyond what would allow good performance and then cut corners where necessary to get acceptable performance

 

Good luck with that!

Share this post


Link to post
Share on other sites

@Phronk, BIS_fnc_sortBy is very fast. I'd never use anything else for sorting. That said, in this particular application, what Tajin and Haz proposed is by far the best solution.

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites

On Tanoa , 1 player, one invisible helipad, one red pencil (simple object), vanilla of course.

 

Result:
0.0131 ms

Cycles:
10000/10000

Code:
allSimpleObjects ["land_pencilred_f"]

(slightly depending on the number of simple objects)

 

Result:
38.2593 ms

Cycles:
27/10000

Code:
allMissionObjects "land_pencilred_f" // or "land_helipadEmpty_f"

 

 

 

 

 

 

  • Like 4

Share this post


Link to post
Share on other sites

Jesus Christ, that's insane lmao.  Good to know.

Share this post


Link to post
Share on other sites
On 11/3/2017 at 2:38 AM, phronk said:

I think allMissionObjects isn't as taxing when you're only looking for one type of object, I use it for a garbage collection script I wrote and I haven't noticed any FPS impact at all.  Anyway, for future reference when you feel like learning which commands run faster than the other, this wiki page is a godsend.

I believe "godsend" is a great word for that wiki page.  That page will come very much in handy for my current project.  Thanks again, Phronk!

 

 

 

22 hours ago, Tankbuster said:

Good luck with that!

Thank you, Tankbuster.  I'm going to need it very much, lol.

 

 

 

15 hours ago, pierremgi said:

On Tanoa , 1 player, one invisible helipad, one red pencil (simple object), vanilla of course.

 

Result:
0.0131 ms

Cycles:
10000/10000

Code:
allSimpleObjects ["land_pencilred_f"]

(slightly depending on the number of simple objects)

 

Result:
38.2593 ms

Cycles:
27/10000

Code:
allMissionObjects "land_pencilred_f" // or "land_helipadEmpty_f"

 

 

 

 

 

 

 

WOW!  I don't know what else to say.  Just wow and thank you for sharing this extremely valuable intel that not only I but I'm sure nearly everyone in this community will benefit from.  You are an extremely valuable asset to this awesome community, PierreMGI!  

 

 

 

In conclusion, without trying to sound too much like a thankful drama queen, I feel extremely lucky to be a part of such a unique and beautiful thing as the Arma Editing Community.  For many years I've been reading and studying posts in this forum, as I would start promising projects and then end them when the demands of regular life became too strong to justify the benefits of completing the project.  Only very recently did I get far enough on a project that I would be motivated to start my own threads and the friendly responses I've gotten that are so extremely brilliant and helpful have just about brought a tear to my eye. 

 

I'm just a nerd with a GED from Detroit, Michigan, but with the knowledge I've developed over the years thanks to you, this AMAZING community, I will actually be able to develop my dream Authentic Interactive military gaming experience that I've craved since I first got into online military gaming 15 years ago in 2002. 

 

Here's to this awesome platform we have from the one of a kind Bohemia Interactive Studios, here's to what we've created in this amazing community in the last 15 years, and here's to what we and BIS still have yet to create in the future (drinks shot of Cognac).  

 

Again, please forgive the dramatics, but I really mean every word straight from the heart.  

 

Thank you

 

 

 

 

 

 

 

 

Share this post


Link to post
Share on other sites
1 hour ago, aie-boshell said:

 

Thank you, Tankbuster.  I'm going to need it very much, lol.

 

 

With respect, I think you're going about this back to front. Here's what many would suggest. Make the barebones of the mission and make it run fast and lean. Add features until performance drops below a  threshold, then stop adding features. Much easier and less time consuming than making a behemoth and trying to trim it down.

Share this post


Link to post
Share on other sites
56 minutes ago, Tankbuster said:

With respect, I think you're going about this back to front. Here's what many would suggest. Make the barebones of the mission and make it run fast and lean. Add features until performance drops below a  threshold, then stop adding features. Much easier and less time consuming than making a behemoth and trying to trim it down.

I think you're right, Tankbuster.  The only trouble I'm having is that I cannot decide exactly which features I would like to implement more than others as that would depend on not only the importance of having the feature but also on the effect the feature would have on performance.  With that being said, my current plan is to figure out how to create as many features as possible that I've dreamed about over the years and I'm saving only so many features to each project.  When all is done I will try to combine all of the projects to create the ultimate military gaming experience, but I expect that performance might be bad after I do that so I might end up have to combine my 8 projects of features to 4 separate game modes instead of one, for example, if that makes sense. 

 

I've spent many years, actually 10+, trying to create a playable mission or game mode out of each project before something would stop me whether it was regular life, boredom, or simply coming up with a better game mode idea and moving on.  This time, I think I've finally found my solution to that longtime problem in that I'm focusing on each project as a feature or collection of features then I will combine all or certain features to make one or more epic game modes or missions later.   I hope that makes sense but if you still have info that may help me accomplish my goal I am definitely listening and I appreciate every piece of information you're willing to share with me.  

 

Share this post


Link to post
Share on other sites

Good idea using allSimpleObjects in that case. Of course you could also add all those pencils to an array on init. That'll also be very fast.

 

Makes me wonder though... what are you planning to build anyway and why do you need to find pencils all over the map?  :dontgetit:

 

Share this post


Link to post
Share on other sites
On 11/6/2017 at 4:35 AM, Tajin said:

Good idea using allSimpleObjects in that case. Of course you could also add all those pencils to an array on init. That'll also be very fast.

 

Makes me wonder though... what are you planning to build anyway and why do you need to find pencils all over the map?  :dontgetit:

 

LOL, yes, I should clarify that in case someone has a better idea on how to accomplish it.  This is a sector control type feature where ai units that are spawned in constantly assault the closest enemy sector to their location.  Because "objects" are the only entity I've been able to detect the closest to the unit of, I've created a blu pencil at each blufor controlled sector and a red pencil at each opfor.  Then, I create a waypoint using "addwaypoint" on the nearest blue or red pencil to the unit to make them constantly assault the closest enemy held sector.  Pretty sloppy.  Perhaps there is a better way.

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

×