Attaching functions to window returns not defined (Sugarcube 2)

Twine Version: 2.3.14
Story Format: 2.34.1

I’ve had a project for a long time, put it on ice for about half a year, and recently tried to start working on it, but I’ve gotten the weirdest issue.

Several of my functions, that worked flawlessly in the past, now all of a sudden show as undefined, even though they are in Story Script.

Apologies! An error has occurred. You may be able to continue, but some parts may not work properly.
Error [tw-user-script-0]: (intermediate value)(…) is not a function.

Error [StoryInit]: <>: error within widget contents (Error: <>: bad evaluation: objCopy is not defined).

I get this issue if I reference the functions as is, or prefixing “window.”
A console.log(window) in the browser inspector didn’t show much. (FF 89.0.1 (64-bit))

Window file:///C:/Users/Nath/AppData/Local/Temp/48df6f22-6192-4f4e-a620-c693b0d59a9b.html
“$”: function S(e, t)​
EvEmitter: function e()​
FileSaver: Object { }
LZString: Object { _keyStr: “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=”, _f: fromCharCode(), compressToBase64: compressToBase64(e)
, … }
LockScreen: Object { lockWindow: lockWindow(htmltext)}
Scripts: Object { }
SugarCube: Object { Browser: {…}, Config: {…}, Dialog: {…}, … }
imagesLoaded: function o(e, t, r)​
jQuery: function S(e, t)​
returnExports: Window file:///C:/Users/Nath/AppData/Local/Temp/48df6f22-6192-4f4e-a620-c693b0d59a9b.html
saveAs: function g(b, g, h)​

Symbol(Symbol.toStringTag): “Window”
: WindowPrototype { … }

Note that LockScreen and Scripts are successfully getting attached to window

Pastebin Story Script: https://pastebin.com/eEqpxYkF

I want to re-iterate: These functions (objCopy, rerenderInv so far) worked in the past. They have not been altered. Their scope has not been altered, and yet they are now erroring out.

Why is your window.Scripts.requestScriptLoad() function definition setup as a Immediately Invoked Function Expression?

I ask because that’s what’s causing the error, and simply changing that function definition to not being setup that way solves your issue.

eg. Change the following…

(function () {
	window.Scripts.requestScriptLoad = function (options) {
		if (options == null || typeof options !== 'object' || !options.src){
			return;
		}

		var
			opts   = Object.assign({ parent : document.head }, options),
			script = document.createElement('script');

		function onLoadOnce(evt) {
			opts.onload.call(evt.target, evt);	
			script.removeEventListener('load', onLoadOnce);
		}

		script.id   = opts.id;
		script.src  = opts.src;
		script.type = 'text/javascript';

		if (typeof opts.onload === 'function') {
			script.addEventListener('load', onLoadOnce, false);
		}

		opts.parent.appendChild(script);
		
		addScript(opts.id, opts.site);
		console.log(`Added script-file ${opts.id} to global scripts`);
	};
})();

…to being just…

window.Scripts.requestScriptLoad = function (options) {
	if (options == null || typeof options !== 'object' || !options.src){
		return;
	}

	var
		opts   = Object.assign({ parent : document.head }, options),
		script = document.createElement('script');

	function onLoadOnce(evt) {
		opts.onload.call(evt.target, evt);	
		script.removeEventListener('load', onLoadOnce);
	}

	script.id   = opts.id;
	script.src  = opts.src;
	script.type = 'text/javascript';

	if (typeof opts.onload === 'function') {
		script.addEventListener('load', onLoadOnce, false);
	}

	opts.parent.appendChild(script);
	
	addScript(opts.id, opts.site);
	console.log(`Added script-file ${opts.id} to global scripts`);
};

…stops the error from occuring.

I’m… gonna go and sit in a corner now. Should’ve seen that.

requestScriptLoad was taken from one of my other projects, but was altered to be fired from a passage, and used for more than 1 script, not instantly load a single predefined one.

Rubber duck method powering through code blindness, I suppose.