OFPEC Forum
Editors Depot - Mission Editing and Scripting => OFP - Editing/Scripting General => Topic started by: TheOakster on 13 Jul 2006, 11:37:36
-
Hello,
I have the following code in a script:
_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:
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
-
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
_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 :)
-
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
-
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
-
@ 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
-
@bedges
Nope that doesnt work, there are no errors given, ch1 just doesnt move to the car.
Ben
-
testing now.
EDIT
right. the call format doesn't seem to like local variables. global variables work just fine. the ammended script thus reads:
_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
-
Cheers bedges that works
Ben
-
... the call format doesn't seem to like local variables...
...
call format ["d = car%1 distance ch1", _i]
...
I was just wondering if the reason that:
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.
_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.
-
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