Home   Help Search Login Register  

Author Topic: function conjunction malfunction  (Read 1655 times)

0 Members and 1 Guest are viewing this topic.

Offline .pablo.

  • Former Staff
  • ****
  • When in doubt, empty the magazine.
function conjunction malfunction
« on: 26 Jul 2003, 00:34:32 »
http://www.rhymezone.com/
rhymes are fun (and they get stuck in people's heads easily)

anyway, heres the problem:
i have a function in which im trying to join together a foreach and an if-then into one process, but it isn't working.  the function is supposed to take an array of objects/men and a single man, and return which object/man is closest to the single man.  heres the code:

Quote
private ["_potentialtargets", "_man", "_closestdistance", "_closesttarget", "_distance"];

_potentialtargets = _this select 0;
_man = _this select 1;

_closestdistance = 999999;

{
_distance = _man distance _x;

If (_distance < _closestdistance) Then
  {
     _closestdistance = _distance;
     _closesttarget = _x;
  }
Else
  {
  };
};
ForEach _potentialtargets;

_closesttarget

i just started making functions yesturday so i don't completely understand how they work yet.  what'd i do wrong?

deaddog

  • Guest
Re:function conjunction malfunction
« Reply #1 on: 26 Jul 2003, 00:41:45 »
I think you need to take a look at my answer to your other post.  That shows the way you use if-then with the foreach command.  

Offline .pablo.

  • Former Staff
  • ****
  • When in doubt, empty the magazine.
Re:function conjunction malfunction
« Reply #2 on: 26 Jul 2003, 00:52:26 »
Quote
The _x in a foreach command has "local" scope.  You can have a _x variable in the other part of your script but it will not be affected by the _x inside the foreach command.


how does that tell me what i did wrong?

the error im getting is unknown operator, pointing at the _potentialtargets; i think im using the _x fine, but idk...

Offline LCD

  • Former Staff
  • ****
    • Everon Cartel
Re:function conjunction malfunction
« Reply #3 on: 26 Jul 2003, 01:07:57 »
how do u pass potential targets ?

btw u dont need da whole else part ;)

Quote
Else
  {
  };

delete em ;D

dere may b somin else - but its 2 late now ;)

LCD OUT
"guess being the community has downsides .." - cheetah
Help Perfecting Da Next Best Thing - O-Team Beta

Offline .pablo.

  • Former Staff
  • ****
  • When in doubt, empty the magazine.
Re:function conjunction malfunction
« Reply #4 on: 26 Jul 2003, 01:15:46 »
Quote
_potentialtargets = _this select 0;
_man = _this select 1;

then i call the function with this line:

Quote
;_target = [_potentialtargets, _guy] call closesttarget

and before that i set:

Quote
_potentialtargets = [man1, a1, a2, a3]

and i originally didn't include the else part but it wasn't working so i thought "whatever"

Offline LCD

  • Former Staff
  • ****
    • Everon Cartel
Re:function conjunction malfunction
« Reply #5 on: 26 Jul 2003, 01:17:46 »
try printing da _potentialtargets thingy w/ da format command ;)

hint format ["%1",_potentialtargets]

in da function - checks if u realy got da array or somin strange else ;)

LCD OUT
"guess being the community has downsides .." - cheetah
Help Perfecting Da Next Best Thing - O-Team Beta

Offline LCD

  • Former Staff
  • ****
    • Everon Cartel
Re:function conjunction malfunction
« Reply #6 on: 26 Jul 2003, 01:23:43 »
just figured wat deaddog meant (shudnt party dat much next time ::) ;))

dis thing is wrong  :-X

Code: [Select]
{
_distance = _man distance _x;

If (_distance < _closestdistance) Then
  {
    _closestdistance = _distance;
    _closesttarget = _x;
  }
Else
  {
  };
};
ForEach _potentialtargets;

change da code 2 somin like dat

"If ((_man distance _x)< _closestdistance) Then
{
    _closestdistance = _man distance _x;
    _closesttarget = _x;
};"ForEach _potentialtargets;

give it a shot nd report ;)

LCD OUT
« Last Edit: 26 Jul 2003, 02:03:16 by LCD »
"guess being the community has downsides .." - cheetah
Help Perfecting Da Next Best Thing - O-Team Beta

Offline .pablo.

  • Former Staff
  • ****
  • When in doubt, empty the magazine.
Re:function conjunction malfunction
« Reply #7 on: 26 Jul 2003, 02:00:31 »
Quote
"If ((_man distance _x)< _closestdistance) Then
{
    _closestdistance = _distance;
    _closesttarget = _x;
};"ForEach _potentialtargets;

works perfect now, just had to change _distance to _man distance _x in this line:
Quote
_closestdistance = _distance;

Offline LCD

  • Former Staff
  • ****
    • Everon Cartel
Re:function conjunction malfunction
« Reply #8 on: 26 Jul 2003, 02:04:59 »
u c ? im genius (nd drunk ;D but nit alot ;))

just qoute me right ::) ;) (look @ ma post again ;))

so solve da topic now it works ;D

LCD OUT

 
"guess being the community has downsides .." - cheetah
Help Perfecting Da Next Best Thing - O-Team Beta