A Tip for using numbers in Vassal

Thought I’d pass on some of my hard earned knowledge. Vassal generally does not like decimals when using calculated expressions, etc. Like it will not multiply something by 0.5, but it can be sort of fooled by using 1/2, But there is another way around this.

(Math.round((float)(If(($jares$+$jaoil$-$oilsave$)>($jared$+$jablue$),($jared$+$jablue$),($jares$+$jaoil$-$oilsave$))*$prodm$)))

The example above illustrates this. Adding Math.round at the front will round the results to the nearest whole number. Use of the (float) before the actual equation being evaluated allows the use of decimals. In the module using this example, the last term prodm can be 0.25 and multiples of this base. The beast works nicely. Attached pic shows this example in use.

Nice! I found the various java string functions likewise work in this sort of context (e.g. GetProperty(“VictoryPointSites”).contains(GetProperty(“LocationName”))==true) but hadn’t tried the math ones yet. Must remember next time I wish there was an easier way to do an absolute value!

ONE NOTE – since you’re using those in a Beanshell expression to have access to the math functions and “If”, you don’t actually need to put the $ around the variable names.

You are so right re the $x$. I used them as I was initially getting a lot errors and using the $$ seemed to solve them. On reading your post, I revisited the equations and removed the $'s. Still works, yeh. I did try Math.min() first but min became an unrecognised something. So I went for the If() statement approach. Using min would be more elegant. Perhaps I stuffed up the syntax or something.

Maybe you here in this thread can help me.
I’ve been trying to get a simple math calculation to show the Modified Die roll in the report line.
I have the die Result $result$ and a global property $Att_DRM$ (which is set as a numeric value by a “turn counter”)
I know it can’t be that hard to total these two values and report them in the widow. But I can’t seem to get it to work.
Report line in Dice button would read:
$name$ = [$result$] Modifier= {$Att_DRM$], Modified Die Roll =[???] *** $PlayerSide$, $PlayerName$
I can’t get any expression to work where the ??? is in the above. What am I overthinking?

So I have been having an issue trying to get the total of a Die roll result ($reslut$) plus a Global Dynamic property $att_DRM$ (this later one is set by a numeric “turn counter”)

I just want to show the total in the Message reported.

The ??? in the beelow is the part I can’t get to work, I’ve tried “Get Property” and several other variation of converting a string to an Integer but nothing seems to work. I assume I am over thinking this but just don’t see it.
Any help would be appreciated:
In the report box I have:
** $name$ = [$result$], Modifier = [$Att_DRM$] Modified Result = [???]*** $PlayerSide$ **<$PlayerName$>

Unfortunately I don’t know of any way (short of writing a custom Java class – a pretty heavy lift) do any arithmetic in direct response to a die roll. You can have the Modifier be displayed if you stash it in a Global Property beforehand (which is I assume what Att_DRM is for you in your example), but since the die-roll thing doesn’t process triggers and things like a normal piece does, I’m not sure how to get a calculated property to take effect.

One “long shot” you could try would be this: (it depends on if the $result1$ of the die roll is available to other pieces after it is generated)

  1. You have your die roller thing
  2. You make a button somewhere on your map that is the actual “roll the combat die” button (you can even give it an “Action Button” property so that you can click right on it to activate it).
  3. A trigger on your button activates a “Global Hotkey” trait that sends the hotkey for your die-roller, causing it to roll.
  4. Your button contains a Calculated Property, e.g. “ModifiedRoll”, the expression for which is result1 + Att_DRM
  5. The second thing your trigger does is activate a Report trait, which can now include $ModifiedRoll$
    Maaaaaaybe that would work… haven’t tried it. Only thing I can think of.

Brian

Thanks Brian I wasn’t very clear - the Report line was in the Dice Button Report line, Would have thoguht I could just add the two together to place in the report string not trying to do anything fancy with the summed value.
I really hate having to add a class to do simple math.
As A note: the above report line does ahow the REsult of the die roll and the Att_DRM so it should be possible to get thme to be added together to display.
I’ll keep trying and check back in case anyone else comes up with a way ot do this.

A thought. If the text is really a number (integer), have you tried to multiply that by 1? Works some times.

That said, I have never struck a problem with a text trait that is only a number. Calculations work as if it was a number.

Re the (???). The result + Att_DRM proposed by Cattlesquat should work as both variables are known.

Keep trying. The solution is there, just undiscovered atm. :smiley:

If it looks like a duck…

All numbers in Vassal 3 are stored as strings. If you try and do arithmetic on two values, Beanshell tries to convert both to numbers before doing the arithmetic operation, then converts them back to Strings to be saved.

well I give up No matter what I try I can not get the result of a die roll ($result$) to be added to Value of $Att_DRM$ (global property set by player before die roll) in side the REport line (of the Die roll definition). IT would seem simple but what ever underlying code is going on in the Vassal Engine Just won’t recognize any expression I have use. The Add to each die and add to total, boxes will only accept actual numbers (again any kind of Expression or just the Global property will not take).

I’d like to think it is just me missing some sort of syntax, but it seems to be way more complicated - I don’t want to add a piece somewhere that you have to scroll to to get the modified result. Just want it recorded in the log.

Maybe they will allow this in 3.3. I might try to copy my Vmod to a new name and try to see if I can make it work there.

If there is someone who has done this out there please post here (Again I don’t want to create new pieces that act like buttons etc. just to get A+B in a report line in the message window/log file).

For now people will have to do the math themselves. Thanks for all the suggestions, Wish I could have made it work.

The Dice Button class has not been engineered to allow the sort of thing you describe.

However, Dice Buttons do expose their result values as an external property with a name of ‘xyzzy_result’ where xyzzy is the configured name of the Dive Button.

So while you can’t do the whole process inside the Dice Button itself, you could Trigger a Dice Roll using a Global Hotkey, and then use the result xyzzy_result in a Calculated Property that calculates the combat result as {xyzzy_result + Att_DRM}.

Thanks for the information, My initial query would be Why not? Especially the “add to each die” and “add to total” boxes should allow a Global property to be used. That was the real shocker initially for me. They seem tailor made for such an application.

I tried doing the global key stuff but I still need a “piece” to give the calculated value to it seems (only place I find that available).
However this is just to report it in the Message window, don’t want to do that much work and allowing an inadvertent messing with a “piece” on the map, just to report something for convenience sake.

If I were to use it say to look up combat results, etc. I would be adding a new Class and could handle it there.

Would the developers add the ability to specify a global property in add to each die/total parts of the Dice button? It would be nice.