Using preferences checkbox boolean as an integer

I have a piece layer trait which I want to follow a global preference. It works perfectly fine as a whole number (zero or not zero) but it would be cleaner if I could make it a checkbox and treat “false” as zero and “true” as one. Am not having any success making the expression work. Either it’s a royal pain or I’m having a brain freeze and missing something easy.

Thanks

OK - I’m getting somewhere with preference_name.compareTo(“false”) which returns integer 0 when the checkbox is clear.

compareTo(“false”) is not a good choice; while it does return 0 if the values match, it’s only going to return 1 if they don’t match if the actual value of the string begins with the letter “e” (one letter before “f”); i.e., if the string is “true”, it will return -20, because “f” is 20 characters before “t”. I believe the expression you want is preference_name ? 1 : 0 . That will return 1 if the preference is true, 0 if it’s false.

I can use that syntax? Wow

You’re right, that works like a charm. Thanks!

if you’re using a Global Options checkbox preference, just beware that Vassal seems to ignore the default setting, leaving the Global Property as null, which will result in an expression error if that GP is treated as a boolean (i.e. true or false). Once a user actually selects or deselects the option, all is well. A workaround is to set the default via a Startup GKC or whenever you reference the GP.

So, for a default of true in your example expression, use :
{preference_name!=false ? 1 : 0}

For a default of false, use:
{preference_name==true ? 1 : 0}

As a module developer, you may not notice the error because you probably test the user preference checkbox early on by setting/resetting it.

Mark

Thanks for that informative tip !