"Track does not exist" error

I’m having problems with playing audio in Twine, specifically version 2.3.16 using Sugarcube 2.36.1. I’ve been looking for a solution for over an hour but nothing so far seems to fix my issue. Whenever I get to the passage where the audio is supposed to play, it gives me: “error during runner initialization: track “club_bgm” does not exist”

The code in my StoryInit passage looks like this:

<<set _bgm = setup.SoundPath + "outside_club.mp3">>
<<cacheaudio "club_bgm" _bgm>>

JavaScript looks like this:

if (document.location.href.toLowerCase().includes("/temp/") || document.location.href.toLowerCase().includes("/private/") || hasOwnProperty.call(window, "storyFormat")) {
	setup.Path = "C:\Users\Bára\Documents\Twine\Stories";
} else { 
	setup.Path = "";
setup.SoundPath = setup.Path + "sounds/";

And finally, inside the actual passage I’ve got this:

<<audio "club_bgm" play loop>>

The audio file itself should be in the right place, I’ve checked more times than I’d care to admit.
And I apologize if the solution is really simple, I’m pretty new at this.

You don’t need javascript for the path, just follow relative paths according to the HTML standard.

In your StoryInit:

<<cacheaudio "club_bgm" "./sounds/outside_club.mp3">>

Then play it in a passage with this.

<<audio "club_bgm" play>>

When you do it like this you will need to make sure your game file, that is - the html file needs to be exported next to your sounds folder. For example.

1 Like

Also, it looks like you want to use a variable instead of the track name.

I’m guessing that you are planning to change the background music based on events rather than passages.

In this case use $bgm rather than _bgm as the latter is a temporary variable.

In the StoryInit passage:

<<cacheaudio "club_bgm" "./sounds/outside_club.mp3">>

In the passage or code block where you want to set the track:

<<set $bgm to "club_bgm">>

In the passage where you want to play the music:

<<audio $bgm play>>

Thank you for the response, however I’m afraid that it’s now giving me a different error. Instead of “track “club_bgm” does not exist”, it says “track “club” does not exist.”

Oh, I forgot that I changed that. I’ve updated the answers to match what you used. Instances of “club” should be “club_bgm”. Or rather they should be consistent whichever you choose.

Unfortunately, it’s now saying that “track “undefined” does not exist.”
I’ve copied everything over, not sure what I’m doing wrong.

Are you using the $bgm variable? Make sure <<set…>> comes before <<play…>> if they are both in the same passage.

Or just put <<set…>> after <<cacheaudio…>> in the StoryInit passage.

You’re using the wrong slashes and not including a final slash within your local path. Slashes in internet network paths should always be forward slashes, regardless of what your filesystem uses.

For example, assuming that your local path includes a sounds directory within it, it should look like the following:

setup.Path = "C:/Users/Bára/Documents/Twine/Stories/";

NOTE: It is possible to use backslashes. While incorrect, browsers do allow them for the sake of Windows compatibility. That said, complicating this is the fact that the backslash is the escape character in strings, so you have to escape the escape character to get a literal backslash—i.e., you need to double them; e.g., instead of \ you’d use \\. The best thing though is simply to use forward slashes when on the web.

1 Like

I’ve actually done that before and it didn’t do anything, which hasn’t changed…

I’ve tried both, neither worked.

Here is a working example.

Make sure to extract the zip file, then open index.html in your browser and click “Play” to hear the music play.

You can also import index.html into Twine 2 in order to edit it. However, in order to make the music play you will have to export the game to the same folder on your hard disk again.

audiogame.zip (897.7 KB)

My code is exactly the same, but it’s still giving me the same “undefined” error. Could something else be messing with it, maybe?
Either way I tried capitalizing the “s” in the StoryInit passage because I previously had it as “storyInit” for whatever reason, and when I launch it, it’s now giving me a popup: “Error [StoryInit]: set: bad evaluation: Invalid left-hand side in assignment” along with the previous error. Not sure if it’s related anyway, but I thought I’d mention it.

I tried capitalizing the “s” in the StoryInit passage because I previously had it as “storyInit” for whatever reason

Yes, the letter case matters.

Anything else is probably due to minor typos or syntax errors.

Can you troubleshoot by creating a completely new small test game that just plays music in one or several passages the way you want it to work? If you can get it to work in isolation, that might give insight whether the problem is code or something weird externally - like you’re trying to play music from a folder that’s cloud-saved but archived and not actually downloaded in the directory the story references. That happened to me once.

It does work completely fine in a new game.

1 Like

Ah, okay. What should I do about the StoryInit error pop up it’s now giving me, though?

Can you export your game and share it (either with the Publish or Proof button under the build tab)?

If you don’t want to post it publicly here you can send it to me privately at pbparjeter@protonmail.com and I can take a closer look.

1 Like

Right, I’ll send it to you, then.

It seems to be unrelated to the audio…the dash in $touch-averse is responsible.

So change all of instances of that to $touchaverse or $touch_averse

This section of the documentation will tell you what is or isn’t allowed as variable names. The explanation is not very clear IMO but the examples are helpful.

1 Like

God, yeah, completely forgot about the underscores… thank you!

1 Like