Jump to content
Sign in to follow this  
Lucky44

GetNearestObject: how to find the closest player to an object???

Recommended Posts

I've searched all over and can't find anything close to this. (That probably means I'm going about it all wrong!)

All I want to do is find the nearest PLAYER to an object. I've got an Action on the object, and when a player "uses" the object/action, I want to play (force) an animation on the player. I know how to do the animation, but I am having trouble finding the player unit name.

What am I missing?

Thanks in advance.

Share this post


Link to post
Share on other sites

Getnearestobject can only find types(classes). The AI wont use the action unless called by a script. So you could probably use

_myobject = nearestObject [objectname, "Man"];

Although i am lazy and havent tested this.

Share this post


Link to post
Share on other sites
Getnearestobject can only find types(classes). The AI wont use the action unless called by a script. So you could probably use

_myobject = nearestObject [objectname, "Man"];

Although i am lazy and havent tested this.

OK, thanks. I'll try that. EDIT: that works, so far. I'm only worried about whether any other unit that's a "man" could be closer and mess things up.

Can you not use "player" as a class?

Edited by Lucky44

Share this post


Link to post
Share on other sites

see here

The player isnt a class, but i might work. But you can also browse the classnames provided in the link above, and change the classname to the same as the player. Like if the player is an insurgent medic, it would be

_myobject = nearestObject [objectname, "Ins_Soldier_Medic"];
. It would reduce the risk of other AI interfering.(In this example, only an AI insurgent medic wich is closer to the object than the player, could ruin the addaction.)

But this solution is not the optimal overall, i hope someone else chimes in with a better solution. But this will work, just with a risk of occasional fuckups.

Edited by Jelliz

Share this post


Link to post
Share on other sites

OK, thanks. I am making this for just 4 (possible) players, so I could give them names and do 4 "if" statements.

On the other hand, maybe I'm worried unnecessarily. What I'm doing is "breaking down" a static MG to load it for transport. Maybe the closest unit to the MG is the one who should be doing the work anyway!

(But I would like to know the easiest/safest way to find the closest PLAYER to an object anyway!)

Share this post


Link to post
Share on other sites

Reference all the players in an array, and then check the object found against the array?

eg...

_myobject = nearestObject [objectname, "Man"];
if (_myobject in MyBigArrayOfPlayers) then
{
// Pop your codey stuff here
};

Share this post


Link to post
Share on other sites

You say you have an action on the object, if so why not use that to identify the man.

_obj = _this select 0;

_man = _this select 1;

Share this post


Link to post
Share on other sites
You say you have an action on the object, if so why not use that to identify the man.

_obj = _this select 0;

_man = _this select 1;

OK, would those two lines go in the script called by the action? I'm just not clear on what "_this" will refer to here.

---------- Post added at 04:31 PM ---------- Previous post was at 04:25 PM ----------

Reference all the players in an array, and then check the object found against the array?

eg...

_myobject = nearestObject [objectname, "Man"];
if (_myobject in MyBigArrayOfPlayers) then
{
// Pop your codey stuff here
};

That looks like an interesting approach to check to see if the "man" involved is a player. I can use that some places. Thanks.

Share this post


Link to post
Share on other sites
OK, would those two lines go in the script called by the action? I'm just not clear on what "_this" will refer to here.

The variable "_this" at the beginning of the script always refer to the passed variables. Mostly when calling a script you have a line like this:

nul = [here, there, anywhere] execVM "anyscript.sqf"

The square brackets part is an array holding several variables. Those are passed to the script.

_var1 = _this select 0;
_var2 = _this select 1;
_var3 = _this select 2;

These lines does read the variables out of the passed array. You could also just read the array and split it up inside the script:

_myArray = _this;
_var1 = _myArray select 0;
_var2 = _myArray select 1;
_var3 = _myArray select 2;

The result is that _var1 to _var3 now holds the parameters passed by the scriptcall.

In case of using a action, a array is passed by default containing who is the owner of the action (was attached to via addAction) and who finally activated the action.

so:

_unit = _this select 1;

will always refer to the unit that used the action.

Share this post


Link to post
Share on other sites

Thanks F2k_Sel and Myke for taking the time to help with that. I get it now.

I'm still just learning coding, and I love it! I just wish I knew more. -Practice practice practice!

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  

×