Tweego (sugarcube 2) function return undefined

I recently made a post asking about the HiEv status bar function, he updated the code and it worked on Twine 2, but a problem is happening when I try to run this on tweego.

window.statusBar = function(CurST, MaxST, BarID) {
	var ST = parseInt((CurST / MaxST) * 100).clamp(0, 100);
	$("#" + BarID).css( { width : ST + "%" } ).attr("title", CurST + "/" + MaxST + " EN");
	$("#" + BarID + "bkg").attr("title", CurST + "/" + MaxST + " ST");
};

This is in a JS file, belonging to one of the files in the game folder. But it returns as undefined when the HTML run.

TypeError: {(intermediate value)(intermediate value)(intermediate value)} is not a function

Other snippets of code in the same files have no problems running though.

Twine Version: Tweego
Story Format: Sugarcube 2

That isn’t enough information to solve that problem.

We’d need to see the line of code that’s triggering that error and know where it’s being called from before we could figure that out.

It is not called at any time, as soon as it is declared the error happens. And the only code snippet that causes this is this function, so I ran out of ideas.

That doesn’t make any sense.

Do you have a simple example which demonstrates the problem?

Apparently, when the HTML file is executed, the error occurs…

That picture doesn’t tell me anything new.

Again, could you please post a simple example of the code which demonstrates the problem?

OK, you sent the source, which showed the problem. You left out a semicolon “;” at the end of one part, and then followed that up with a second part that began with a left parentheses “(”, and so JavaScript interpreted that second part as an attempt to call a function on the previous part. Adding in a semicolon fixed the problem.

It didn’t have anything to do with my “health bar” code.

Here's the corrected JavaScript: (click the arrow on the left)
/* twine-user-script #1: "Characters.js" */
State.variables.characters = {
	player: {
		firstName: '',
		surname: '',
		health: {
			curHP: 59,
			maxHP: 60
		},
		energy: {
			curEN: 100,
			maxEN: 100
		},
		lust: {
			curLT: 20,
			maxLT: 100
		},
		experience: {
			curEX: 30,
			maxEX: 100
		},
		inventory: []
	},
	isabella: {
		firstName: "Isabella",
		surname: ''
	},
	miles: {
		firstName: "Miles",
		surname: ''
	}
};
/* twine-user-script #2: "init_js.js" */
/* JS Game Init */

(function () {
	const $actBar = jQuery(document.createDocumentFragment()).append('<div id="action_menu"></div>');

	jQuery("#story").prepend($actBar);

	$(document).on(':passagedisplay', function() {
		setPageElement('action_menu', 'ActionMenu');
	});
})();

importStyles("gameStyles.css");

setup.Clock = function(d) {
	let minutes = d.getMinutes();
	let hours = d.getHours();
	//let years = d.getYear();

	const weekDays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];

	var day = weekDays[d.getDay()];

	if (d.getHours() < 10) { hours = "0" + hours; }
	if (d.getMinutes() < 10) { minutes = "0" + minutes; }

	State.variables.day = day;
	State.variables.hours = hours;
	State.variables.minutes = minutes;

	let printDate = "$day <br> $hours : $minutes";

	return printDate;
};

/*
Macro.add("Menu_Options", {
	tags : null,
	handler : function() {
		if (this.args[0] === 'general') {
			try {
				for (let i = 0; i < 15; i++) {
					
				}
			} catch (err) {

			}
		}
	}
});
*/

/* Status bar JS */

Config.ui.updateStoryElements = false;

$(document).on(":passageend", function() {
	$("#charname").empty().wiki("$mcName $mcSurname<hr>");
	statusBar(State.variables.characters.player.health.curHP, State.variables.characters.player.health.maxHP, "statusBase1");
	statusBar(State.variables.characters.player.energy.curEN, State.variables.characters.player.energy.maxEN, "statusBase2");
	statusBar(State.variables.characters.player.lust.curLT, State.variables.characters.player.lust.maxLT, "statusBase3");
	statusBar(State.variables.characters.player.experience.curEX, State.variables.characters.player.experience.maxEX, "statusBase4");
});

window.statusBar = function(CurST, MaxST, BarID) {
	var ST = parseInt((CurST / MaxST) * 100);
	if (ST < 0) { ST = 0; }
	else if (ST > 100) { ST = 100; }

	let BarElement = $(document).find("#" + BarID);
	BarElement.css( { width : ST + "%" } );
	BarElement.attr("title", CurST + "/" + MaxST + " EN");

	$(document).find("." + BarID + "Base").attr("title", CurST + "/" + MaxST + " ST");
};
/* twine-user-script #3: "map_sys.js" */
setup.Maps = {
	intro_map : {
		'0x2'  : { icon: "hallway", desc: "Road" },
		'0x1'  : { icon: "hallway", desc: "Road" },
		'0x0'  : { icon: "bedroom", desc: "Carriage" },
		'0x-1' : { icon: "hallway", desc: "Road" },
		'0x-2' : { icon: "hallway", desc: "Road" },
		initial_pos : "0x0",
		pos		 : [0, 0],
		get_actPos : function() {
			return this.pos[0] + "x" + this.pos[1];
		}
	}
};

There are a few other minor fixes in there, and I commented out the part where you define the “years” variable, since you never use that variable.

You can see now why I couldn’t solve the problem without actually seeing your code.

Have fun! :grinning:

1 Like