VASSAL Reference Manual
Expressions
Any field within the Vassal Editor that is followed by a Calculator icon allows the entry of an Expression. Expressions are essentially an in-line Calculated Property, re-evaluated every time VASSAL needs to know their value. There are two main flavors of Expression: BeanShell Expressions which are identified by being surrounded by curly braces {}, and Simple Expressions which are not.
Property Match Expressions select which pieces to operate on by checking properties, e.g., in a Global Key Command.
The Expression Builder makes building expressions from available properties, operations, and methods a bit more convenient.
Note
|
Beanshell expressions should not be confused with "old style" Simple Expressions, which have a different syntax and are far more limited. |
BeanShell Expressions
BeanShell Expressions (named after the software package which implements them in Vassal) allow you to use arbitrarily complex formulae to define the value returned. BeanShell Expressions can be identified by their surrounding curly braces {}. The BeanShell processor is an accurate implementation of the Java programming language and BeanShell Expressions consist of Java language components. You can use any introductory Java tutorial to learn more about the syntax of BeanShell Expressions. The basic components are as follows:
Type | Options | Examples | Notes |
---|---|---|---|
Numbers |
{35} |
Vassal works with whole numbers. It stores decimal numbers as strings. |
|
Strings |
{"German"} |
A String literal, or text message, must be enclosed in "" quotation marks |
|
Comments |
/* Check not flipped */ |
Comments can be included in-line in an expression in any break between elements. .e.g.
Adding comments to expression does not affect performance in any way. |
|
Properties |
{Nation} |
VASSAL interprets any word not enclosed in "" as a Property name. Can refer by name to Global Properties, Marker and Dynamic Properties of pieces, and properties exposed by traits. The current value of the property will be used. |
|
Substitution |
$..$ |
{"$Nation$"} |
Property names enclosed by $..$ symbols immediately substitute the property value, based on the piece or component issuing the command. Should only be used in Property Match Expressions to use properties from the issuing piece rather than target piece. |
Arithmetic |
+ Add |
{(CurrentHP + 2) * (Damage - Resistance)} |
Using the + operator on two String will concatenate (join) them. If the values on both sides of the + look like whole numbers, Vassal will attempt to add them. Parentheses can be used to organize operations and/or override the ordinary order of operations. |
Comparison |
> Greater than |
{height > 10 } |
Comparison operators come in handy for Property Match Expressions and also with the ? (ternary) operator. Parentheses can be used to organize operations and/or override the ordinary order of operations. For more examples and information about Regular Expressions see the dedicated RE page. |
Logical |
&& Logical AND |
{Nation=="Germany" && Type == "Unit"} |
Comparison operators are used primarily in Property Match Expressions and also in the If function. |
Ternary |
Expr ? IfTrue : IfFalse |
{ (Nation=="Germany") ? "Axis" : "Allies" } |
The Ternary or "?" operator can be thought of as posing a yes/no question: if the expression before the ? symbol evaluates as "true", then the value of the expression as a whole is equal to the middle ("if true") part of the expression; otherwise (initial part is false), the result is the right ("if false") side. |
Math |
Math.abs |
{Math.abs(Number - 5)} |
Math.abs() Returns the absolute value of the numeric property value1. Math.min() Returns the smaller of two numeric values. Math.max() Returns the larger of two numeric values. |
Property |
GetProperty GetAttachProperty |
{GetProperty("Nation"+myNation)} |
All GetProperty() family functions return the value of a named property. The name of the property can be constructed from an expression. See the Property Functions page for full details. |
Random |
Random |
{Random(6)} |
Random(value1) returns a random number between 1 and value1. Random(value1,value2) returns a random number between value1 and value2. IsRandom() returns "true" 50% of the time. IsRandom(percent) returns "true" the specified percent of the time. |
String Methods |
length |
{ LocationName.length() } |
See the String Function page for examples and more information about String Functions. |
Sum and Count |
SumStack SumMat SumAttach SumMap SumLocation SumZone SumRange Sum |
{SumStack("Attack")} {SumMat("AttackFactor")} {SumAttach("MyMinions", "Ammo")} {SumLocation("Strength")} {SumZone("Strength")} {Sum("Strength", "{Owner=\"me\"}"} |
There is an extensive range of Sum and Count functions to count pieces and to sum properties attached to pieces. See the Sum and Count Functions page for more details on choosing and using these Functions. |
Range |
Range |
Range(0, 0) |
Range functions calculate the range in pixels, or in grid units (hexes, squares) between 2 locations. See the Range Functions page for more details on choosing and using these Functions. |
Alert |
Alert |
{Alert("Adding 1 to HP")} |
Alert displays a message in a popup dialog box, to the currently active player only. If a message needs to be shown to all players, the recommended method is to send something to the Chat Log, e.g., with a Report Action trait or other Message Format field. |
Simple Expressions
Simple expressions—expressions not surrounded by braces—exist to provide compatibility with earlier versions of Vassal that only implemented a much simpler version of Expressions. Simple Expressions are far more limited than BeanShell Expressions, only allowing the substitution of property values into a pre-determined string. If you are learning VASSAL for the first time, you should mostly concentrate on learning the more powerful BeanShell Expressions.
A Simple Integer Expression: |
A whole number. In general, Vassal does not support decimal numbers, except when stored and used as Strings. |
|
A basic Simple String Expression: |
A String in a Simple Expression does not need quotation marks. |
|
A more complex example: |
The string $Nation$ will be replaced by the value of the Nation property. You can use multiple $…$ strings in an expression ($Nation$-$Division$), but CANNOT nest them ($Nation$Count$$). |
SEE ALSO: Properties