Home   Help Search Login Register  

Author Topic: Can 'nearestobject' be used in functions?  (Read 685 times)

0 Members and 1 Guest are viewing this topic.

Offline General Barron

  • Former Staff
  • ****
  • Semper Fi!
Can 'nearestobject' be used in functions?
« on: 28 Jul 2003, 23:00:02 »
Pretty simple question. I am trying to write my first function, and it involves using the 'nearestobject' command. After a lot of frustration wondering what I was doing wrong, I did a little test, and made a small function that basically says "_return = nearestobject [player, "SoldierWB"]". When I run it, I get an error "unknown function". I think I read somewhere that some game-functions don't work in functions. Does anybody know if that is true/what functions don't work?

Thanks.
HANDSIGNALS COMMAND SYSTEM-- A realistic squad-control modification for OFP
kexp.org-- The best radio station in the world, right here at home! Listen to John Richards!

Offline Igor Drukov

  • Contributing Member
  • **
  • Conscientious Subjector
Re:Can 'nearestobject' be used in functions?
« Reply #1 on: 29 Jul 2003, 00:40:43 »
Hello General  :wave: !

Your question is quite interesting and... I have an answer !

NearestObject and NearestBuilding DO work in functions, and to test it I created this nice lil' .sqf :

Code: [Select]
private ["_obj0","_obj1","_string","_return"];
_obj0=_this select 0;
_obj1=_this select 1;
_string=format ["%1",typeof _obj1];
_return=nearestobject [_obj0,_string];
hint format ["%1",_return]

This function returns the nearest object to _obj0 of the same type as _obj1.
Example : [player, myflag] call ClosestObj will return the closest flag to the player. Handy, isn't it ?

Anyway, your "unknown function" message has got to be some kind of typing mistake, for sure.

As to the rest, I don't know commands that DON'T work in .sqf.

 :cheers:

Offline toadlife

  • OFPEC Old Skool
  • Former Staff
  • ****
  • Official OFP Editing Center Toad
    • toadlife.net
Re:Can 'nearestobject' be used in functions?
« Reply #2 on: 29 Jul 2003, 00:57:03 »
Time means nothing within a function (time is stopped during the execution of a function), and therefore, every command that relies on time will not work in a function....

These are things that will not work in a function...
Code: [Select]
~10 <--relies on the passage of time

@condition = 5 <--relies on the passage of time

goto "blah"  <-labels and jumping around is not allowed

while {_time < 1}  <--relies on the passage of time

Anything other than these examples should work just fine within a function.

Also, a function can do no more than 10000 iterations, ie...

This will work:
Code: [Select]
_c = 1
while {_c < 10000} do {blah blah;_c = _c + 1}

This will give an "Generic Error in Expression" error:
Code: [Select]
_c = 1
while {_c < 10001} do {blah blah;_c = _c + 1}
"Whenever you want information on the 'net, don't ask a question; just post a wrong answer." -- Cancer Omega.

Offline General Barron

  • Former Staff
  • ****
  • Semper Fi!
Re:Can 'nearestobject' be used in functions?
« Reply #3 on: 29 Jul 2003, 06:54:01 »
Thanks for your replies, gentlemen. That's what I was hoping, that it was an error on my part! That means that my function could work.

Now, if you would be so kind, would one of you please tell me what I am doing wrong with my code? Like I said, this is my first time writing a function, so I'm not surprised that I have some mistakes.

What I'm trying to make is an improved "nearestobject" function. Basically, it will find the nearest object out of an array of object types, instead of just finding the nearest object of just one type. Hence, instead of just saying: nearestobject [player, "SoldierWB"], you could say: [player, ["SoldierWB","SoldierEB","SoldierGB"]] call nearestobject. Sounds good, right? It's about as close to a nearestobject [unit,class] as you can get in OFP1. Anyway, here is the code:

Code: [Select]
private ["_center", "_objectList", "_lowestDist", "_i", "_max", "_object", "_dist", "_closestObject"];

_center = _this select 0;
_objectList = _this select 1;
_lowestDist = 99999;
_i = 0;
_max = count _objectList;

While {_i < _max}
Do
 {
  _object = nearestObject [_center, _objectList select _i];
  _dist = _center distance _object;
 
If (_dist <= _lowestDist) Then
  {
    _lowestDist = _dist;
    _closestObject = _object;
  };

  _i = _i + 1;
 };

_closestObject

Any help would be greatly appreciated! ::)
HANDSIGNALS COMMAND SYSTEM-- A realistic squad-control modification for OFP
kexp.org-- The best radio station in the world, right here at home! Listen to John Richards!

Offline toadlife

  • OFPEC Old Skool
  • Former Staff
  • ****
  • Official OFP Editing Center Toad
    • toadlife.net
Re:Can 'nearestobject' be used in functions?
« Reply #4 on: 29 Jul 2003, 21:07:52 »
Try consolidating it into one line and making sure you have spaces between your commands.

Code: [Select]
While {_i < _max} Do {_object = nearestObject [_center, _objectList select _i];_dist = _center distance_object;If (_dist <= _lowestDist) Then {_lowestDist = _dist;_closestObject = _object;};_i = _i + 1;};
_closestObject
"Whenever you want information on the 'net, don't ask a question; just post a wrong answer." -- Cancer Omega.

onepremise

  • Guest
Re:Can 'nearestobject' be used in functions?
« Reply #5 on: 30 Jul 2003, 00:59:55 »
Quote
While {_i < _max} Do {_object = nearestObject [_center, _objectList select _i];_dist = _center distance_object;If (_dist <= _lowestDist) Then {_lowestDist = _dist;_closestObject = _object;};_i = _i + 1;};
_closestObject

Just curious, but what advantage or benefit do you get outa making this fit into one line?