OFPEC Forum

Editors Depot - Mission Editing and Scripting => OFP - Editing/Scripting General => Topic started by: TheOakster on 13 Jul 2006, 11:37:36

Title: Custom Array not working... list of vehicles
Post by: TheOakster on 13 Jul 2006, 11:37:36
Hello,

I have the following code in a script:

Code: [Select]
_veh = ["SKODA","SKODARED","SKODAGREEN","SKODABLUE","TruckV3SCivil"]
car = nearestobject[ch2,"_veh"]

ch1 domove getpos car
ch2 domove getpos car

But it doesnt work, however if I do this then it does:

Code: [Select]
car = nearestobject[ch2,"skodared"]

ch1 domove getpos car
ch2 domove getpos car

Is there something wrong with how I have defined the array, or do arrays not work for nearestobject?

Ben
Title: Re: Custom Array not working... list of vehicles
Post by: bedges on 13 Jul 2006, 12:05:51
Quote
or do arrays not work for nearestobject?

correct.

what you will have to do is run a loop to determine which types of vehicle are near to ch2, and if there's more than one, determine which is closest. something like


Code: [Select]
_veh = ["SKODA","SKODARED","SKODAGREEN","SKODABLUE","TruckV3SCivil"]

_i = 0

#loop

call format ["_car%1 = nearestobject [ch2, (_veh select _i)]", _i]

_i = _i + 1
?not (_i == (count _veh)) : goto "loop"

_dist = 1000
_i = 0
#loop2

call format ["_d = _car%1 distance ch1", _i]
? _d < _dist : _dist = _d; call format ["_closest = _car%1", _i]

_i = _i + 1
?not (_i == (count _veh)) : goto "loop2"

ch1 domove getpos _closest

what this does is loop through the array of vehicles, checking if there are any near ch1. if there is a vehicle of that type nearby, it is assigned a variable (_car1, _car2, _car3, etc).

then another loop is done checking which of the _cars is closest to ch1. the closest car is assigned the variable _closest. ch1 is then told to move to it.

untested, but give her a whirl :)
Title: Re: Custom Array not working... list of vehicles
Post by: TheOakster on 13 Jul 2006, 12:12:11
what you will have to do is run a loop to determine which types of vehicle are near to ch2, assign them to variables and then determine which is closer.

That easy huh?  :'(

I will be back soon then....

Ben
Title: Re: Custom Array not working... list of vehicles
Post by: Planck on 13 Jul 2006, 12:17:31
nearestObject only works on a position in x,y,z format or an object......[position, "type"] or [object, "type"]

Consult the command reference there is a link on the Home page.

In case you cant find it,  :D   here (http://www.ofpec.com/COMREF/index.html).

nearestObject   here (http://www.ofpec.com/COMREF/index.php?action=list&letter=n).


EDIT:  That bedges , getting to quick on the type these days. >:(




Planck

Title: Re: Custom Array not working... list of vehicles
Post by: TheOakster on 13 Jul 2006, 12:19:35
@ bedges - trying now

@planck - I was using the comref when I was trying to work out how this was used, but i just wondered if it would work  ;)

Ben
Title: Re: Custom Array not working... list of vehicles
Post by: TheOakster on 13 Jul 2006, 12:25:39
@bedges

Nope that doesnt work, there are no errors given, ch1 just doesnt move to the car.

Ben

Title: Re: Custom Array not working... list of vehicles
Post by: bedges on 13 Jul 2006, 12:27:17
testing now.

EDIT

right. the call format doesn't seem to like local variables. global variables work just fine. the ammended script thus reads:

Code: [Select]
_veh = ["SKODA","SKODARED","SkodaGreen","SKODABLUE","TruckV3SCivil"]

_i = 0

#loop

call format ["car%1 = nearestobject [ch1, (_veh select _i)]", _i]

_i = _i + 1
?not (_i == (count _veh)) : goto "loop"

_dist = 1000
_i = 0
#loop2

call format ["d = car%1 distance ch1", _i]
? d < _dist : _dist = d; call format ["closest = car%1", _i]

_i = _i + 1
?not (_i == (count _veh)) : goto "loop2"

ch1 domove getpos closest
Title: Re: Custom Array not working... list of vehicles
Post by: TheOakster on 13 Jul 2006, 12:51:41
Cheers bedges that works

Ben
Title: Re: Custom Array not working... list of vehicles
Post by: Mr.Peanut on 13 Jul 2006, 14:48:11
... the call format doesn't seem to like local variables...

Code: [Select]
...
call format ["d = car%1 distance ch1", _i]
...

I was just wondering if the reason that:
Code: [Select]
call format ["_d = car%1 distance ch1", _i]does not work is because _d needs to be defined before the code string is executed?
i.e.
Code: [Select]
_d = 0
call format ["_d = car%1 distance ch1", _i]

Can't check right now as I am at work but am still curious.

A new tute on the finer points of the call command and the call format idiom, and numerous examples of their possible uses would be good.  This thread (http://www.ofpec.com/forum/index.php?topic=27589.0) is a primer.


Title: Re: Custom Array not working... list of vehicles
Post by: TheOakster on 13 Jul 2006, 15:08:31
A new tute on the finer points of the call command and the call format idiom, and numerous examples of their possible uses would be good.  This thread (http://www.ofpec.com/forum/index.php?topic=27589.0) is a primer.

 ???  That thread just scares me....!!  ;)

Ben