Home   Help Search Login Register  

Author Topic: Quick help with functions please  (Read 513 times)

0 Members and 2 Guests are viewing this topic.

Offline General Barron

  • Former Staff
  • ****
  • Semper Fi!
Quick help with functions please
« on: 28 Sep 2003, 00:43:58 »
I've read the tutes on functions, and my function below seems like it should work, but it doesn't. Can somebody please tell me what's wrong with it?

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

Thanks in advance!
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 CrashDome

  • Members
  • *
Re:Quick help with functions please
« Reply #1 on: 28 Sep 2003, 07:27:00 »
First off, put all the variables listed in the private declaration into ALL lower case letters.
Dont worry about the rest of the function as case doesn't matter. It only matters with the "private" command.

Secondly, what is your error? or better yet.. how do you know it doesn't work? Is it the value returned or is it not running at all? What are you doing to test it? How do you call it? etc...

need a bit more info.

Offline General Barron

  • Former Staff
  • ****
  • Semper Fi!
Re:Quick help with functions please
« Reply #2 on: 28 Sep 2003, 08:37:11 »
Okay, I tried changing the variables to all lowercase, but to no effect. Do all variables with the "local" command have to be in lowercase?

Part of the problem is that I can't see what the error is. I get an error message that appears to be the whole do-while part of the script, but it is too long for me to see what exactly is the error.

I'll attach my mission file so you can see for yourself.

Basically this is an improved version of the nearestobject command. Instead of finding the nearest object of one particular type, it will find the nearest object out of a list of types you provide. For example, you could find the nearest tank by providing an array of all tank types used in your mission.

It is called as such: [object, [type array]] call "nearestObject.sqf"

An example type array: ["SoldierWB","SoldierEB"]
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!

Unnamed

  • Guest
Re:Quick help with functions please
« Reply #3 on: 28 Sep 2003, 10:26:39 »
You called your function NearestObject, this is the same name as the command NearestObject so it does not like that at all. Just call your function NearestType or something.

A couple of other things:

To help with debugging long error messages, try splitting the code up into smaller chunks.

Add this at the top of your code:

Code: [Select]
_Debug1={_object = [_center, _objectlist select _i];_dist = _center distance _object;};
Then change:

Code: [Select]
Do
 {
  _object = [_center, _objectlist select _i];
  _dist = _center distance _object;

If (_dist <= _lowestdist) Then

To:

Code: [Select]
Do
 {
 
 Call _Debug1;

If (_dist <= _lowestdist) Then

I'm sure you will spot this once your up and running and it's easy to fix. But if you are playing as type "SoldierWB", and you ask for the nearest "SoldierWB", you will return yourself as the nearest object.

Offline CrashDome

  • Members
  • *
Re:Quick help with functions please
« Reply #4 on: 28 Sep 2003, 18:49:36 »
ok... I just spent 30mins typing and I got a "page not found error" and lost my entire post.

Anyway, I fixed it....
Here's the run down:

A) must pre-define _objectList as an array before putting _this select 1 into it (_objectList = [];) or the "count _objectList" will not work

B) You have a unit of same type as callee. This means that "SoldierWB" will always return "player" to be nearest because of BIS NearestObject command.

C) defined _closestObj as objNull in declarations to be safe

I added two different types of units... move em around, your script works wonderful now. Enjoy.

oh.. and yes the "private" command must contain all lowercase variables - it's a bug. But, you do not have to leave them lowercase in the rest of your scripts/functions.
« Last Edit: 28 Sep 2003, 18:51:20 by CrashDome »

Offline General Barron

  • Former Staff
  • ****
  • Semper Fi!
Re:Quick help with functions please
« Reply #5 on: 28 Sep 2003, 20:46:34 »
Hey it works! Thanks a lot for helping me with my first function!
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!