Calculated Property - Timing of Calculation and ???s

Folks,

I’ve gone back to a couple of modules that are pre-3.2 and I’m updating some of the trait sets. However, there are a couple of concepts about Calculated Properties that I don’t understand yet.

  1. When does the calculation occur? I’ve been trying to work out where to place the CP traits amongst the other ones already present, as I need some of the calculations to occur before various triggers and reports are generated. Two cases:

1a) If the CP is called “Total” and the expression is “A+B”, then when are A and B summed? Are A and B summed when that trait is reached in the list of traits, or is it summed when “Total” is called the first time? Or is it neither?
1b) If the CP is called “Total” and the expression is (pseudo-code) “If DeckName==DiscardPile || CurrentZone==AllocatedZone, Cards = Cards + 1, Cards”, when is “Cards” calculated?

  1. If the new Expressions are based on BeanShell, is there a way to have the Alert box text accept a CR? I have a few messages that need to pop up in the middle of a series of traits, but the text is a bit long, so a carriage return would help immensely with formatting. However, “/n” doesn’t seem to achieve this. Is there an escape code or some other “slash code” that I need to use?

  2. Does “SumStack” work in an analogous way to the commands that allow you to calculate the number of cards remaining in a deck, or am I misunderstanding its usage?

  3. If the “Compare” command for properties allows you to compare objects, can it accept “null” as a parameter? I tried to use this to test whether or not something had been set by another trait, but a null value just generated an error. How do you use this?

Thanks to everyone for the work on 3.2. I’m finding all sorts of new ways to enhance my modules. 8)

Whenever a reference to it in another trait or component is evaluated.

Each and every time a trait referencing Total is evaluated.

I do not understand the ‘Cards = Cards + 1, Cards’ part. A CP can never change the value of a property, it only returns a value. It is recalculated each time it is needed.

Not sure about that one, will look into it.

It could be used this way if the property you where summing had the value of ‘1’ in each unit in the stack.

Use the empty string “” to compare to something that has not been set. Everything in Vassal is strings.

Brent,

Thanks for the valuable info–I was beginning to wonder if I had committed a forum no-no since nobody had replied. :confused:

To clarify a few items:

This answers a big question of mine, as I thought you had to add a trigger in there somewhere to specifically force the CP to calculate. Man, that’s gonna simplify some code in a hurry!

Ah…I was thinking of this in terms of addition in Java and I messed up the code. “Cards” should be “Total”. Oops. :blush: I wanted to increment the total number of cards by 1. If I had used “Total”, would this work, or would I need to code it differently?

Is there a way to use null for anything at all, or is that an invalid parameter at all times? Should I always use the “empty string”? The reason I’m being this specific is that I need to check for whether some items are “offboard” or not. In the past, using “offboard” as a parameter didn’t work, so I had tried using “null”, meaning that the piece did not have a valid location set, but to no avail. Would “empty string” be the logical way to handle this at present, or is there something that I’m missing?

Also, on a somewhat related note, has anyone expressed interest in setting up a howto or “learn by example”-type subforum for coding? I’ve been doing a lot with automation and I know Dr. Nostromo has in the past as well. If I was to submit some of my examples or code walkthroughs, where would be the best place–here in Module Design, or in a different/new location? Just wondering…it’s taken me a while to figure some of this stuff out, but I’d love to learn more and trade ideas.

Thanks again!

Yes, but it means you have to be careful. If you have a complex CP that is being re-calculated often (e.g. every time a piece is redrawn), then you may experience performance issues.

A CP has no side effects - it cannot change any global properties or trigger any other traits. To increment a property named Total by 1, you would need to use a Set Global property, or Dynamic Property trait appropriately triggered.

Java does not have the concept of null, so it it cannot be used in a beanshell expression. The empty string “” is equivalent to null. If property xxx has no value then {xxx==“”} is true.

A piece will report it’s location as “offboard” if it is in a border defined around the edge of a board. In this case, comparing to the string “offboard” should work. Depending on how you have set up boards, maps, and zones, pieces may return any location name at all including “”. In this case, comparing to “” should work.

The wiki would be the best place for this. The wiki needs a friend to sort through it, re-organise and bring it up to date.

Brent,

Thanks again for the additonal help.

Yes, I thought about the constant re-calculation aspect of this. I’ll have to check that against my coding model, but I think that may actually solve a problem for me in this case, as I need a couple of calculations to be constantly updating.

This is good information as well–thank you. I was under the impression that part of my IF clause could be the alteration of another property–apparently not.

As far as my “offboard” issue goes, I seem to recall in the past that any piece that was not currently residing in a defined zone, region, or other grid area was considered “offboard”. I may be remembering that wrong, as the module in question has changed several times now. Does that sound right to you? Also, I’m not sure exactly what you mean by “border defined around the edge”. Could you explain that a bit? I always add some extra pixel area to my map images for counter stacks and decks, sort of like a virtual table. Is this what you mean?

yes

As an alternative to adding extra space to around the edges of you board images, you can specify Horizontal and Vertical padding in the Map Component that will automatically add extra space around the boarder of your map.

Thus spake Brent Easton:

Is there a way to use null for anything at all, or is that an invalid
parameter at all times? Should I always use the “empty string”? The
reason I’m being this specific is that I need to check for whether
some items are “offboard” or not. In the past, using “offboard” as a
parameter didn’t work, so I had tried using “null”, meaning that the
piece did not have a valid location set, but to no avail. Would “empty
string” be the logical way to handle this at present, or is there
something that I’m missing?

Java does not have the concept of null, so it it cannot be used in a
beanshell expression. The empty string “” is equivalent to null. If
property xxx has no value then {xxx==“”} is true.

Could you clarify this point, Brent? Java definitely has ‘null’ as
a keyword, and the value of a String reference can be null. Is there
a different between Java and BeanShell here, or is our implementation
more restrictive in some way?


J.