Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Variable Displays as 0 in roll Button Text

Hi all, One last question (I think) for my custom sheet. I have a variable that I call at a couple different places and that seems to have the intended value everywhere, except in one particular message. Not only does it not work, I'm quite convinced that it absolutely used to work just a few days ago, and I have no idea what I could possibly have changed that would affect it since then. It's a simple max hp variable. Here is what my HP display looks like: Pretty straightforward. The -/+ buttons obviously just increase or decrease the current value. When I use the + button's sheetworker contains a condition that uses the max value to cap the value. My problem is with this show button. Players in my team often display their HPs after they were modified both for transparency and so that other players know of their conditions (not really needed anymore now that I've manage to have it display properly on the health bar, but whatever). The values you see displayed in that table are those: < input type = "hidden" name = "attr_hp_bar" value = "0" /> < input type = "hidden" name = "attr_hp_bar_max" value = "0" /> < input type = "number" name = "attr_Current_HP_Display" value = "@{hp_bar}" disabled /> < input type = "number" name = "attr_Max_HP_HEALTH_Display" value = "@{Current_HP_Max}" disabled /> Here is the button's code: < button type = "roll" name = "roll_HPDisplay" value = "@{template} {{talent=Health}} {{talentdescription=Health: @{hp_bar} / @{hp_bar_max}}}" > < span data-i18n = "Show" > Show </ span > </ button > Here is the code for the sheetworker of the + button. // Heal HP     on("clicked:heal_hp", function() {         getAttrs(             [                 "hp_bar",                 "hp_bar_max"             ], function(v) {             let present_hp = parseFloat(v["hp_bar"]);             let max_hp = parseFloat(v["hp_bar_max"]);             let u = {};             if (present_hp < max_hp) {                 present_hp = present_hp + 1;             } else {                 present_hp = max_hp;             }             u["hp_bar"] = present_hp;             setAttrs(u);         });     }); And here is the sheetworker I use to update the hp_bar_max value whenever the sheet is opened (I had to use that because I spent forever trying other ways of accessing the value of Current_HP_Max, and I think the fact that it is an auto-calc value makes it somehow unusable. Whenever I tried using Current_HP_Max, it simply wasn't being treated as a numerical value. So instead the workaround I found was to use separate hidden hp_bar and hp_bar_max attributes of which the value is not auto-calc but sheetworkers-calculated. Because hp_bar_max doesn't update often, I just created a worker that triggers when the sheet opens. I'll explain what that is relevant in a minute. Here's the code of that sheetworker: on("sheet:opened", function() {         getAttrs([         "Initial_HP_Max",         "Talent_HP_Max",         "Advances_HP_Max",         "Modifier_HP_Max"         ], function(v) {             let max_hp = Math.floor(                 parseFloat(v.Initial_HP_Max) + parseFloat(v.Talent_HP_Max) + parseFloat(v.Advances_HP_Max) + parseFloat(v.Modifier_HP_Max)             )             let u = {};             u["hp_bar_max"] = max_hp             setAttrs(u)         });     }); Now, I say the hp_bar_max value works everywhere. Not entirely true. If in this line < input type = "number" name = "attr_Max_HP_HEALTH_Display" value = "@{Current_HP_Max}" disabled /> I use hp_bar_max instead of Current_HP_Max, I get a 0 displayed in the table too. However, I know hp_bar_max's value is not zero. I know this because 1- If I create a token from the sheet and display the hp bar with the hp_bar / hp_bar_max variables, the max value displayed is correct. 2- If I use the + button, current HPs value will indeed stop at the expected value for hp_bar_max And I know that the sheetworker responsible for updating hp_bar_max works because if I change my character's HP stats in the main stats table (the values used to calculate Current_HP_Max and hp_bar_max), close and then reopen the sheet, and then use the + buttons, current HPs will now be capped at the new correct value for hp_bar_max. Yet if I try to display hp_bar_max, either in the table (in lieu of Current_HP_Max) or if I hit the show button, what I get is 0. What am I missing?
1673643091
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
I believe the issue you are running into is that to reference the max version of an attribute in a macro/roll button, you actually want @{hp_bar|max} . However, note that to call the max value in a macro, you also need to specify the character's name in the attribute call like @{my character|hp_bar|max} . There are some other things: If you are using sheetworkers, I would highly  recommend doing away with all disabled fields on your sheet. The two systems are not compatible without a lot of legwork You have capitalized attribute names. I would recommend switching these to pure snake_case, which is all lower case. There are a variety of reasons for this which I've gone into in depth in other posts. Why do you have two sets of hp values? I'm guessing that this might be because you were trying to use autocalcs and sheetworkers, but see the first point in this list.
1673643447

Edited 1673643668
GiGs
Pro
Sheet Author
API Scripter
First, disabled attributes (and thus autocalcs) are incompatible with sheet workers. The worker only sees the text value of the autocalc, not what it resolves to. So it's best to rplace those with values calculated by sheet workers. There are other places autocalcs don't work (like token bars), so it's bestt o avoid them for calculating max hp anyway. In this section you seem to have some unnecessary duplication: < input type = "hidden" name = "attr_hp_bar" value = "0" /> < input type = "hidden" name = "attr_hp_bar_max" value = "0" /> < input type = "number" name = "attr_Current_HP_Display" value = "@{hp_bar}" disabled /> < input type = "number" name = "attr_Max_HP_HEALTH_Display" value = "@{Current_HP_Max}" disabled /> Why isn't this simply < input type = "number" name = "attr_hp_bar" value = "0" /> < input type = "number" name = "attr_hp_bar_max" value = "0" /> In your sheet worker you have let present_hp = parseFloat(v["hp_bar"]); let max_hp = parseFloat(v["hp_bar_max"]); Is there are reason they are floats? You probably only need integers here, since those values are only expected to be whole numbers. You also need to account for "" results, so it would be better to have let present_hp = parseInt(v["hp_bar"]) || 0; let max_hp = parseInt(v["hp_bar_max"]) || 0; This applies on other places you have used parseFloat too. If you fix the disabled entry and calculate hp_max with sheet workers, I think your problem will go away - and you won't need to use sheet:opened for that worker. Edit , also, ditto everything Scott said. Nice spot on HP|max too. To be clear (as mud), you use _max in sheet workers and attribute names, but |max in rolls.
Thanks to you both, using hp_bar|max fixed it. Then I can do away with the Current_HP_Max variable. GiGs had advised me to use readonly attributes where I want fields that the user cannot change directly, instead of disabled fields. But readonly fields would simply not show up at all for some reason. But I took note to not try to read from disabled fields for any other calculations. So I've been using hidden variables to do my computations and disabled fields to display hidden values. There's probably something I'm not getting right because I can't figure out how to display anything without having to create a new attribute. Meaning I run into this weird situation where I might want to have the same value appear in two different locations for convenience (as in this example... My character's HP, as in, the characteristic which is subject to advancements and bonuses and so on, appears in the global characteristics table. But I also want it to appear in the box where current status is displayed), and in these cases I end up having to declare a new attribute altogether, and now the naming becomes a hassle, unless I'd start having cues in my attribute names as to their location... This gymnastic is why you see different names for what seems like it should all be a single variable. And you're right for the styles, that's because I'm making modifications from an existing sheet. In the best of worlds I would use only snake case, but I don't want to start changing all existing variable names (Because keeping the same names makes for a very easy transition between the sheet we're currently using and this one).
1673726136
GiGs
Pro
Sheet Author
API Scripter
Mathieu L. said: Thanks to you both, using hp_bar|max fixed it. Then I can do away with the Current_HP_Max variable. GiGs had advised me to use readonly attributes where I want fields that the user cannot change directly, instead of disabled fields. But readonly fields would simply not show up at all for some reason. That's weird. Was that happening while still using autocalcs? You can create the same attribute (with the same name) in multiple places in your code. It's hard to be sure what exactly is going on, because what you're describing should not be happening. have you created an entirely new character sheet and tried working with it? Sometimes a character you have worked on a lot can get corrupted and changes become erratic.