Home   Help Search Login Register  

Author Topic: Order to move to a nearest car from the array?  (Read 3233 times)

0 Members and 1 Guest are viewing this topic.

Offline haroon1992

  • Members
  • *
  • My life is hopeless...
Order to move to a nearest car from the array?
« on: 03 Jun 2010, 13:08:47 »
there are many objecst in an array.
And what i wanted to find is the nearest object in the array to the specific person.

cars=[truck1,lorry1,car1,skoda1];

and how can i find the nearest car to a unit from the array "cars"?

and how can i order the unit to move to that nearest car?


I think i've asked for a similar question in the past.
But this times its for the mission I am currently doing.

A civilian comes and picks a random car in the car park.
(The embarkation and other things are not to be considered now)


Thanks in advance................
Very busy with life, business, and other stuff. Away from OFP for months. Not sure if I could get back onto it. :(

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: Order to move to a nearest car from the array?
« Reply #1 on: 03 Jun 2010, 14:34:06 »
Insert the BIS functions module into the mission and call BIS_fnc_nearestPosition..

Code: [Select]
_nearestPos = [_cars,_person] call BIS_fnc_nearestPosition;
_person doMove _nearestPos;

Should work.. :dunno:
« Last Edit: 03 Jun 2010, 14:35:53 by h- »
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline haroon1992

  • Members
  • *
  • My life is hopeless...
Re: Order to move to a nearest car from the array?
« Reply #2 on: 04 Jun 2010, 05:21:59 »
What?
Is that this simple?

But I don't want to use any modules...(I think make problems)

But now it's okay...
Yesterday, I learnt a new command called "min" and "Max"

And  i used them to test and the script is working.......
I will post it here later.
EDIT :
I have the script running,
And I have to compile it to use.
For example,
Chk_Dis=compile preprocessfile "script.sqf"

OR
variable=[arguments] call compile "script.sqf"

Is it a good idea to call the script every 0.2 seconds or less?
(Because I am going to apply it to my zombie script too,man1 as the zombie and objects as the victims)

The following is what I actually want to do :

While {alive zombie} do
{
_vic=[_zom,_Victimarray] call compile "script.sqf";
_zom move getpos _vic;
sleep 0.2;
}
Will it lag the high performance system?
(My system is crap and it lags all the time while playing ArmA 2)
« Last Edit: 04 Jun 2010, 05:59:44 by haroon1992 »
Very busy with life, business, and other stuff. Away from OFP for months. Not sure if I could get back onto it. :(

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: Order to move to a nearest car from the array?
« Reply #3 on: 04 Jun 2010, 11:34:10 »
Quote
But I don't want to use any modules...(I think make problems)
Why would it cause any problems ???
It's a module full of all kinds of useful functions, all the module does it preprocesses them on mission init, exactly like when you write your own function and preprocess it.

Although it's of course good for learning purposes to write your own I myself would be way too lazy to do that if there was a function already available in the vanilla game doing exactly what I need  :P

Quote
OR
variable=[arguments] call compile "script.sqf"
No, do not use that kind of syntax...
The whole point of functions is that they are preprocessed before using them.

Use
Code: [Select]
Chk_Dis=compile preprocessFileLineNumbers "script.sqf"because that gives more detailed error report.

Also, remember to add -showScriptErrors in your Arma2 shortcut target line, otherwise you won't see much of scripting errors displayed on the screen
Code: [Select]
D:\Games\Arma2\ArmA2.exe -nosplash -showscripterrors If you're using some launcher app that probably adds that already..

Quote
Is it a good idea to call the script every 0.2 seconds or less?
Not really.
Especially if you run the same script calling that function on each separate zombie..

And it's not a script you're calling, it's a function. Even though they share the same file extension the method of execution defines which one it is and when using call commands like sleep don't work (cause errors) because funtions can't be paused.
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline haroon1992

  • Members
  • *
  • My life is hopeless...
Re: Order to move to a nearest car from the array?
« Reply #4 on: 07 Jun 2010, 12:19:31 »
Using BIS Modules
I think using them requires the Functions Module to be placed in the editor.
That's why I don't want to use it.And from what you've said, it is checking the objects on the entire map.
I wanted to check the object only if there are within a specified distance.   :D
------------------------
-showscripterrors
I've been using that thing from the day that I've started to script in ArmA 2 :)
(And sometimes it won't show errors like : '{' is missing )  :whistle:

I compiled the function using :

Quote
H_Check_Dis=compile preprocessFile "script.sqf"

What's the difference between preprocessFile and preprocessFileLineNumbers?  :blink:
--------------
sleep command
The function is at the bottom of this post.
Note that I've not added any sleep commands in it.  ;)
-------------
Testing the function :
The array contained about 46 us soldiers(listed using trigger, and yes,I've put a delay of 2 secs in the trigger which calls the script) and the object was a civilian put in the middle of those us men.
The zombie man (civilian) was taking a while before he got the nearest victim.
And sometimes he act like he's stuck(turning around standing at the same position)
The function is to be called every 2 seconds via my extra long main zombie script.
(that main script was run by ALL the zombies)

And I've got a problem :

The problem is that it is taking a bit long for the zombies to check the nearest victim.
And what should I do to fix that prob?

The syntax i use for calling the function is :

Quote
_h_victim=[_h_zom,_h_victimarray] call H_Check_Dis;


Note :  h_victim is the current victim for _h_zom while _h_victimarray being the array of victims

here is the actual function

One can define the radius for the check with my Function.  :D

Quote
//Pass an object and an array of objects and the checking radius, the result is the
//object in the array which is nearest to the object.
//
//eg  : [man1,[car1,truck1,plane1],400] CALL H_Func_NearestToObject
// If car1 is nearest to man1 and is in the radius, result will be car1.


private ["_i"];
_i=0;
_output="";
_loop=0;
_radius=0;
_min=0;
_dist=0;
_filtered=0;

_unit   =   _this select 0;
_array   =   _this select 1;
_radius   =   _this select 2;
if (isNil "_array") then {_array=nearestObjects [_unit,["MAN"],50];
if (isNil "_radius") then {_radius=60};

   //For Loop until _i matches the _array's element count/room count
   for [{_i=0},{_i < count _array},{_i=_i+1}] do
   {
      _cur_element_dis=(_array select _i) distance _unit;
      if (_cur_element_dis < _radius) then
      {
         _min=_radius min _cur_element_dis;
         if (_min < _radius) then
         {
                            
            _radius=_cur_element_dis;
            _filtered=_i;
            }
      };
                //Debugging...
      //hintSilent format ["Min : %1\nMax : %2\nCur_Element_Dis : %3\nVictim : %4\nElement: %5(Name: %6)\nLoop %7",_min,_radius,_cur_element_dis,_output,_i,_array select _i,_loop];

   };
if (not (isNull (_array select _filtered))) then
{
   _output= _array select _filtered;
};

_output;

What i am concerned about is:

is my script written in the proper way?
Can i make a change to the script so that it's gives out performance?
I found no errors with the isnull,but I was curious if it is written correctly...,is it?
« Last Edit: 07 Jun 2010, 12:41:18 by haroon1992 »
Very busy with life, business, and other stuff. Away from OFP for months. Not sure if I could get back onto it. :(

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: Order to move to a nearest car from the array?
« Reply #5 on: 07 Jun 2010, 13:22:58 »
Quote
I think using them requires the Functions Module to be placed in the editor.
People have to insert all kinds of things in the mission when making it so it shouldn't be a stretch ;)

And you can preprocess the BIS functions in your script init too. You just need to check the filepath to the function in question and use that, for example
Code: [Select]
BIS_fnc_nearestPosition = compile preprocessFileLineNumbers "\modules\functions\misc\fn_nearestPosition.sqf";
Quote
it is checking the objects on the entire map.
No, it is checking the array you give it ;)
So, in case of wanting to limited radius you can write your script so that the array includes only things in the given radius and then calls the function..

Quote
What's the difference between preprocessFile and preprocessFileLineNumbers
Like I said, preprocessFileLineNumbers gives more detailed error output in case something is not working.

Quote
is my script written in the proper way?
Your function shouldn't even work because if you put ; in the end of the last line it should not return any value. The last line of your function should be _output without the semicolon.

Don't know about the isNull, I have always used isNil myself..

Your private array must include all the local variables in the function.
And you don't need to set _i = 0 in the beginning of the function because the for/do loop sets it.
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline haroon1992

  • Members
  • *
  • My life is hopeless...
Re: Order to move to a nearest car from the array?
« Reply #6 on: 07 Jun 2010, 14:07:47 »
it is checking the objects on the entire map.
No, i meant that the function will check the objects listed in the array no matter where they are at.
_outpus;
But I received the value(_output) in the testing mission. (I will remove that semicolon  )

Private
I was and am still confused about the private command.
It says it makes the variables local in the current scope.
What exactly does scope means? The whole function.sqf ?

locals in private
I have not put all the local variables in my functions in their respective private arrays.
Should I put them all?
as far as i remember ,the for each loop was not running if i didn't declare _i=0
(I've defined them at the start of the funtions. like _i=0) LoL
In the main script, what will happen if i put _h_victim in the private array?
_h_victim is in the arguments array
to be exact :
Quote
_h_victim=_this select 1;

Regards,
haroon1992
Very busy with life, business, and other stuff. Away from OFP for months. Not sure if I could get back onto it. :(

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: Order to move to a nearest car from the array?
« Reply #7 on: 07 Jun 2010, 16:19:51 »
Quote
What exactly does scope means? The whole function.sqf ?
Each script you execute, be it sqs or sqf is executed in it's own local scope (hence the term local variable for example). I can't really explain scope that good, but imagine it being what the word means, a tube. One script is always in it's own tube. I think scope is also called variable space or something which can a bit clearer term..

But when you call a function and even though it does get a scope of it's own it shares the scope with the script calling the function, and so you need to privatise the variables in the function or else they "leak" into the script calling it.
If you happen to have same named variables both in the script and the function after the function is called you may completely screw up the rest of the script.

You can search around the forums here, this has come up in the past and someone probably does way better in explaining this than I do..
For example in this recent post there's some talk about private. Explaining some of the reasons for it's usage.

Quote
Should I put them all?
That is the general practise, note the difference here though: sqf script (which is executed with either execVM or spawn) doesn't necessarily need the private array to function properly (it depends how you have written your script I guess) but you may run into trouble if you don't use it. Read the above linked post.

In functions (executed with call or loadFile) you must always put all your local variables in the private array.

Quote
In the main script, what will happen if i put _h_victim in the private array?
As said in the post I linked to it ensures that the variable is available throughout the scope of the script. No harm will come to you if you do it ;)
« Last Edit: 07 Jun 2010, 16:21:58 by h- »
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline haroon1992

  • Members
  • *
  • My life is hopeless...
Re: Order to move to a nearest car from the array?
« Reply #8 on: 07 Jun 2010, 17:56:37 »
I now understands what private is ... :)
I've been willing to know about it for a long time... :yes:
Thanks... :clap:
But i still have a doubt.. :scratch:

I used the variable _h_victim to CALL the function.
And I think the variable _output will be assigned to _h_victim.
(i.e _h_victim gets the same value/object name as _output,right?) :dunno:

if I DON'T add _h_victim to the private array, nothing will happen(as the two variables have different names),right?  :whistle:

Just for clearance... :D

And i heard that in ArmA 2 ,you must declare a local variable before you use it..,right?
can i declare a variable by just using private?

I meant , is the following script correct?

Quote
private ["_a","_b","_c","_min","_1st","_2nd","_3rd","_max"];

_a=_this select 0;
_b=_this select 1;
_c=_this select 2;

while {alive player} do
{
   _a=player distance race_end;
   _b=player2 distance race_end;
   _c=player3 distance race_end;
   _min=_a min _b min _c;   
   _max=_a max _b max _c;
   if (_min == _a) then{_1st=_a} else {if (_min==_b) then {_1st=_b} else {_1st=_c}};
   if (_max == _a) then{_3rd=_a} else {if (_max==_b) then {_3rd=_b} else {_3rd=_c}};
   if (_a!=_1st AND _a!=_2nd) then {_midman=_a};
   if (_b!=_1st And _b!=_2nd) then {_midman=_b};
   if (_c!=_1st AND _c!=_2nd) then {_midman=_c};
   hint format ["1st : %1\n2nd : %2\n3rd : %3",_1st,_2nd,_3rd];
   sleep 3;
};
//script showing positions in a three man race
Very busy with life, business, and other stuff. Away from OFP for months. Not sure if I could get back onto it. :(

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: Order to move to a nearest car from the array?
« Reply #9 on: 07 Jun 2010, 18:22:47 »
Quote
i.e _h_victim gets the same value/object name as _output,right?
Yes, that is correct.

Quote
if I DON'T add _h_victim to the private array, nothing will happen(as the two variables have different names),right?
Not sure what you mean, but if _h_victim is not in the private array of your script and your function is called inside a loop, for example
Code: [Select]
.. code'n'stuff...
for[{_i=0},{_i<_blah},{_i=_i+1}] do {
    _h_victim = [_bluh,_bleh] call FINDSTUFF;
    sleep 1;
};

if (!(alive _h_victim)) then {.. stuff.. };
that if/then will not work because the _h_victim is "local" to the for/do loop and not available outside it.

Putting the variable in the private array, or declaring it first (before the loop) removes that "problem".

Quote
And i heard that in ArmA 2 ,you must declare a local variable before you use it..,right?
can i declare a variable by just using private?
I guess private should be enough but I have had problems if I have not declared the variables (that need declaring) so I have been declaring them.
But that might have been because when testing something I don't always remember to put the new variables in the private array so I can't say for sure (kingdom for a scripting software that automatically updates the private array when new local variables are typed :P ).

Quote
I meant , is the following script correct?
Like said above I can't give you a definite answer it should be OK.
I haven't done much of A2 scripting yet..
« Last Edit: 07 Jun 2010, 18:24:53 by h- »
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline haroon1992

  • Members
  • *
  • My life is hopeless...
Re: Order to move to a nearest car from the array?
« Reply #10 on: 08 Jun 2010, 04:29:27 »
It seemed like you've confused by my post and i've confused by your post :D  ???
From what you said.
Quote
.. code'n'stuff...
for[{_i=0},{_i<_blah},{_i=_i+1}] do {
    _h_victim = [_bluh,_bleh] call FINDSTUFF;
    sleep 1;
};

if (!(alive _h_victim)) then {.. stuff.. };

that will not work ,right?

what if
Quote
_h_victim=_this select 0;

.. code'n'stuff...
for[{_i=0},{_i<_blah},{_i=_i+1}] do {
    _h_victim = [_bluh,_bleh] call FINDSTUFF;
    sleep 1;
};

if (!(alive _h_victim)) then {.. stuff.. };
will it work?
I think _h_victim = _this select 0 is also declaring that variable,or giving it a value from the arguments.So it works,right? ???

You've been a great help for me _h... :clap:
I've been dong things wrong with my script,thank you for your helpful advices.. ;)

Regards,

Haroon1992
Very busy with life, business, and other stuff. Away from OFP for months. Not sure if I could get back onto it. :(

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: Order to move to a nearest car from the array?
« Reply #11 on: 08 Jun 2010, 07:30:27 »
Quote
I think _h_victim = _this select 0 is also declaring that variable,or giving it a value from the arguments.So it works,right?
Yes, either declaring the variable before the loop or putting it in the private array works.

It's not limited to loops only, as far as I know any code block like switch/do, if/then/else and so on behave the same.
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline haroon1992

  • Members
  • *
  • My life is hopeless...
Re: Order to move to a nearest car from the array?
« Reply #12 on: 08 Jun 2010, 10:54:49 »

i've got a prob _h look at the following link :
maybe you could handle that  :scratch:

http://www.ofpec.com/forum/index.php?topic=34676.0
Very busy with life, business, and other stuff. Away from OFP for months. Not sure if I could get back onto it. :(