Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

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

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.

Comments

  • I'll reply to this as soon as I get back from an errand.
  • I appreciate that, no rush.
  • Chapel wrote: »
    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. […]
    Shouldn't be as issue.

    Chapel wrote: »
    […] 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).
    That's not a SugarCube v2 API, and that's not how you silently execute a passage anyway.

    Chapel wrote: »
    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;
    
    That is not how the API works. The callback you assign to Config.saves.onSave is passed one parameter, the save object. You have to modify that save, changing story variables is pointless at that stage. That's why your PassageReady/PassageDone code failed, because $AudioLoad will always be 0 upon loading a save.


    I'd suggest something like the following. No save modifications, $AudioLoad story variable, or PassageReady/PassageDone special passage necessary.

    For your Story JavaScript:
    Config.saves.onLoad = function () {
    	postrender['restore-audio'] = function (_, taskName) {
    		delete postrender[taskName];
    		new Wikifier(null, Story.get('AudioLoad').text);
    	};
    };
    


    And the AudioLoad special passage:
    <<masteraudio stop>>
    <<switch $chapter>>
    <<case 1>>
    	<<audio "chapter1" loop play>>
    <<case 2>>
    	<<audio "chapter2" loop play>>
    <<case 3>>
    	etc.
    <</switch>>
    


    That should do everything you want.
  • Thanks, worked perfectly.
Sign In or Register to comment.