Advertisement

Author Topic: Strange 'Scope' Error  (Read 463 times)

0 Members and 1 Guest are viewing this topic.

Offline Rommel92

  • Members
  • *
Strange 'Scope' Error
« on: 28 Mar 2009, 04:36:58 »
Code: (marker network) [Select]
///////////////////////
////VARIABLES DECLARATION
///////////////////////

private ["_ca","_ta","_sa","_as","_i"];

_ca = nearestlocations [[0,0,0], ["namecitycapital"], 1*10^10];
_ta = nearestlocations [[0,0,0], ["namecity"], 1*10^10];
_sa = nearestlocations [[0,0,0], ["namevillage"], 1*10^10];

call {
private ["_c","_s"];
_c = (configfile >> "cfgworlds" >> worldname);
_s = getarray(_c >> "ilsposition");
_c = (configfile >> "cfgworlds" >> worldname >> "secondaryairports");

_as = []; _as set [count _as,_s];

while {count _as <= count _c} do {
_s = getarray(_c select ((count _as) - 1) >> "ilsposition");
_as set [count _as,_s];
};
};

_i = 0;

///////////////////////
////PROCESSING OF DATA
///////////////////////

{
private ["_l","_n","_t","_m","_v"];
_l = locationposition _x;
_n = _ca select 0;

{
private ["_d"];
_d = (locationposition _x) distance _l;
_v = (locationposition _n) distance _l;
if (_d < _v) then {_n = _x};
} foreach _ca;

_t = ((_l select 0)-(locationposition _n select 0)) atan2 ((_l select 1)-(locationposition _n select 1));
_v = (locationposition _n) distance _l;

_m = createmarkerlocal [str(text _x)+ str(_i), locationposition _n];
_m setmarkerposlocal [sin (_t)*(_v/2) + (locationposition _n select 0), cos(_t)*(_v/2) + (locationposition _n select 1)];
_m setmarkershapelocal "rectangle"; _m setmarkerbrushlocal "solid"; _m setmarkercolorlocal "coloryellow";
_m setmarkerdirlocal _t; _m setmarkersizelocal [20, (_v/2)];

_i = _i + 1;
} foreach _ta;

{
private ["_l","_n","_t","_m","_v"];
_l = locationposition _x;
_n = (_ca + _ta) select 0;

{
private ["_d"];
_d = (locationposition _x) distance _l;
_v = (locationposition _n) distance _l;
if (_d < _v) then {_n = _x};
} foreach (_ca + _ta);

_t = ((_l select 0)-(locationposition _n select 0)) atan2 ((_l select 1)-(locationposition _n select 1));
_v = (locationposition _n) distance _l;

_m = createmarkerlocal [str(text _x)+ str(_i), locationposition _n];
_m setmarkerposlocal [sin (_t)*(_v/2) + (locationposition _n select 0), cos(_t)*(_v/2) + (locationposition _n select 1)];
_m setmarkershapelocal "rectangle"; _m setmarkerbrushlocal "solid"; _m setmarkercolorlocal "colorblue";
_m setmarkerdirlocal _t; _m setmarkersizelocal [10, (_v/2)];

_i = _i + 1;
} foreach _sa;

hint str(_as);
{
private ["_l","_n","_t","_m","_v"];
_l = _x;
_n = (_ca + _ta + _sa) select 0;

{
private ["_d"];
_d = (locationposition _x) distance _l;
_v = (locationposition _n) distance _l;
if (_d < _v) then {_n = _x};
} foreach (_ca + _ta + _sa);

_t = ((_l select 0)-(locationposition _n select 0)) atan2 ((_l select 1)-(locationposition _n select 1));
_v = (locationposition _n) distance _l;

_m = createmarkerlocal ["mkrAIR_"+ str(_i), locationposition _n];
_m setmarkerposlocal [sin (_t)*(_v/2) + (locationposition _n select 0), cos(_t)*(_v/2) + (locationposition _n select 1)];
_m setmarkershapelocal "rectangle"; _m setmarkerbrushlocal "solid"; _m setmarkercolorlocal "colorred";
_m setmarkerdirlocal _t; _m setmarkersizelocal [10,(_v/2)];

_i = _i + 1;
} foreach _as;



--------------

The Problem

This 'as is' works perfect, however that was only after adding:
Code: [Select]
_v = (locationposition _n) distance _l;after each foreach loop.

_n = nearest
_l = location

Now what would happen if the foreach was not there, was the very last few markers (linked to Masbete, the last of the _ta array) would move off to a random location. This would also occur to all the markers linked to Bagango, (the last of the _ca array).

What was strange is that it would only occur on the last town if it is closest, and even though the:
Code: [Select]
_v = (locationposition _n) distance _l;Inside the foreach loop was being declared and _n is being found by this distance, it comes up outside the loop as a random number between 1 and ten thousand. I then found by re-defining _v it would find the actual distance, but that seems strange considering it had found the distance moments before via foreach loop with _n...
« Last Edit: 28 Mar 2009, 04:42:59 by Rommel92 »

Offline i0n0s

  • Former Staff
  • ****
Re: Strange 'Scope' Error
« Reply #1 on: 28 Mar 2009, 09:43:22 »
 :D
Found it.
_v is the smallest distance between _n and _l, but only as long as the if-statement don't get true. At that point it is the second smallest distance and not the smallest.
Code: [Select]
_v = (locationposition _n) distance _l;
{
private ["_d"];
_d = (locationposition _x) distance _l;
if (_d < _v) then {
_n = _x;
_v = _d;
};
} foreach (_ca + _ta + _sa);

Btw:
Is there any reason for the first call?

Offline Rommel92

  • Members
  • *
Re: Strange 'Scope' Error
« Reply #2 on: 29 Mar 2009, 04:25:16 »
Quote
:D
Found it.

Ah!!!

Thanks a tonne, saved my brain from exploding.  ;)

note: The call is there for no reason, except to null those variables c and s as they are used later, which can be done 1000 other ways, but I was just lazy.  :P


#EDIT: Don't quote the entire previous post you're replying to..   h-
« Last Edit: 29 Mar 2009, 07:16:58 by h- »