Home   Help Search Login Register  

Author Topic: while loop  (Read 1663 times)

0 Members and 1 Guest are viewing this topic.

CopyrightPhilly

  • Guest
while loop
« on: 21 Apr 2005, 15:06:15 »
Hey,

OK i'm using the following code to eject the units in the cargo of the chopper

Code: [Select]
;-------------------------------------------------
; Make all the units in the cargo jump one by one
;-------------------------------------------------
~3

_chopper = _this select 0
_height = _this select 1
_chopper_height = getpos _chopper select 2

_chopper flyinheight _height

_pilot= driver _chopper
_gunner = gunner _chopper
_cargo = (crew _chopper) - [_pilot, _gunner]

_loops = count _cargo
_counter = 0

while "_loops > _counter" do {
_unit = _cargo select _counter
_unit action ["eject", _chopper]
_counter = _counter + 1
~1
}

_chopper flyinheight _chopper_height
exit

now the problem i'm haiving is that i get an error at the end of the while...

the error is:
Quote
'|#|}'; Error Invalid number in expression

btw, this is the first time i have needed to use the while command so its problery the format that is wrong...

cheers,
Phil
« Last Edit: 21 Apr 2005, 15:07:29 by CopyrightPhilly »

Offline 456820

  • Contributing Member
  • **
Re:while loop
« Reply #1 on: 21 Apr 2005, 15:11:21 »
this should be riht thats if i understood the question wich i think i did where it says

while "_loops > _counter" do {

change it to

# while
_loops > _counter" do {

that should work i think its because to define a name in a script there has to be a "#" in front of it

CopyrightPhilly

  • Guest
Re:while loop
« Reply #2 on: 21 Apr 2005, 15:28:17 »
thanks for the reply 456820 but,

i'm still getting the same error when using:
Code: [Select]
# while
"_loops > _counter" do {
_unit = _cargo select _counter
_unit action ["eject", _chopper]
_counter = _counter + 1
~1
}

Offline Pilot

  • Contributing Member
  • **
Re:while loop
« Reply #3 on: 21 Apr 2005, 15:36:36 »
Hey CopyrightPhilly,

I think you need to seperate your commands.  I think (not too sure, though) you can do this with an and:

while "_loops > _counter" do {_unit = _cargo select _counter and _unit action ["eject", _chopper] and _counter = _counter + 1 and ~1}

or you may have to use a comma:
while "_loops > _counter" do {_unit = _cargo select _counter, _unit action ["eject", _chopper], _counter = _counter + 1, ~1}

Just a suggestion, I may be totally wrong, I have never used the while do command before

-Student Pilot

UNN

  • Guest
Re:while loop
« Reply #4 on: 21 Apr 2005, 15:45:40 »
Can you use while loops in a script or is it just functions? Well either way you would probably need semi-colons and the end of each command line.

But could do it this way, at least I know for sure that will work:

Code: [Select]
_counter=0
_cargo=+_cargo
_count=count _cargo

#Loop
(_cargo select _counter) action ["eject",_chopper]
_counter = _counter + 1
~1
If (_counter<_count) Then {goto "Loop"}

I don't think you can use ~ in between curly brakets, because the brakets make it a function.

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:while loop
« Reply #5 on: 21 Apr 2005, 15:51:14 »
Quote
Can you use while loops in a script or is it just functions
Yes you can use them in scripts but when you do you should separate each instruction with a semi-colon (not a new line) and since you can only use a tilda (~) to denote a delay if you put it at the start of a line you cannot put a delay in the list of instructions executed by a while statement.  AFIK
« Last Edit: 21 Apr 2005, 15:51:47 by THobson »

Offline Pilot

  • Contributing Member
  • **
Re:while loop
« Reply #6 on: 21 Apr 2005, 15:55:42 »
Commands are seperated by a semi-colon?!  I thought any text after a semicolon was considered to be a note and not used during the processing of the script.

Offline Baddo

  • Former Staff
  • ****
  • Reservist Jaeger
Re:while loop
« Reply #7 on: 21 Apr 2005, 16:07:45 »
The while...do loop has to be on a single line when used in a script. In a function you could spread it to multiple lines.

But the delay causes an error if you put it into the while...do block even if it is on a single line. Because you obviously want a delay to your script, you have to use some other method.

You could use something like this:

Code: [Select]
;-------------------------------------------------
; Make all the units in the cargo jump one by one
;-------------------------------------------------
~3

_chopper = _this select 0
_height = _this select 1
_chopper_height = getpos _chopper select 2

_chopper flyinheight _height

_pilot= driver _chopper
_gunner = gunner _chopper
_cargo = (crew _chopper) - [_pilot, _gunner]

_k = count _cargo
_i = 0

#Loop

_j = _cargo select _i
_j action [{EJECT}, _chopper]
unassignvehicle _j

~1

_i = _i + 1

? (_i == _k) : exit
goto "Loop"

_chopper flyinheight _chopper_height

Note that you have to use the unassignvehicle command because otherwise those AI's will try to get back into the chopper if they were assigned as cargo.

And a semicolon marks a comment only when used as the first character in a line.
« Last Edit: 21 Apr 2005, 16:08:01 by Baddo »

CopyrightPhilly

  • Guest
Re:while loop
« Reply #8 on: 21 Apr 2005, 16:21:25 »
thanks chaps,

i ended up using the following:
Code: [Select]
#loop
?_loops <= _counter : goto "end"
_unit = _cargo select _counter
_unit action ["eject", _chopper]
unassignVehicle _unit
_counter = _counter + 1
~0.3
_unit setdammage 0
goto "loop"
and it worked fine

thanks again,
Phil