The Attachment trait could indeed help you here, and might be the most efficent way to do this. You make sure that dismount1 and dismount2 have the same Attachment name - say the name of the mounted unit - say mounted.
To check if the two attached pieces are in the same location (hex), you can use the comparison
LocationName == mounted_LocationName
You can also use the Range(attachment) BeanShell function to check that the distance between the two dismountX units is zero, as in
Range("mounted")==0
If you detect, say in a Trigger Action trait that the two dismountX units are in the same location, then you can use a Global Key Command with Current Attachments Location filter to remove the other counter, or similar. You can also use the Property filter with
LocationName == "$LocationName$"
and then you probably do not need the Trigger Action trait at all. Note, you need the quotes around $LocationName$, because $...$ properties in BeanShell expressions are textually replaced before the expression is evaluated. So if the sending piece has the location A1, then the above expression becomes
LocationName == "A1"
Without the quotes, it would be
LocationName == A1
which would be a comparison between the values of the properties LocationName and A1 in the destination piece. Note also, if you LocationName properties are strictly numerical, and never has leading 0s, then you probably do not need the quotes. If LocationName is numerical, but with leading ‘0’, then you may need to do
GetString("LocationName") == "$LocationName$"
to coerce the destination LocationName to be evaluated as a string.
In fact, you can avoid the Attachment trait all together by a similar idiom. Suppose both dismountX have the same property - say Mounted (f.ex. defined via a Marker trait), and they both have the same value for that property - say "mounted". Then you can use a Global Key Command with a Location filter of
{"$LocationName$"}
and a property filter of
Mounted == "$Mounted$"
and possibly a restricted range of 0.
However, there might be a performance difference between the two options (using an Attachment trait with a Global Key Command trait, versus just using a Global Key Command trait). Attachments are stored in memory by Vassal, meaning when a piece sends commands or queries its attachements, then Vassal does not need to go look for the attached pieces - its there directly. Without the attachment, then Vassal would need to query all pieces on the map to see if they fulfill the requirements.
That said, I would recommend that you test the different approaches and see what works best for you. The second approach can be more attractive if you want to define this behaviour in a Prototype. The Attachment trait would have to be defined for each individual piece. On the other hand, Attachment traits allow you to send commands between the pieces in an effective fashion.
Sometimes it is really helpful if you can provide a link to the module in question, so that others may see for themselves what it is you are doing and want in the proper context.
Remember to mark the solution to your problem - which isn’t necessarily this one
. It really helps others find the solution to their problems. Only you, @sfcmikej, as OP can do that.
Yours,
Christian