Home   Help Search Login Register  

Author Topic: Custom Array not working... list of vehicles  (Read 1327 times)

0 Members and 3 Guests are viewing this topic.

TheOakster

  • Guest
Custom Array not working... list of vehicles
« 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

Offline bedges

  • Administrator
  • *****
    • OFPEC The Editing Center
Re: Custom Array not working... list of vehicles
« Reply #1 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 :)
« Last Edit: 13 Jul 2006, 12:16:33 by bedges »

TheOakster

  • Guest
Re: Custom Array not working... list of vehicles
« Reply #2 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

Offline Planck

  • Honoured
  • Former Staff
  • ****
  • I'm never wrong ....I'm just not always right !
Re: Custom Array not working... list of vehicles
« Reply #3 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.

nearestObject  here.


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




Planck

I know a little about a lot, and a lot about a little.

TheOakster

  • Guest
Re: Custom Array not working... list of vehicles
« Reply #4 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

TheOakster

  • Guest
Re: Custom Array not working... list of vehicles
« Reply #5 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


Offline bedges

  • Administrator
  • *****
    • OFPEC The Editing Center
Re: Custom Array not working... list of vehicles
« Reply #6 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
« Last Edit: 13 Jul 2006, 12:44:00 by bedges »

TheOakster

  • Guest
Re: Custom Array not working... list of vehicles
« Reply #7 on: 13 Jul 2006, 12:51:41 »
Cheers bedges that works

Ben
« Last Edit: 13 Jul 2006, 12:56:08 by Ben »

Offline Mr.Peanut

  • Former Staff
  • ****
  • urp!
Re: Custom Array not working... list of vehicles
« Reply #8 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 is a primer.


« Last Edit: 13 Jul 2006, 14:54:07 by Mr.Peanut »
urp!

TheOakster

  • Guest
Re: Custom Array not working... list of vehicles
« Reply #9 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 is a primer.

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

Ben