Hi,
Are there any string functions in VASSAL? I can’t see any in the Designer Guide.
Specifically I need to (a) find the length of a string and (b) extract the Nth character.
Thanks,
Ian.
Hi,
Are there any string functions in VASSAL? I can’t see any in the Designer Guide.
Specifically I need to (a) find the length of a string and (b) extract the Nth character.
Thanks,
Ian.
Yes! In a beanshell “expression”, java string functions are available. So you would be looking for the str.length() and str.charAt(n) functions.
But 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:
#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:
If(GetProperty(“AP_Objectives_”+Variant).contains(GetProperty(“LocationName”)),1,0)
#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)
Integer.parseInt(GetProperty(“LocationName”).replaceAll("[^0-9]",""))
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!
Brian
Thanks Brian! This is great stuff.
I know this is going to be very useful except… right now I can’t quite remember what I needed it for.
Never mind this kind of not-in-the-manual knowledge is very much appreciated.
Ian.