Home   Help Search Login Register  

Author Topic: Assorted questions (SetUnitPos, SetPos and GetDir, variables, etc.)  (Read 1351 times)

0 Members and 1 Guest are viewing this topic.

Offline Kurayami

  • Members
  • *
OK. Several questions now.

#1: How could I adapt SetUnitPos to multiple units with a single command? It won't accept arrays of units.

#2: I still can't get a script to check for a dynamic global variable, and nobody was really clear with me on how to get that working last time...

Code: [Select]
UnitSide = _this select 0
_Group = _this select 1
Condition = _this select 2*************

#Loop
_Count = UnitSide CountSide units _Group;
? (_Count == 0) : goto "End1";
? (Condition == 1) : goto "End2";**********
;TitleText [format ["%1" + " alive", _Count], "plain down"];
~2
goto "Loop";

#End1
hint "Everybody's dead.";
exit;

#End2
hint "Cond. true";
exit;
The lines with the asterisks are the ones that don't work. I have tried at least a dozen variations, but this script never can return T/F on a global variable.

#3: How (if it's possible) can I get the names of individual units comprising a group to be returned on the fly?

#4: And finally... a weird one. I have a feeling this one's going to be complicated. How can I use the info returned by GetPos with SetPos?
Example:
Say I'm facing north at 0, 0, 0. GetDir returns that I'm facing 0, so I want SetPos to move me to 0, 4, 0. That's simple enough, but it gets more complicated when GetDir returns something like 128... How would I have SetPos move a unit in that direction?

Thanks.

Offline Ranger

  • Members
  • *
  • Hoo-ah!
#1: How could I adapt SetUnitPos to multiple units with a single command? It won't accept arrays of units.

It seems that one of the most oft overlooked commands is forEach, and any others that allow application to multiple units.  Anyway, use the following code:

Code: [Select]
"_x setUnitPos {Down}" forEach units squad1
Replace Down with Up or Auto if those are what you intend to use instead.  Replace squad1 with the name of your group.

Quote
#2: I still can't get a script to check for a dynamic global variable, and nobody was really clear with me on how to get that working last time...

You use global variables exactly the same way as you do local variables, except that they don't have the underscore (_) in front of them.  E.g.,

GlobalVar1 = GlobalVar1 + 1

vs.

_LocalVar1 = _LocalVar1 + 1

Same syntax.

Quote
Condition = _this select 2*************
...
? (Condition == 1) : goto "End2";**********
exit;

The lines with the asterisks are the ones that don't work. I have tried at least a dozen variations, but this script never can return T/F on a global variable.

What value are you passing to the third parameter?  That's probably where your problem lies.

Why are you using a global variable for this, anyway?

Quote
#3: How (if it's possible) can I get the names of individual units comprising a group to be returned on the fly?

The easiest way I know how to explain this is to use an example of trying to display the unit's name in a title or hint.  In this case, just treat the unit's variable as a string.

Code: [Select]
hint format ["Unit's name: %1",_soldier1]
Where _soldier1 is the example unit for which you want the name.  The resulting hint will display the following text:

Unit's name: Alpha Black 1

Or something similar.

Quote
Say I'm facing north at 0, 0, 0. GetDir returns that I'm facing 0, so I want SetPos to move me to 0, 4, 0. That's simple enough, but it gets more complicated when GetDir returns something like 128... How would I have SetPos move a unit in that direction?

You would need to use some geometry functions to move a unit some meters in some direction.  I'm no math expert, so I'll leave this one up to the resident math experts.

I could figure it out, but it would take me a while, whereas I'm sure others here could pull it off the top of their heads.
Ranger

Offline Kurayami

  • Members
  • *
It seems that one of the most oft overlooked commands is forEach, and any others that allow application to multiple units.
That was certainly overlooked by me. Thanks, that will help out in a dozen different places.

Quote
Why are you using a global variable for this, anyway?
Because I need one. I'd rather use a trigger, but that isn't working either.
Short version of the explaination.
I have two groups (Actually four, but two is faster to explain.) One is Alpha1 and the other Alpha2. Alpha1 has a move watpoint with the condition "AlphaGo == 1." Alpha2 has a Join waypoint with Alpha1 with "AlphaReady = 1" on activation. Then I set a trigger with a true activation that executes the script above. The third param (Condition) would be "AlphaReady" in this case. The script just never replaces the Condition variable with AlphaReady when it executes.

Basically all I want is something that'd check "West CountSide units _xGroup == 0 || Condition == 1"

I can't get that to work in a trigger, so I've been trying to work it into a script with a user defined variable so that I don't have to have four sperate scripts that do the same thing for each group.

Quote
The easiest way I know how to explain this is to use an example of trying to display the unit's name in a title or hint.  In this case, just treat the unit's variable as a string.

Code: [Select]
hint format ["Unit's name: %1",_soldier1]
Where _soldier1 is the example unit for which you want the name.  The resulting hint will display the following text:

Unit's name: Alpha Black 1

Or something similar.
That'll help. Thanks.

Quote
You would need to use some geometry functions to move a unit some meters in some direction.  I'm no math expert, so I'll leave this one up to the resident math experts.

I could figure it out, but it would take me a while, whereas I'm sure others here could pull it off the top of their heads.
I'm cetainly no math expert either. I'm also not clear on what some of the geometry functions do, so I'm kinda stuck.

Offline Kurayami

  • Members
  • *
It seems that one of the most oft overlooked commands is forEach, and any others that allow application to multiple units.  Anyway, use the following code:

Code: [Select]
"_x setUnitPos {Down}" forEach units squad1
I just gave that a shot. It returned
'_x SetUnitPos |#|{Down}': Error invalid number in expression.

Using anything other than {} brackets just doesn't do anything at all with SetUnitPos. It did work fine with other things that I tried though... like SetDammage.

Offline Sui

  • Former Staff
  • ****
    • OFPEC
Hey there, Kurayumi

Concerning your foreach bit, try:

"_x setunitpos ""UP""" foreach units squad1

Which is about the same thing as the braces line, except I think you would need two sets of those to make it work as well ;)

"_x setunitpos {{UP}}" foreach units squad1 ???

I'd get off my lazy butt and test it myself, but my copy of OFP is in a box bound for Australia at the moment ;D

As for your global variable problem, you do not have to define the gobal variable in the script. So, to get it working, simply get rid of this line:

Code: [Select]
Condition = _this select 2*************
Condition is global... you don't need to tell your script what it is, your script already knows ;)

To get an array of all the units in a group, use: units group

The units will turn the group into an array

As for your setpos question.. .you want the unit to be moved forward 4 metres (in the direction it is facing?)

Try:

unit setpos [(getpos unit select 0) + sin (getdir unit) * 4, (getpos unit select 1) + cos (getdir unit) * 4, getpos unit select 2]

If you do a forum search of that sort of line you should find more examples... ;)
Good Luck

Offline Ranger

  • Members
  • *
  • Hoo-ah!
Thanks for helping clarify my explanation, Sui.

"_x setunitpos ""UP""" foreach units squad1

Which is about the same thing as the braces line, except I think you would need two sets of those to make it work as well ;)

It was my understanding that { } represented the same as "" "", which is why I suggested that Kurayami use it.  I have used it that way in some of my code, and it works fine.  I don't know why it didn't work for Kurayami, though.  Weird.
Ranger

Offline Ranger

  • Members
  • *
  • Hoo-ah!
Basically all I want is something that'd check "West CountSide units _xGroup == 0 || Condition == 1"

Probably this isn't working because your group name should be a global variable.  E.g.,

Code: [Select]
West CountSide units xGroup == 0 || Condition == 1
When giving your group a name, it similarly shouldn't have the underscore in front of it.  E.g.,

Code: [Select]
xGroup = group this
« Last Edit: 20 Dec 2002, 23:51:11 by Ranger »
Ranger

Offline Kurayami

  • Members
  • *
Hey there, Kurayumi

Concerning your foreach bit, try:

"_x setunitpos ""UP""" foreach units squad1

Which is about the same thing as the braces line, except I think you would need two sets of those to make it work as well ;)
Hmm.
"_x SetUnitPos "down"" foreach units Test = Unknown operator Down

"_x SetUnitPos ""down"" foreach units Test = Invalid number in expression

"_x SetUnitPos {{down}}" foreach units Test = '_x SetUnitPos |#|{{Down}}': Error invalid number in expression.

This is proving rather difficult. I've tried every variation of quotes, brakets or parenthesis that make even the least bit of sense and all of them return errors.

The reason that I need this on a group level is because the AI has a habit of drowning itself anywhere near the water ::)

I'd like to be able to turn on "Up" at will for the whole group in order to prevent this.

Quote
As for your global variable problem, you do not have to define the gobal variable in the script. So, to get it working, simply get rid of this line:

Code: [Select]
Condition = _this select 2*************
Condition is global... you don't need to tell your script what it is, your script already knows ;)
Heh, yes. The script works when the actual variable is put into the script instead of condition...
But that defeats the purpose of the script. I don't want the actual global variable to be in the script because I want to be running the same script from multiple triggers checking different variables. If I hard-coded the variable then I'd need several versions of the same script. That'd just be messy, so that's why I'm trying to get this...

Quote
To get an array of all the units in a group, use: units group

The units will turn the group into an array
Excellent. That's exactly what I need. Thanks.
I had been trying to find a way to get group to return what was in the group. I didn't realize this could be done with units. Makes more sense now.

Quote
As for your setpos question.. .you want the unit to be moved forward 4 metres (in the direction it is facing?)

Try:

unit setpos [(getpos unit select 0) + sin (getdir unit) * 4, (getpos unit select 1) + cos (getdir unit) * 4, getpos unit select 2]
That's a huge step in the right direction. Thanks.

I'm going to have to play with the sin and cos commands a bit. They've been kind of a mystery to me up 'til now since I haven't seen many scripts that use them.
« Last Edit: 21 Dec 2002, 02:30:12 by Kurayami »

Offline Kurayami

  • Members
  • *
Probably this isn't working because your group name should be a global variable.  E.g.,

Code: [Select]
West CountSide units xGroup == 0 || Condition == 1
When giving your group a name, it similarly shouldn't have the underscore in front of it.  E.g.,

Code: [Select]
xGroup = group this
*slaps head*
Putting the underscore there was an accident.
I meant that the trigger didn't work with
Code: [Select]
West CountSide units xGroup == 0 || SomeCondition == 1;When using that as the condition in a trigger field the first part is disregarded. In other words, when xGroup dies, the trigger doesn't activate. It will only activate if SomeCondition is true. That's why I decided to move to external scripts, but there I'm having the exact opposite problem.

I imagine I'm missing something really stupid, but in digging through the source of a few of the official missions I really didn't come across any triggers like this.

Offline Kurayami

  • Members
  • *
OK, I solved one of the problems and I figured that you guys might want to know the solution for future reference.

Code: [Select]
pos="DOWN"; "_x SetUnitPos pos" forEach units xGroup;
This is the way that BIS did it. It's also the only way that I've gotten it to work.

Offline Kurayami

  • Members
  • *
OK. I still can't get the damned unit count || variable thing to work. I've tried everything and I've pulled apart half a dozen BIS missions looking for an answer.

I don't care about the external script. I'd rather have it work in a trigger, but no matter what I've tried it hasn't worked.

West CountSide units xGroup == 0 || MyVar == 1
No good.

After defining Count as
"Count = West CountSide units xGroup"
Count == 0 || MyVar == 1
No good.

I've tried a million different things, but the syntax is wrong somewhere (and I've tried a million different syntaxes  ::) )

It's so incredibly simple, but I just can't figure it out.

The script will get the unit count part right but completely drop the Var whereas the trigger will detect the var being true but completely borks the unit count. >:(


Offline Ranger

  • Members
  • *
  • Hoo-ah!
First, have you defined your global variable somewhere?  Put MyVar = 0 in your init.sqs to initialize it.  I've had trouble in the past with or statements, and people claim that initializing the variable solves this problem.  I've had mixed success with this solution, though.  Sometimes it works, sometimes it doesn't.

Second, try adding some parenthesis to your condition to make sure the game separates things correctly.  E.g.:

(West CountSide (units xGroup) == 0) || (MyVar == 1)

See if that helps any.
Ranger

Offline Kurayami

  • Members
  • *
First, have you defined your global variable somewhere?  Put MyVar = 0 in your init.sqs to initialize it.  I've had trouble in the past with or statements, and people claim that initializing the variable solves this problem.  I've had mixed success with this solution, though.  Sometimes it works, sometimes it doesn't.
All right. That did it. I didn't have to change any syntax at all. Thanks. Now I can finally move on with me life  ;D

Quote
Second, try adding some parenthesis to your condition to make sure the game separates things correctly.  E.g.:

(West CountSide (units xGroup) == 0) || (MyVar == 1)

See if that helps any.
OK, somebody explain this to me. I've never seen the usage of brackets and parenthesis defined.

I know that [] is used to define the elements of an array.
I know that "" is used to enlose text strings and is (?) interchangible with () in certain situations.

But what is the technical definition for () and {} brackets?

Last question. Promise  ;)

Offline Ranger

  • Members
  • *
  • Hoo-ah!
Quote
I know that [] is used to define the elements of an array.
I know that "" is used to enlose text strings and is (?) interchangible with () in certain situations.

But what is the technical definition for () and {} brackets?

Actually, "" and () are not interchangeable. THey represent different things.

"" indeed are used to enclose text strings.

() are used to force certain parts of code to be grouped together.  The easiest example I can think of is a mathematical calculation.

For example, if you have multiplication and addition in the same equation, the multiplication always is done first, and then the addition as a basic math rule.  Let's say you have the following equation:

3 * 2 + 1

The result would be 7, since the calculation would be 3 multiplied by 2, which equals 6, and then add 1, which totals 7.

However, let's say you wanted to do 2 + 1 first, and then multiply by 3.  Since the basic math rule requires that the multiplication be done first, you have to use parenthesis to force the addition to be done first.

3 * (2 + 1)

The result would be 9.  The parenthesis forces the 2 + 1 to be done first, which totals 3.  Next, the total is multiplied by 3, for a result of 9.

The same idea applies to any other kind of code.  That is, the parenthesis forces portions of code to be grouped together.

I hope that helps explain it.
Ranger

Offline Kurayami

  • Members
  • *
Ah. Thanks. Makes sense.