Calculate the Sum of the Cost of all pieces in a stack

Hello everyone, I’ve often read these forums but never posted until now.

I have a module that uses the Deck feature to (among other things) count the number of pieces in a deck. For example, if I put two Stormtroopers and an Elite Stormtrooper in a deck, then it shows the “Activation” count (ie, unit count) as 3. Good so far.

Now I want to know how to Sum the total cost of those pieces. For example, if the Stormtroopers cost 5 each and the Elite Stormtrooper costs 12, I want it to provide me with the Sum (22 in this case).

Thanks to prototype definitions, every piece in the module has a Marker (Activation=1). I’m pretty sure that eventually I’ll need to add a Dynamic Property or another unique marker to indicate each piece’s Cost. However, at this point I need help with the actual formula or command line to calculate the Sum.

Do I need to do this via BeanShell coding? If so, would someone please suggest a line that I could copy into the “Expressions to Count” field in the Deck? I’m sorry if this seems like a noob-ish request; I’m very new to BeanShell coding and after a couple of hours of trial and error, I’ve come here for help.

Thanks in advance.

If the cost is fixed, a Marker trait is fine. If it can vary, you should use a Dynamic Property.

Pretend you assign a Cost property to your pieces with a Marker trait. The Expressions reference page shows a Beanshell trick you can adapt to use in a Text Label:

{Sum("Cost", "{DeckName==\"Your-Deck-Name-Here\"}")}

Maybe if you want to append some text to make it look more readable:

{"Total Cost: " + Sum("Cost","{DeckName==\"Your-Deck-Name-Here\"}")}

Note the warnings on the reference page about the possible performance impact if you have many pieces in your game. This will work without having to add anything to the “Expressions to count” portion of the deck config.

2 Likes

Bingo! It works! Thank you so much.

Sounds like this technique might be useful for many attributes on a stack of counters. For example the sum of attack values for the attacker versus the sum of defense values for the defender. Or perhaps the min of the movement attributes for stack of counters that make up a corp.

Yes, you could use this similar function for that use case (also mentioned on the reference page): {SumStack("Attack")}

1 Like