Quantcast
Channel: Twine Forum
Viewing all articles
Browse latest Browse all 3830

Get audio to play conditionally upon loading a game (Sugarcube 2).

$
0
0
Man I'm pulling my hair out here.

So different parts of my story have different looped tracks. I need those tracks to play when you load in without restarting on every passage. I tried a few things. I tried adding
state.display("AudioLoad");

to the onLoad config object, and that didn't really work, since it's pulling the chapter variable from StoryInit instead of from within the save, since the AudioLoad passage is technically displaying before the variables are loaded in. It loads the first song correctly, so the code does technically work, it's just not giving me access to the variables I need (which made sense once I thought about it).

No problem I thought. Let's just package a variable change in when the game saves. In StoryInit I wrote
<<set $AudioLoad to 0>>

then added the following to the onSave config object:
State.variables.AudioLoad = 1;

And then added a PassageDone (also tried PassageReady) passage with
<<if $AudioLoad is 1>>
      <<masteraudio stop>>
      <<if $chapter is 1>>
          <<audio "chapter1" loop play>>
      <<endif>>
      <<if $chapter is 2>>
      etc.
      etc.
      etc.

       <<set $AudioLoad to 0>>
<<endif>>

This, for some reason, has no effect at all--the main menu music just keeps looping. Additionally, one of them (PassageReady or PassageDone) didn't work on loading and caused the music to restart on each and every passage before any loading or saving even happens. Not sure why.

I tried using variables to store the audio as well
<<audio $current loop play>>

and that also didn't work, so using that for conditions is out.

I know there has to be some easy way to do this I'm missing, and if at all possible I'd prefer to avoid using PassageReady/PassageDone since my individual passages are already bloated enough with CSS elements and images.

Any help is greatly appreciated.

Viewing all articles
Browse latest Browse all 3830

Trending Articles