Create account / Log in

Calculated Property - Timing of Calculation and ???s

Talk about module design ideas and techniques.

Moderators: Tim M, uckelman

Calculated Property - Timing of Calculation and ???s

Postby Rommel14 » August 11th, 2013, 4:00 pm

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?

2) 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?

3) 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?

4) 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)
"For to win 100 victories in 100 battles is not the acme of skill.
To subdue the enemy without fighting is the acme of skill.
Thus, what is of supreme importance in war is to attack the enemy's strategy."
-- Sun Tzu, The Art of War
User avatar
Rommel14
 
Posts: 39
Joined: June 27th, 2011, 12:58 am

Re: Calculated Property - Timing of Calculation and ???s

Postby Brent Easton » August 19th, 2013, 6:29 am

1) When does the calculation occur?


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

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?


Each and every time a trait referencing Total is evaluated.

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?


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.

is there a way to have the Alert box text accept a CR?


Not sure about that one, will look into it.

3) 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?


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

4) 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?


Use the empty string "" to compare to something that has not been set. Everything in Vassal is strings.
User avatar
Brent Easton
 
Posts: 2757
Joined: December 21st, 2007, 3:06 am
Location: Berry, NSW, Australia

Re: Calculated Property - Timing of Calculation and ???s

Postby Rommel14 » August 20th, 2013, 12:12 am

Brent,

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

To clarify a few items:

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?

Each and every time a trait referencing Total is evaluated.

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!

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?

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.

Ah...I was thinking of this in terms of addition in Java and I messed up the code. "Cards" should be "Total". Oops. :oops: 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?

4) 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?

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

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!
"For to win 100 victories in 100 battles is not the acme of skill.
To subdue the enemy without fighting is the acme of skill.
Thus, what is of supreme importance in war is to attack the enemy's strategy."
-- Sun Tzu, The Art of War
User avatar
Rommel14
 
Posts: 39
Joined: June 27th, 2011, 12:58 am

Re: Calculated Property - Timing of Calculation and ???s

Postby Brent Easton » August 20th, 2013, 1:19 am

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!


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.

Ah...I was thinking of this in terms of addition in Java and I messed up the code. "Cards" should be "Total". Oops. :oops: 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?


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.

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.

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.

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.


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.
User avatar
Brent Easton
 
Posts: 2757
Joined: December 21st, 2007, 3:06 am
Location: Berry, NSW, Australia

Re: Calculated Property - Timing of Calculation and ???s

Postby Rommel14 » August 20th, 2013, 2:06 am

Brent,

Thanks again for the additonal help.

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.

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.

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.

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.

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.

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?
"For to win 100 victories in 100 battles is not the acme of skill.
To subdue the enemy without fighting is the acme of skill.
Thus, what is of supreme importance in war is to attack the enemy's strategy."
-- Sun Tzu, The Art of War
User avatar
Rommel14
 
Posts: 39
Joined: June 27th, 2011, 12:58 am

Re: Calculated Property - Timing of Calculation and ???s

Postby Brent Easton » August 20th, 2013, 2:21 am

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?


yes

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?


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.
User avatar
Brent Easton
 
Posts: 2757
Joined: December 21st, 2007, 3:06 am
Location: Berry, NSW, Australia

Re: Calculated Property - Timing of Calculation and ???s

Postby uckelman » August 20th, 2013, 2:13 pm

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.
User avatar
uckelman
Site Admin
 
Posts: 8140
Joined: December 10th, 2007, 9:48 am
Location: Durham, England


Return to Module Design

Who is online

Users browsing this forum: No registered users and 0 guests