How to Manipulate Strings or Count by Letters

From Vassal

Howdy all,

While building a module for a game played in phases designated by letters, I faced the challenge of how do I get VASSAL to count by letters. In the documentation for expressions I saw that BeanShell expressions and Java methods (functions) could be used. So I looked up some Java string methods (and just now discovered many of them directly referenced in the documentation) to facilitate my module building efforts. I was building a game piece that would change appearance with each new phase using layers. I had already defined a global property, CurrentPhase to indicate the game’s phase in letters, but layer levels are designated with numbers. Wanting to keep the phase designation in letters for chat/log reporting clarity, I thought about making the conversion.

I defined another global property, a string called PhaseLetters with all the letters in order used in phase designations. For this game it is “A” through “H”. So PhaseLetters is “ABCDEFGH”. In the layers trait I converted the phase letter to a number by the expression {PhaseLetters.indexOf(CurrentPhase)+1}. I added the “1” to account for the fact the indexOf() starts counting with “0” and layer levels start counting with “1”. It worked great on the first try!

Going the other way, which I plan to do later, I intend to use charAt() when the letter designations are a single character, and substring() when one of the designations uses more than one character.