[messages] [Module Design] Stacked pieces

JustaBill martinson2005 at netscape.net
Sat Jul 14 20:52:33 MST 2012

Hmm, so I just stumbled onto a pretty simple solution that should work
most of the time.  

Let's say you have an existing 'result' command, Ctrl-R, that 'does
something' with a piece, but you want to automatically apply that
command to only one piece from every selected stack; that is, you want
to choose multiple stacks, invoke something like Ctrl-1 (possibly from a
right-click on one of the selected pieces), and have it apply Ctrl-R to
only one piece per stack, without having to initiate this using
something external to the pieces themselves (like a global Action
Button).  (In the OP's case he also wanted to 'do something else' with
the remaining pieces in the stack, but it should be obvious how to
extend what I'm about to say to accomplish that.)

This method exploits the fact that when you select multiple stacks and
invoke a Trigger Action, Vassal appears to apply that action
stack-by-stack, meaning it applies it to each piece in stack 1, then
each piece in stack 2, and so on.  This discovery, combined with the
knowledge that all the pieces in a given stack have the same X and Y
coordinate, allows us to write traits that perform the action when the
piece currently being considered has a different location than the piece
previously moved.

Ctrl-R - 'Do Something' result command

Ctrl-X - Set Global Property - sets GenericX = $CurrentX$
Ctrl-X - Set Global Property - sets GenericY = $CurrentY$ (so a single
invocation of Ctrl-X copies the piece's location)

Ctrl-1 - Trigger - Do Something to 1 Piece Per Stack
Trigger when Properties Match: CurrentX != $GenericX$ || CurrentY !=
Perform these Keystrokes: Ctrl-X Ctrl-R

That's it.  Each time a piece is processed, if either its X location or
its Y location is different from the piece previously affected, it must
be in a different stack and is therefore given the result action (right
after copying its own location for subsequent comparisons).

The reason I say this will work most of the time is because there's a
risk of overlap from a previous use of the command.  If the first stack
processed this time around happens to be the last stack processed last
time around, then a piece from that stack will not be affected like it
should be.  You'll have to analyze that risk in the context of your
specific game implementation to determine whether it's a show-stopper
for you or not.  If you know that some other action or type of action
will always occur in between two different Ctrl-1 uses, then you can
have that other action set GenericX and GenericY to wacky values to
ensure that a fresh comparison will always be performed.  If not, then
you will have to move this functionality to something invoked from
somewhere other than the pieces themselves, so you can begin by
initializing GenericX and GenericY with unusual values before any
comparisons are performed.

Read this topic online here:

More information about the messages mailing list