How to use String functions in VASSAL

Jump to: navigation, search


Yes, you can use string functions in Vassal! In a beanshell "expression", java string functions are available for checking length, finding substrings, etc.

A key trick: I have found that I can't just pass in the property name directly, I have to use GetProperty.

So for example instead of e.g. LocationName.length() I find I need to use GetProperty("LocationName").length()

Examples of string voodoo I have successfully accomplished in Vassal:

EXAMPLE #1 Here is an expression I have used to check if the current location is listed in the table of victory objectives spaces (and give me 1 or 0 as the result). In this case I have two different AP_Objectives tables (AP_Objectives_0 and AP_Objectives_1) and I pick which one based on the global Variant property:


So the AP_Objectives_0 string looks something like "Metz Strasbourg Frankfurt Stuttgart Berlin ..." etc etc. Doesn't even matter for me that some names have spaces in them e.g. "Port Said", since none of the individual VP space names in the game will fall afoul of such a search.

I don't "really" need to use the "If" format, it was just convenient for me to translate into an integer.

EXAMPLE #2 Here I take a LocationName in the format "Turn 17", delete everything that isn't a 0-9 digit, and parse back into an integer value (which in the example of "Turn 17" would give me 17):


I would have expected I could just say LocationName.length() or whatever, but for whatever reason I only got it to work once I started using GetProperty.

I hope this is helpful! It was hard-won knowledge since I couldn't find functioning examples anywhere!

You can google "java string functions" for a complete list of such things.