Problem with foreach
-
I am receiving this error with a foreach loop
GameParseException: Attachment has invalid variables in foreach: $Major_Power$
Here is my code
<variable name="Major_Power"> <element name="Soviet_Union"/> <element name="Western_Europe"/> <element name="Britain"/> <element name="United_States"/> </variable>and
<attachment foreach="$Major_Power$" name="triggerAttachmentsubmarine1@Major_Power@" attachTo="@Major_Power@" javaClass="games.strategy.triplea.attachments.TriggerAttachment" type="player"> <option name="conditions" value="conditionAttachmentEveryTurn" /> <option name="purchase" value="submarine1" count="1" /> <option name="players" value="@Major_Power@" /> <option name="chance" value="1:20" /> <option name="chanceIncrementOnFailure" value="1" /> <option name="when" value="before:@Major_Power@Purchase" /> <option name="productionRule" value="production@Major_Power@:buysubmarine1"/> <option name="uses" value="1"/> </attachment>Any idea what the problem is?
-
@wc_sumpton would be the one to ask. I still struggle with foreach myself

-
@beelee I have used foreach successfully before.
-
I have as well just not always
Just pinging wc so he'll see it -
Why "players" when this will be the same as "attachTo"? Just a question?
"GameParseException: Attachment has invalid variables in foreach: $Major_Power$" usually means one of the variables are wrong. Could be a miss spelling. So, recheck there. Beyond that it's hard to tell with the information given. Sorry.
Cheers...
-
@rogercooper said in Problem with foreach:
Major_Power
After further review, your problem may be the "_". Instead of "Soviet Union", there is "Soviet_Union". The "attachTo" (and "players") must be the actual player name. If there are both, player "Soviet Union" and "productionSoviet_Union" then you will need two variable lists and they will need to be separated by the carrot "^" and not the colon ":".
There could be other problems, but without more information, I'm just shooting fish in a barrel.
Cheers...
-
@wc_sumpton said in Problem with foreach:
@rogercooper said in Problem with foreach:
Major_Power
After further review, your problem may be the "_". Instead of "Soviet Union", there is "Soviet_Union". The "attachTo" (and "players") must be the actual player name. If there are both, player "Soviet Union" and "productionSoviet_Union" then you will need two variable lists and they will need to be separated by the carrot "^" and not the colon ":".
Cheers...It turns out that the production frontier was misnamed. However, fixing that did not resolve the issue. I am now using "Soviet_Union" in all places
-
@wc_sumpton said in Problem with foreach:
Why "players" when this will be the same as "attachTo"? Just a question?
"GameParseException: Attachment has invalid variables in foreach: $Major_Power$" usually means one of the variables are wrong. Could be a miss spelling. So, recheck there. Beyond that it's hard to tell with the information given. Sorry.
Cheers...
I was copying existing code. If I understand correctly, "attachTo" is arbitrary and "players" is required to be sure that the correct player is involved.
-
@rogercooper I tried simplifying the situation.
<variable name="Major_Power"> <element name="United_States"/> </variable><attachment foreach="$Major_Power$" name="triggerAttachmentsubmarine1United_States" attachTo="United_States" javaClass="games.strategy.triplea.attachments.TriggerAttachment" type="player"> <option name="conditions" value="conditionAttachmentEveryTurn" /> <option name="purchase" value="submarine1" count="1" /> <option name="players" value="United_States" /> <option name="chance" value="1:20" /> <option name="chanceIncrementOnFailure" value="1" /> <option name="when" value="before:United_StatesPurchase" /> <option name="productionRule" value="productionUnited_States:buysubmarine1"/> <option name="uses" value="1"/> </attachment>This gets the error. If I remove **foreach="$Major_Power$" ** the error disappears. (I also tried without the underscore to no avail). I also tried removing every option in trigger.
Here is some code that definitely does work
<attachment foreach="$LandArea$" name="triggerAttachmentZombiesrussianZombie@LandArea@" attachTo="Zombies" javaClass="games.strategy.triplea.attachments.TriggerAttachment" type="player"> <option name="conditions" value="conditionAttachmentEveryTurn" /> <option name="placement" value="@LandArea@:zombie" count="1" /> <option name="players" value="Zombies" /> <option name="chance" value="2:70" /> <option name="when" value="before:russianPurchase" /> </attachment> -
@rogercooper SOLVED IT. I forgot the variableList tags.
-
@rogercooper said in Problem with foreach:
I was copying existing code. If I understand correctly, "attachTo" is arbitrary and "players" is required to be sure that the correct player is involved.
If "players" is used, then "attachTo" is ignored. I only use "players" when the trigger affects more than one player. But to each his own.
@rogercooper said in Problem with foreach:
@rogercooper SOLVED IT. I forgot the variableList tags.
Would have never guessed this! Happy you have figured it out.
Cheers...
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login