I want to create global property names that have dynamic portions.
Still struggling with getting this working.
I have a predefined list of Global Properties like:
gLocationSoviet3A=0
gLocationSoviet10A=0
For example, when the piece for Soviet 3A is placed, I’d LIKE to set the Global Property by calculating the variable name in real time in the prototype instead of hard coding it in each separate piece.
The goal would be to create the GP name (in this case gLocationSoviet3A and assign a value with a SetGP trait inside the piece.
Here is my Set GP dialog (right now, defined in the piece, not a prototype…)
This runs OK, but I don’t see the value in the GP ever change - I see the report action when the piece moves, so it should be triggering the SGP as well since they react to the same event.
When I attempt to print out the value in the Report Action using the constructed name to try and get a value:
ex: {$(“gLocation”+Side+BasicName)} -or-
{ $(“gLocation”+Side+BasicName)$ }
You can’t use $$ to construct the GP name inside the curly braces {} in this context. This method does have use in a search type function (it means “substitute the current value of the enclosed property, rather than the value on the target piece”).
For your application, I think just removing the curly braces will do the trick. That puts you in legacy mode, where the $ enclosed names will be evaluated and substituted to yield the actual GP name.
e.g. gLocation$Side$$BasicName$ might give you a GP of gLocationSoviet3A
That doesn’t work if you need indirection (a second level of substitution). In that case, go back to beanshell (curly braces) and use the GetProperty() function.
I see local loc = 622 (correct)
I see loc =
nothing - no value is printed out from GetProp function call… I am using the global property name literally just to check to see if it is set to anything…it appears that it is still empty.
In general, don’t use $variable$ if Beanshell is available (Calculator icon on the field).
If BeanShell is available then from the point of view of an trait in the piece:
{"gLocation"+Side+BasicName} is the name of the GP you are interested in and {GetProperty("gLocation"+Side+BasicName)} is the current value of that GP.
As mentioned, $variable$ should only be used in Global Command Property Match Expressions for the specific purpose of replacing sections of the expression with values from the piece issuing the Global Command.
The fact the $variable$ syntax works in some usages in various fields is a historical anomaly, you can’t depend on it.
If you want to learn a bit more about Beanshell syntax, read the chapter on expressions in any Java beginners course. Beanshell is pure Java syntax.
I suppose I could, but I want to learn how this works…
Initially, I wanted to have access to some data for a small number of pieces, in this case, HQs. There are only about 20 total and usually not all in the game at the same time.
The game uses supply lines (terrain that is a road, rail, or town) to determine in or out of supply. Each supply line can be interrupted by the other side’s pieces. I was using the global props to maintain essentially a list of occupied terrain (location) as well as side and type of unit on that terrain so I can examine them during a supply check activity.
Ultimately, I may go a different direction altogether, but it was fun learning about it.