I would like to create a skill setup for my Twine book. When a variable increases, I want the corresponding CSS code to change accordingly. For example, in this code below.
I want it so that when my “html” variable increases by 5 points, the HTML skill bar fills up by 5 points. I’m not sure how to accomplish this with just if, elseif, and else statements. If I were to use these statements, I would need at least 20 different commands, right? Can you please help me with this?
I would assign the progress bar element an id attribute, so you can get it in a script via let e = document.querySelector("#id");, then you can set the width via e.style.width = $html + "%";.
Except you can’t just do that because of the way SugarCube’s rendering works: it doesn’t exist on the page while it’s in the process of rendering the passage.
You might be better off looking at Chapel’s Meter macros
You can see how the width of the bars changes with the variables now. The <<done>> makes it so that the script only executes after the rest of the passages is loaded iirc. That should take care of the SugarCube rendering thing. At least, it seemed to work when I tested it.
Ah, using jQuery. I know it’s included in SugarCube, but I’m more familiar with standard web APIs. And you’re right, the <<done>> macro is a good solution to make sure the page is rendered already.
I’ll be honest, I usually avoid jQuery and I only used it in my earlier code because the original link used it! Half the time I barely remember Sugarcube comes with it. You could easily do the same thing with vanilla JS:
It depends on personal preference, I guess. I slightly like vanilla JS more because it familiarizes you with functions you don’t need to install jQuery to use, but there’s nothing wrong with either.