Home   Help Search Login Register  

Author Topic: crouching when speed < 1  (Read 831 times)

0 Members and 1 Guest are viewing this topic.

headshot_snipe

  • Guest
crouching when speed < 1
« on: 23 May 2004, 16:52:03 »
I'm trying to make a script in which whenever a unit in a certain group goes slower than 1kmph then they crouch.

Is there a command that selects any unit in a certain group instead of making the whole group crouch at the same time?

I tried this, but it doesn't work, thats the first thing, and if it did work, it would probably make all the units in the group crouch. it came up with an error saying that the speed line was expecting an object not a group...

Code: [Select]
_grp = _this select 0

#loop
@ speed _grp < 1
"_x switchmove crouch" foreach units mygrp
goto "loop"
any ideas?

[EDIT]

 i think the real problem is how to use the speed command with the @ command?
 
« Last Edit: 23 May 2004, 17:24:46 by headshot_snipe »

Offline macguba

  • Former Staff
  • ****
    • macguba's operation flashpoint page
Re:crouching when speed < 1
« Reply #1 on: 23 May 2004, 19:18:33 »
Being a bear of very little brain I don't understand the question.     Do you mean

When a particular unit goes slow, it crouches?
or
When any unit in the group goes slow, it crouches?
or
When a particular unit goes slow the whole group crouches?
or
When any unit in the group goes slow the whole group crouches?

To what does _grp refer?   That is, what is _this select 0?

In any event, you would probably be better off losing the @ command and replacing it with a conditional line

?condition: consequence

and putting a delay in the loop.
Plenty of reviewed ArmA missions for you to play

headshot_snipe

  • Guest
Re:crouching when speed < 1
« Reply #2 on: 23 May 2004, 20:40:20 »
i meant  ;D :When any unit in the group goes slow, it crouches?

my script is rubbish, don't look at it  :-\ but i'd like to know how to do the above...

Quote
In any event, you would probably be better off losing the @ command and replacing it with a conditional line

do i have to?  :-\ does the speed command not work with the @ command?

Offline General Barron

  • Former Staff
  • ****
  • Semper Fi!
Re:crouching when speed < 1
« Reply #3 on: 24 May 2004, 00:05:04 »
Quote
do i have to?   does the speed command not work with the @ command?


No, all commands work with the @ command (provided you can make a true/false expression using them).

However, the @ command makes a loop that loops every 0.001 seconds (I think thats right). That creates a large drain on the CPU, especially when you start using more and more @ commands and have more scripts running at once. Unless you REALLY need to check a condition about 1000 times a second, then you should make a loop:

Code: [Select]
#loop
~0.1
? condition : goto "next"
goto "loop"
#next

The above loop will use 100x less CPU power, since it only loops 10 times/second, instead of an @ which loops 1000 times/second. That is why you should avoid @ conditions whenever possible.

-----------------------------------------------------------------

Okay, now that the lesson is over, on to your question:

You are going to want the following script to be run on every unit in the group. It is called like so:
        unit exec "crouch.sqs"

Or you could put this line in the group leader's init field:
        {_x exec "crouch.sqs"} foreach units this

crouch.sqs:
Code: [Select]
#loop
~0.1
? speed _this < 1 [b]&& speed _this > 0[/b] : _this playmove "crouch"
?alive _this : goto "loop"
exit

The part in bold may or may not be needed. I put it in there so that when a unit is stopped (speed = 0), he doesn't continuously try to crouch, which could end up making him frozen in place. Try the script without it, if it still works, then leave it out.

That should do it. Let me know if there are any problems.
HANDSIGNALS COMMAND SYSTEM-- A realistic squad-control modification for OFP
kexp.org-- The best radio station in the world, right here at home! Listen to John Richards!

headshot_snipe

  • Guest
Re:crouching when speed < 1
« Reply #4 on: 24 May 2004, 20:17:26 »
It doesn't seem to work, on the first line it says that it is an object and an array is expected...

(_this = _this select 0 [ # ] error select, type object, expected array)

I kinda changed the code a little

Code: [Select]
_this = _this select 0

#loop
~5
? speed _this < 1 : goto"crouch"
goto"loop"
#crouch
_this setunitpos"UP"
_this setbehaviour"combat"
_this playmove "crouch"
?alive _this : goto "loop"
exit

Actually, the script works still showing this error message, except they all seem to crouch at the same time

the _this = _this select 0 error seems to be happening because of the {_x exec "crouch.sqs"} foreach units this

[EDIT]

It doesn't work, i had forgoten to put a loop, but now the units dont crouch at all

here i have taken off the {_x exec "crouch.sqs"} foreach units this and instead put [this] exec"crouch.sqs" but another error comes up

at the speed line it says its an array an is expecting an object ?? any ideas?

(? speed _this < [ # ] 1 : goto"crouch"error speed: type array, expected object)
« Last Edit: 24 May 2004, 20:52:52 by headshot_snipe »

Offline General Barron

  • Former Staff
  • ****
  • Semper Fi!
Re:crouching when speed < 1
« Reply #5 on: 24 May 2004, 20:59:51 »
Whoops, I thought that I could do some yabbse tag in the code block, but it didn't work. I hope you weren't directly copying and pasting the code I gave you above, if you did, that would explain the error.

Trying it out in the editor, I noticed that my code (minus the typo) would make them crouch again and again when stopped. Instead of using the speed command, I suggest using the "unitready" command, which equals "true" when a unit isn't moving. Here is my revised code:

Code: [Select]
;don't run script on player
? _this == player : exit

;wait until unit stops moving
#moveLoop
~0.5
? unitready _this : goto "crouch"
?alive _this : goto "moveLoop"
exit

#crouch
_this setcombatmode "combat"
_this setunitpos "up"
_this playmove "Crouch"
#waitLoop
~0.5
? !(unitready _this) : goto "moveLoop"
?alive _this : goto "waitLoop"
exit

This is called the same way as the first one:

        unit exec "crouch.sqs"    -OR-
        {_x exec "crouch.sqs"} foreach [array of units]


This code almost works, but for some reason, the guys won't stay crouched after they halt. I'm not sure how to fix this, but at least they crouch when they are supposed to.
HANDSIGNALS COMMAND SYSTEM-- A realistic squad-control modification for OFP
kexp.org-- The best radio station in the world, right here at home! Listen to John Richards!

headshot_snipe

  • Guest
Re:crouching when speed < 1
« Reply #6 on: 26 May 2004, 14:05:00 »
It doesn't work, how does this:

{_x exec "crouch.sqs"} foreach [array of units]

work? I tried to put the group name in the [array of units] but it doesnt seem to work.

there is also another error in the script, but that occured because i had specified a group,

in the alive check, it said it was expecting an object not a group...

anyway, i haven't tried to do it with each individual unit, i'll try that later...

Offline General Barron

  • Former Staff
  • ****
  • Semper Fi!
Re:crouching when speed < 1
« Reply #7 on: 26 May 2004, 20:15:26 »
Quote
I tried to put the group name in the [array of units] but it doesnt seem to work.


That is because you need to put an array of units where it says, not a group. A group is not an array. However, to make an array of units out of a group, you need the units command. Example:

{_x exec "crouch.sqs"} foreach units GROUP1

[EDIT]
Oh, and about your post a few posts ago:

Quote
_this = _this select 0
Don't ever try to assign a value to the _this variable. There is no need to ever do that. I suggest you read over some scripting tutorials and get a better grasp on what the _this variable really is in a script:

player exec "script1.sqs"

If I call a script in this way, then in that instance of "script1.sqs", the variable _this will equal the player:

_this -> player

[player, bob] exec "script2.sqs"

If I call a script this way, in that instance of "script2.sqs", then the variable _this will instead be an array, which contains the player, as well as "bob":

_this -> [player, bob]

The command "_this select 0" will return the FIRST ITEM in the array called _this:

_this select 0 -> [ player (0), bob (1)]

Notice the first item starts at index 0, the second is at index 1, etc.

So in summary, when you call a script, whatever you put before "exec", will be passed to the script in the variable _this:

[array] exec "script.sqs"         ->   _this => [array]
unit exec "script.sqs"         ->   _this => unit
11 exec "script.sqs"         ->   _this => 11

And so on. Most scripts require an array to be passed to the script, as in the first example, because an array can hold more than one unit of data. However, this is not a requirement. In the script I wrote you, there was only one thing the script needed to know: which unit to "run the script on", so I only require that you pass it that.
« Last Edit: 26 May 2004, 20:28:22 by General Barron »
HANDSIGNALS COMMAND SYSTEM-- A realistic squad-control modification for OFP
kexp.org-- The best radio station in the world, right here at home! Listen to John Richards!

headshot_snipe

  • Guest
Re:crouching when speed < 1
« Reply #8 on: 26 May 2004, 20:33:47 »
hi, i changed your script again  ::) now it works, and the units stay in that position more or less, there are still errors like sometimes the units crouch and uncrouch straight after but its quiet cool to see the ai crouch when firing... here is the script, if you want to optimize it please do

Code: [Select]
_man = _this select 0

#loop
~1
? unitready _man : goto "crouch"
?alive _man : goto "Loop"
exit
#crouch
_man setunitpos"UP"
_man setbehaviour"combat"
_man playmove"crouch"
~1
_man switchmove"crouch"
? unitready _man : goto "wait"
?! alive _man : exit
#wait
~2
?(speed _man) == 0 : goto"loop"
goto"wait"

thanks for the explanation general barron  :) that does clear things up a bit

[EDIT]

well actually, there is a problem, the death sequences are looped :(
« Last Edit: 26 May 2004, 20:52:13 by headshot_snipe »

wi77ard

  • Guest
Re:crouching when speed < 1
« Reply #9 on: 02 Jun 2004, 19:31:32 »
hiya,

just found this script looks good, tho the man keeps crouching even when dead, so i changed one line in the code and it seems to stop this happening.

Code: [Select]
_man = _this select 0

#loop
?! alive _man : exit
~1
? unitready _man : goto "crouch"
?alive _man : goto "loop"
exit
#crouch
_man setunitpos"UP"
_man setbehaviour"combat"
_man playmove"crouch"
~1
_man switchmove"crouch"
? unitready _man : goto "wait"
?! alive _man : exit
#wait
~2
?(speed _man) == 0 : goto"loop"
goto"wait"