Jump to content
Sign in to follow this  
Doolittle

Foreach

Recommended Posts

Anyone know why this doesn't work??

_weapons = weapons _vehicle

"_vehicle addmagazine _x" forEach _weapons

Seems like forEach only works when _x is at front???

Doolittle

Share this post


Link to post
Share on other sites

By the way, weapons _vehicle was added with 1.75...it's nice! It returns an array.

Doolittle

Share this post


Link to post
Share on other sites

_x works everywhere (not only at the beginning)

e.g: "alive _x" count (units group) < 5

would become true, once there are less than 5 in the group

Your case cannot work, because it doesn't make sense.

You're trying to add the weapons of a unit as magazines

to a unit.

With resistance we got: weapons _vehicle,

and we got:

magazines vehicle

Operand types:

vehicle: Object

Compatibility:

Version 1.75 required.

Type of returned value:

Array

Description:

Returns array of types names of all vehicle's magazines.

Example:

magazines player

Just an example, of how your array could look like, by the way, you are using it:

_weapons = ["G36A","Glock"]

Now you can't add magazines of type "G36A" or "Glock" to a

unit's magazine slots, because the magazine names would be:

"G36Amag" or "Glockmag"

I'm not sure, what you want to do here.

Do you want to detect the weapons of a unit, to be able to

equip the same unit with magazines, of it's weapon type?

Or do you want to equip other units with magazines of this

unit's weapon types?

Further more, the syntax you were using, doesn't make sense

to me;

"_vehicle addmagazine _x" foreach _weapons

In the case of the two weapons, i mentioned above it would

be then:

_vehicle addmagazine "G36A";

_vehicle addmagazine "Glock"

If you would say:

_magazines = magazines unit1

_weapons = weapons unit1

unit2 addmagazine _magazines

unit2 addweapon _weapons

Then this should work.

Please come with more detail and maybe we get closer to

a solution.

~S~ CD

Share this post


Link to post
Share on other sites

You can't use local variables with foreach!

This isn't allowed:

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">"_vehicle addMagazine _x" foreach _weapons<span id='postcolor'>

You have to do something like this:

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">"MyVehicle addMagazine _x" foreach _weapons<span id='postcolor'>

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (bigpoppa @ Oct. 08 2002,12:52)</td></tr><tr><td id="QUOTE">You can't use local variables with foreach!

...

You have to do something like this:

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">"MyVehicle addMagazine _x" foreach _weapons<span id='postcolor'><span id='postcolor'>

... or you have to wait for 1.85 patch, where nested variable scopes are supported and you will therefore be able to use local variables even in forEach.

The reason why your code does not work in 1.75 is because forEach has its own local variable scope (this is where variable _x is defined) and cannot see local variables of outer scopes.

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (Suma @ Oct. 08 2002,13:28)</td></tr><tr><td id="QUOTE"></span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (bigpoppa @ Oct. 08 2002,12:52)</td></tr><tr><td id="QUOTE">You can't use local variables with foreach!

...

You have to do something like this:

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Code Sample </td></tr><tr><td id="CODE">"MyVehicle addMagazine _x" foreach _weapons<span id='postcolor'><span id='postcolor'>

... or you have to wait for 1.85 patch, where nested variable scopes are supported and you will therefore be able to use local variables even in forEach.

The reason why your code does not work in 1.75 is because forEach has its own local variable scope (this is where variable _x is defined) and cannot see local variables of outer scopes.<span id='postcolor'>

That's cute! Thanks BIS! smile.gif

Share this post


Link to post
Share on other sites

Wow, you guys reply fast! That was like my first post & I am impressed.

Chris Death, they have it as "unit addmagazine weaponName", so yeah that confused me too but it looks like they want the name of the weapon and not the magazine.

Doolittle

Share this post


Link to post
Share on other sites

OK Doolittle, the comref says:

unit addmagazine weapon name, but you still need to

add the correct magazine name (not the weapon name).

I think the addmagazine command in the comref has not been

changed since OFP's release last year in june. At this time

the weapon names and the magazine names were identically

(weapon "M16", magazine "M16").

But since the release of weapons like XMS, G36A, etc. the

weapon names and the magazine names became different.

(weapon "STEYR", magazine "STEYRmag")

So you need to capture the magazine names instead of the

weapon names, to be able to refill.

mymags = magazines player

~S~ CD

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (DV Chris Death @ Oct. 08 2002,12:39)</td></tr><tr><td id="QUOTE">So you need to capture the magazine names instead of the

weapon names, to be able to refill.

mymags = magazines player

~S~ CD<span id='postcolor'>

I think you are right about having to use Mag Names, but magazines Player might not work as he might be out of them.

Share this post


Link to post
Share on other sites

bn880

off course the magazines have to be saved into an array,

before he's out of them biggrin.gif

So put the magazines at the start into an array, and add the

whole bunch of 'em later again to the unit.

Also before you add the magazines again to the unit, you could remove all magazines, the unit still owns.

~S~ CD

Share this post


Link to post
Share on other sites

</span><table border="0" align="center" width="95%" cellpadding="3" cellspacing="1"><tr><td>Quote (DV Chris Death @ Oct. 08 2002,12:53)</td></tr><tr><td id="QUOTE">bn880

off course the magazines have to be saved into an array,

before he's out of them biggrin.gif<span id='postcolor'>

LOL, yea

but seriously, you may not be able to do that, or at least it's complicated.

Someone may pick up an empty assault rifle... sad.gif

I think he has enough info now to make his script though. smile.gif

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×