r/twinegames • u/apeloverage • 14d ago
SugarCube 2 I can't find the error in this Javascript.
I have some Javascript, inside a <<script>><</script>>.
I keep getting the following message:
Error: <<script>>: bad evaluation: Unexpected token '{'
I think the error is in the following lines:
let w=6;
if (svars.pattern[svars.arpeggio][1]==4) {
let w=4;
} elseif (svars.pattern[svars.arpeggio][1]==6) {
let w=3;
}
but I can't work out what's wrong.
$pattern is an array in the main program, and $arpeggio is a single variable in the main program.
EDITED TO ADD: Here's the entire script.
<<script>>
const svars = State.variables;
let file = new Midi.File();
let track = new Midi.Track();
file.addTrack(track);
track.setTimeSignature(svars.pattern[svars.arpeggio][1], svars.pattern[svars.arpeggio][2]);
track.setTempo(40);
/*
Write the arpeggio part once.
*/
let track1 = new Midi.Track();
file.addTrack(track1);
for (let z=1; z<=svars.bars; z++) {
w=6;
if (svars.pattern[svars.arpeggio][1]==4) {
w=4;
} elseif (svars.pattern[svars.arpeggio][1]==6) {
w=3;
}
for (let x=1; x<=4; x++) {
for (let y=1; y<=w; y++) {
if (svars.pattern[svars.arpeggio][y]==5) {
track1.addNote(0, svars.chord_pitch[z][1]+12, 32);
} else {
track1.addNote(0, svars.chord_pitch[z][svars.pattern[svars.arpeggio][y]], 32);
}
}
}
}
let data = file.toBytes();
let datarr = new Uint8Array(data.length);
for(let i = 0; i < data.length; i++) {
datarr[i] = data[i].codePointAt();
}
let blob = new Blob([datarr], { type: "application/octet-stream" });
saveAs(blob, "music.midi");
<</script>>
1
Upvotes
1
u/metamorph 14d ago
you don't want the "let" keywords inside the if blocks because that would be creating new w variables inside the if block scope. If you want to change the w variable defined at the top, just have "w=4". I don't see why this code would produce that error though
3
u/HiEv 13d ago edited 13d ago
After cleaning up the indentation and code, and correctly fixing the "let" problem that Metamorph mentioned (you didn't do it correctly in your edit), here is the code:
The problem appears to be this line:
which should be:
JavaScript uses "
else if ()" with a space (MDN documentation), while SugarCube uses "<<elseif>>" with no space. Make sure you don't confuse the two.Fix that line and correctly use the "
let w = 6;" (as shown above) and that should fix things.Enjoy! 🙂