Jump to content
Sign in to follow this  
kyfohatl

HELP with {faction _x} foreach <array>

Recommended Posts

I've recently run across the this problem: When I try to get the faction of every unit in a trigger area using:

MyProcedureHandler = [thisList] execVM "FactionCount.sqf"

in the init line of the trigger (with activation set to "ANYBODY") and:

_unitList = _this;
_factionText = {(faction _x)} foreach _unitList;
sleep 2;
hint format ["%1", _factionText];

in "FactionCount.sqf".

The stange thing is that the game gives me an error, saying that the phrase "Faction _x" is invalid, because:

Error faction: Type array, expected object

Which seems to me that it means the command "faction" cannot deal with an array, and it must have single objects. Does anyone know a way around this? :confused: (or is it some sort of syntax error?)

This leads me to the general question that; if there is no way around the problem above, would it be possible to split the array "_unitList" (or any array) into its consisting objects (where each object in this case is a unit in the trigger area), so that I could run the "faction" command for each object? The problem is that it is not possible to predict how many units will be in the trigger area. Or is this unnecessary, and the faction command can somehow work with an array?

Thanks for any help.

Share this post


Link to post
Share on other sites

Help me understand - what do you want to achieve?

_unitList is an array of units [1..n] and you want to know which faction ALL of the units are? They can be in different factions, so how do you decide which one si the one you are interested in?

Why do you need this? maybe if we understand the purpose, we could suggest different/proper solution.

Share this post


Link to post
Share on other sites

faction takes a single units and gives a string saying which faction the unit belongs to. The reason you get the error is because you try to use, faction ARRAY instead of faction UNIT. The problem lies in the way you pass thisList to the script. Let's say thisList is something like this:

[b][[/b]BLUFOR, OPFOR, BLUFOR, CIVILIAN, BLUFOR[b]][/b]

. That's an array. You pass it to your script using:

MyProcedureHandler = [b][[/b]thisList[b]][/b] execVM "FactionCount.sqf"

What it actually says is, if you fill in thisList is:

MyProcedureHandler = [[BLUFOR, OPFOR, BLUFOR, CIVILIAN, BLUFOR]] execVM "FactionCount.sqf"

Then you run forEach on _this. _this is an array containing 1 array. So for each item in the array you assign to _x. There is only one item in the array, the "thisList" array, so _x becomes that resulting in you saying:

{(faction [b][[/b]BLUFOR, OPFOR, BLUFOR, CIVILIAN, BLUFOR[b]][/b])}

In the trigger just remove the [ ] on the sides of thisList and that part should work.

However I think you might intend to do this instead:

_factionText = ""; //Start empty
{_factionText = _factionText + str(faction _x) + ", ";} forEach _unitList;

Share this post


Link to post
Share on other sites

@Muzzleflash: Yes... thank you very much. That is just whay I needed. I see what the problem was. Great explanation as well :thumbsup:.

Why do you need this?

I'm basically trying to make a mission in which basically a group of scientists (the player) with some military training, captured by the enemy, attempt to escape to the frindly base. Meanwhile the friendly faction (Russia) fights against enemies (CDF and USMC), and if the player takes too long, the friendly base could be captured by the enemy. I want to make the battle very dynamic, and the results unpredictable. The map is divided into several capturable "strongpoitns". Once a faction has the most tropps in that "strongpoint", it will be deemed as captured by that faction, and as a reward to help the advance of that faction, a DAC zone will be created, generating new units for that faction (hence a faction becomes more powerful by capturing more strongpoints).

To do this, I need to contantly monitor the number of units for every faction in the area of every "strongpoint", hence why I asked my question. So I basically use the method that Muzzleflash explained, excpet for "_factionText" will this time be an array, so that I can count the number of units of each faction within the array using the "count" command. Other modders (such as Big D KS) have kindly helped me with the rest of the script. Oh, and I hope to release this mission :).

Once again,

thanks very much Muzzleflash

Share this post


Link to post
Share on other sites
Have you considered using the Seized by triggers?

After all of that pain... and yet the answere was so simple...

I had no idea. This will make my mission so much easier to make. Thanks for that mate :yay:.

Though I still need to check which specific faction the dominating units belong to (as the trigger is only side sensative, and cannot specify if for example it was USMC or CDF). Nevertheless, it does shorten my script significantly. Oh, and I'm guessing "thislist" in this case returns the side that has dominating presence?

Cheers.

Share this post


Link to post
Share on other sites

Kyfohatl, did you really need to start a new thread? Your other one would have been fine to use.

Also, this definately won't work:

_factionText = {(faction _x)} foreach _unitList;

ForEach does not return the array, nor does evaluating the faction of _X replace that element of the array.

Share this post


Link to post
Share on other sites
After all of that pain... and yet the answere was so simple...

I had no idea. This will make my mission so much easier to make. Thanks for that mate :yay:.

Though I still need to check which specific faction the dominating units belong to (as the trigger is only side sensative, and cannot specify if for example it was USMC or CDF). Nevertheless, it does shorten my script significantly. Oh, and I'm guessing "thislist" in this case returns the side that has dominating presence?

Cheers.

Just be aware that "seized by" may not be exactly what you want if you have other ideas of how much a unit is worth compared to others. An example I made:

In a seized by trigger I placed:

BLUFOR: 1 Abrams

OPFOR: A lot of infantry

The abrams even though less numerically won because, well, it's a tank.

The point where it switched to OPFOR was, (if I remember correctly!), somewhere between 70 to 80 infantry units.

Just something to be aware of. While it does make sense, that's not my problem with it, I do think that, seized by, is a bit optimistic about that tank.

Share this post


Link to post
Share on other sites
Just be aware that "seized by" may not be exactly what you want if you have other ideas of how much a unit is worth compared to others. An example I made:

In a seized by trigger I placed:

BLUFOR: 1 Abrams

OPFOR: A lot of infantry

The abrams even though less numerically won because, well, it's a tank.

The point where it switched to OPFOR was, (if I remember correctly!), somewhere between 70 to 80 infantry units.

Just something to be aware of. While it does make sense, that's not my problem with it, I do think that, seized by, is a bit optimistic about that tank.

Ooh, that sounds a bit dodgy. The reason why I was more intrested in the "siezed by" module was that the script I'm using only takes into accoutn the number of units, not their quality. Anyway, I think the script will be better, because the quality of units will balance out by itself. The area of the trigger that the units need to be in to "capture" the location is not very large, thus if units of one faction are of better quality but less numbers, they should be able to wipe out the enemy and capture the strong point.

Thanks

Share this post


Link to post
Share on other sites

You can always use seizedBy and then override the result when it returns true for something that should be false. I prefer seizedBy triggers in missions over count unit remaining triggers. Always a problem to find the last guy in some missions.

Also, consider a proper objective win instead of "you win - exit" or "you win - next objective" if units are still allowed to be in the area. It always felt a bit weird when you get "you win - fight the rest off". If these units aren't immediately removed (which also looks strange), it would be better if they "surrendered" (anim pose and captive) rather than remained combatants. Especially on CTI like missions. In some missions this might have a purpose though.

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  

×