Elusive bug with importScipt

Hi there. And here I go again )

Bug I bumped to is strange. I made a bone-stripped example code to demonstrate it.

we have game of just two passages

Start

<<script>>

importScripts(
	["js/Person.js",	
	]);

<</script>>

[[Click me!]]

and Click me!

<<script>>
	State.variables.player = new Person();	
<</script>>

$player.name

and also a JS file in ‘js’ directory right next to game.html

window.Person = class Person {

    constructor(name = 'If you see this - it works!') {

        this.name = name;

    }

}

It works just fine. I making zip archive and transfer it to another PC and unpacked game works fine there too.

BUT

As soon as I give this exact archive (js directory included, it works) to my friends - game is bugged for them. As if js scripts are missing:

screenshot

What can be an issue here?

TYIA

For one of my fiends it works, for two other - don’t

I think that the problem can be the use of “class” in javascript, as it is the “new” way to create classes. The old way of working with Object Oriented Programing was just using “function” and “prototype”.

Check the browser and javascript version of your friend.

1 Like

Ok. I;ll chek it. But I doubd that they have such an old version of chrome…

Oh, thy Seth, our doubts are traitors! U.U

Let us know the result! : D

A few things of note:

  1. As noted in its documentation, importScripts() is asynchronous. Using it synchronously as you are, you’re opening yourself to a race condition, which is likely your main issue.
  2. You shouldn’t be doing initialization in your starting passage. For JavaScript code, that’s what the Story JavaScript section is for. For macros, that’s what the StoryInit special passage is for.

Try the following.

In your Story JavaScript section:

/* Import classes and retain the returned `Promise`. */
setup.classImports = importScripts([
	"js/Person.js",
]);

/* Lock the loading screen, until all imports are settled. */
let lockId = LoadScreen.lock();

/* Set then and catch callbacks on the import `Promise`. */
setup.classImports
	.then(() => {
		/* Unlock the loading screen. */
		LoadScreen.unlock(lockId);
	})
	.catch(err => {
		/* Unlock the loading screen. */
		LoadScreen.unlock(lockId);

		/* Report the error to the user. */
		let mesg = `Error: unable to load external scripts: ${err}`;
		UI.alert(mesg);
		console.error(mesg);
	});

In your starting passage:

[[Click me!]]

In your Click me! passage:

<<set $player = new Person()>>

$player.name
1 Like

I have done how you shuggested, and for me it works ok (as well as inial variant) byt my friend gets Error: unable to load external scripts: Error: importScripts failed to load the script “js/Person.js”.

We cheked it. The error is present on 102.0.5005.115 chrome

I would tell you to make some tests:

  • Try your failing friend’s version to clear cache: “CTRL + F5” on browser, or “F12” to open chrome console, go to the “Network” tab, and check the “Disable cache” checkbox, then reload the page again. Can seems silly but caching old files can be a stupid time consuming problem. Make this on every change they will test.

  • Make Person.js the simplest working file you can (if it can have for example only “var a = 2” instead of full class deffinition, the better. That will show us if the problem is in the file or somewhere else.

  • Check what’s your HTML file doctype. Different kinds of HTML can put the browser in different modes (strict, …) and make some syntax unavailable for these modes.

  • Is it absolutely necessary to make the “window.Person =” assignment in the class definition. Try without it.

Those are some ideas you can try to check what’s happening.

1 Like

Are they on Windows? If so, they may not have extracted the files. The Windows file explorer allows you to view the contents of supported archives without actually extracting the files, which would cause that error.

Ensure that they’re both extracting the files and not moving them apart—i.e., keep the js directory next to the HTML file.

2 Likes

That’s the case!
It works. Thank you!

Cool! Great TheMadExile!