Home   Help Search Login Register  

Author Topic: Nearestobject, I hate you!  (Read 1146 times)

0 Members and 1 Guest are viewing this topic.

KyleSarnik

  • Guest
Nearestobject, I hate you!
« on: 26 Nov 2003, 04:49:51 »
Code: [Select]
_target = nearestobject [_unit,"????"]
is there any way to use a base class, like say "Man", and get the closest soldier? Or do I have to predifine some soldier types?

KyleSarnik

  • Guest
Re:Nearestobject, I hate you!
« Reply #1 on: 26 Nov 2003, 18:13:21 »
Ok maybe nearestobject isn't the right command for this situation. I need a way (in a script) that can find the closest enemy (enemy is west) soldier. I need to detect him and assign him to a local name, _target... if it isn't possible to detect any class of soldlier, I guess I could make an array of classes, but it would be alot better to just get the closest enemy soldier... So any ideas?

deaddog

  • Guest
Re:Nearestobject, I hate you!
« Reply #2 on: 26 Nov 2003, 18:35:11 »
You need an array of all enemy units.  Make a large "present" trigger and in the activation field put:

enemyunits=+thislist

Then in a script:

_dis=999999
_closest=0

{_d=_unit distance _x;if (_d<_dis) then {_dis=_d;_closest=_x}} foreach enemyunits

?_dis < 100 (or whatever):_unit dotarget _closest

_unit is the name of the unit you want to find the closest enemy to.

You can put this in a loop if you want.  Also, if you respawn an enemy group then do this:

enemyunits=enemyunits + (units groupname)


I use this technique a lot to find whatever is closest.



KyleSarnik

  • Guest
Re:Nearestobject, I hate you!
« Reply #3 on: 26 Nov 2003, 19:35:27 »
No, I can't use a trigger, it all has to be in a script. The simplest way to put it is, I need _target to be equal to the west soldier closest to _unit, and can be repeated...

deaddog

  • Guest
Re:Nearestobject, I hate you!
« Reply #4 on: 26 Nov 2003, 19:55:47 »
The trigger is only used at the beginning of the mission.  It has to be large enough to cover the entire map.  It is used to obtain a list of all the enemy units.

After that, the script just simply checks each entry in the enemyunits array to see how close they are to the unit.


Code: [Select]
;*** targetclosest.sqs

_unit = _this select 0

#loop
~5

_dis=999999
_target=0

{_d=_unit distance _x;if (_d<_dis) then {_dis=_d;_target=_x}} foreach enemyunits

?_dis < 100:_unit dotarget _target

goto "loop"

Run the script like this:  [unitname] exec "targetclosest.sqs"

This will automatically target the closest enemy unit under 100 meters, every 5 seconds.  Adjust as necessary.


Offline CrashDome

  • Members
  • *
Re:Nearestobject, I hate you!
« Reply #5 on: 26 Nov 2003, 20:50:56 »
and if the enemy spawns or respawns?

deaddog

  • Guest
Re:Nearestobject, I hate you!
« Reply #6 on: 26 Nov 2003, 21:26:58 »
You didn't fully read my first post :)

Quote
You can put this in a loop if you want.  Also, if you respawn an enemy group then do this:

enemyunits=enemyunits + (units groupname)

KyleSarnik

  • Guest
Re:Nearestobject, I hate you!
« Reply #7 on: 26 Nov 2003, 21:50:50 »
Hmm... That worked alot better than I thought it would, its good enough, thx for the help deaddog...

*Solved*