Twine Sugarcube - how to prevent audio cache error if user reloads browser

Twine Version: 2
Story Format: 2.34.1

Hi, thanks for all the help in the past!

So my first passage has:

<<cacheaudio “ping” “” “media/audio/ping.ogg”>>\

And most of the passages start with:

<<audio “ping” play>>

However if the user reloads their browser at any point, they will get the following error on every passage (and no sound).

“Error: (doublebrackets)audio(doublebrackets): error during runner initialization: track “ping” does not exist”

I’m assuming that caching audio on every passage is going to cause data download repeatedly which doesn’t seem reasonable (no idea - does it?), and possibly will slow the game down (does it?).

So I tried adding the following to the top of each passage as a fix, but it doesn’t work.

if (!(SimpleAudio.tracks.has(“ping”))) {
<<cacheaudio “ping” “” “media/audio/ping.ogg”>>

I’m not sure this code can even go in a passage because twine displays it, so it’s clearly not implemented right. Maybe code for the API doesn’t go in the passages - no idea. The motoslave documentation doesn’t seem to say what you actually do with those sort of snippets.

In future I’ll load the audio from C/username/somewhere, rather than wikimedia, so I guess I could re-cache the audio at the start of every single passage? Seems like overkill though.

Any advice appreciated!

When you say “your first passage”, do you mean the passage where your game starts? Because as the documentation for <<cacheaudio>> says, the best place to set up tracks is a passage named StoryInit. That exists specifically to work around this problem.

Thanks for this. I stupidly believed that StoryInit was something like the stylesheet and javascript pages, and always wondered why my version of Twine doesn’t have a ‘StoryInit’. You’ve helped me realise that it’s something the user has to create for themselves!

1 Like