Here are three variants, that work for different cases.
Let's say you have an attribute named "hp", which tracks hitpoints.
And you have a checkbox input named "wounded", which gets checked when you have wounds.
Important: the checkbox needs value="1".
You might try this very simple sheet worker:
on('change:hp' function(){
setAttrs({wounded:1});
});
Whenever hit point changes, the wounded checkbox will check. This will work for some cases, but not for hit points. The problem is that restoring HP to max is a change, and will cause the wounded checkbox to be checked.
Here's an alternative, using eventInfo.
on('change:hp' function(eventInfo){
let newScore = +eventInfo.newValue||0;
let oldScore = +eventInfo.previousValue||0;
if(newScore < oldScore) setAttrs({wounded:1});
});
whenever hp changes, this will grab the new HP value, and compare it to the previous value - the value it had before this change happened.
If HP is dropping, the wounded box will be marked. If its rising, it wont.
This will work for some situations, but you might want it to be automatically unmarked when HP restore to full.
For that you need to set a hp_max attribute, which doesnt automatically exist. You define that in the same way as hp, and once defined will be available on a token bar. You can then use
on('change:hp change:hp_max' function(eventInfo){
getAttrs(['hp','hp_max'], function(values) {
let current = +values.hp||0;
let max = +values.hp_max||0;
let wounded = (current < max) ? 1: 0;
setAttrs({
wounded: wounded
});
});
});
This will check the wounded checkbox if hp are less than max, and remove the checkbox if hp equal or exceeds hp_max.
You could also modify it so that hp cannot exceed max. That's left as an exercise for the reader :)
This:
let wounded = (current < max) ? 1: 0;
Is the same as:
let wounded = 0;
if (current < max) wounded = 1;