Hi I’m quite new to Twine Sugarcube and I have hit a hurdle I get this error Cannot read property ‘querySelector’ of null, and I can’t see what is wrong with it.
There are a number of issues with your code examples:
1: Any JavaScript code found within a Twine 2.x project’s Story JavaScript area or within any script tagged passage is executed before the contents of the first Passage of a story is processed. This means that at the time the document.querySelector('.progress-bar') function call is made the <div class="progress-bar"> element won’t exist yet, which is why you’re seeing that error message.
You would need to delay the calling of the document.querySelector() function until after the target div has been added to either: the buffer used by the Passage Processing process; or to the page’s DOM. The need to delay the calling of the function leads to the 2nd issue.
2: Each JavaScript block, like that found within: the Story JavaScript area; a script tagged passage; a external JS file; are executed within their own private scope. This means that variable/classes/modules defined within one block are not visible to another block, unless you use some means to evaluate the scope of that thing to a ‘global’ like state.
There are a number of methods you can use to make something ‘global’ like:
a: define it on the special SugarCube setup object
setup.doit = function () {
console.log('doit called');
};