Home   Help Search Login Register  

Author Topic: FindClosest.sqf trouble  (Read 951 times)

0 Members and 1 Guest are viewing this topic.

Offline bdfy85

  • Contributing Member
  • **
FindClosest.sqf trouble
« on: 12 Sep 2005, 00:19:34 »
I need function that will return the closest unit from an array to specified unit. Of course , I can use GB's FindUnits , but I can manage with much  simplier one.
I wrote function that seems to be OK, but is unworky for some reason :(
Can anyone point me at my mistake ? I 'm pretty tired messing around with this.
Simple mission with sqf included. Press 0-0-1 to run function.  
   
 
« Last Edit: 12 Sep 2005, 01:04:31 by bdfy85 »
Liberation Mod scripts&balance

UNN

  • Guest
Re:FindClosest.sqf trouble
« Reply #1 on: 12 Sep 2005, 00:42:44 »
Hi,

It's because your not telling OFP what to return in the function. You need to remove the semi-colon here at the end:

Code: [Select]
_closest;
Change it to:

Code: [Select]
_closest
That way OFP knows to return that value from the function.

Just out of curiosity, about this bit.

Code: [Select]
{if (_x != _unit) then {_found = _found + [_x]}} foreach _list;
I think you can just use:

Code: [Select]
_Found=+(_List-[_unit])
You could also use the foreach loop instead of while:

Code: [Select]
_minDist=900;
_Closest=ObjNull;
{_Dist=_Unit Distance _x ; If (_Dist<_minDist) Then {_minDist=_Dist ; _Closest=_x}} ForEach _Found;

Offline bdfy85

  • Contributing Member
  • **
Re:FindClosest.sqf trouble
« Reply #2 on: 12 Sep 2005, 01:06:49 »
UNN
I don't know why , but with all the fixes function keeps producing strange errors :(
Can you do me a favor and finish this sqf/made a new one ?

*EDIT*
Oops.. I got it - very stupid mistake :)
10x for your help , UNN ;)
« Last Edit: 12 Sep 2005, 01:21:43 by bdfy85 »
Liberation Mod scripts&balance

Offline bdfy85

  • Contributing Member
  • **
Re:FindClosest.sqf trouble
« Reply #3 on: 12 Sep 2005, 01:44:17 »
oops again... :(  May be I'm too asleep or what - function is worky , but only once ( it returns nothing when called twice ) for some unknown reason :( what's the ...?
« Last Edit: 12 Sep 2005, 01:46:37 by bdfy85 »
Liberation Mod scripts&balance

Offline Fragorl

  • Coding Team
  • Former Staff
  • ****
Re:FindClosest.sqf trouble
« Reply #4 on: 12 Sep 2005, 02:22:12 »
afaik, you need semicolons after these two statements (correct syntax shown):

{_Dist=_Unit Distance _x ; If (_Dist<_minDist) Then {_minDist=_Dist ; _Closest=_x};} ForEach _Found;
{if (_x != _unit AND alive _x) then {_found = _found + [_x]};} foreach _list;

(ofp functions are a bit inconsistent when it comes to expecting semicolons, but generally you must always put them at the end of an if, or at the end of an if-else clause). E.g.

Code: [Select]
if (condition) then {
    ...;
    ...;
};
Code: [Select]
if (condition) then {
    ...;
    ...;
} else {
    ...;
};
Note that you can drop the semicolon if the 'if' consists of one statement only, AND the one statement has a semicolon:

Code: [Select]
if (_myvar == 3) then {
    hint "var was 3";
}

I notice that in 'init.sqs' you say -- find=PreprocessFile "func\findClosest.sqf" -- yet in your most recent mission, that directory doesn't exist. Although that wouldn't explain why the function runs correctly once. Still, either of these things could be the problem (not near ofp so i can't test).
 




Offline bdfy85

  • Contributing Member
  • **
Re:FindClosest.sqf trouble
« Reply #5 on: 12 Sep 2005, 03:03:40 »
Even more : I tried running aroun the place - sometimes sqf works fine, sometimes not :( And I don't know why.  
Liberation Mod scripts&balance

UNN

  • Guest
Re:FindClosest.sqf trouble
« Reply #6 on: 12 Sep 2005, 04:55:58 »
Quote
afaik, you need semicolons after these two statements (correct syntax shown):

Not when it's the last command in {}. Either way, wont throw up any errors, so it's down to preference.

Quote
Even more : I tried running aroun the place - sometimes sqf works fine, sometimes not  And I don't know why.

_MinDist has to be some value that you know will always be greater than the distance in your group. I just change it to:

Code: [Select]
_minDist = 9999999;
The mission I tested seems to work ok now, I've attached the updated version.  

Offline bdfy85

  • Contributing Member
  • **
Re:FindClosest.sqf trouble
« Reply #7 on: 12 Sep 2005, 10:18:21 »
UNN
10x It works finally :)
submited to ED.
Liberation Mod scripts&balance