Howdy, Stranger!

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

Audio - Turn off/on option in [Sugarcube 2]? Where to upload?

Right now I'm using the audio macro. Can I give the reader/player an option to mute all audio? If there is not, can I make an option in the UI bar (below saves and restart) that basically turns a variable off (and also stops all audio) or on so that I can check before playing audio if sound is turned on? I've tried some #ui-bar and Setting.addToggle stuff but I don't know how to make it work.

I have tried the Youtube Background Music thing then I found out it's for Twine 1. I guess I'll have to put the audio somewhere else. Most free webhosters seem to have something against hosting music. Obviously some Twine stories do have audio. So where can I upload music for free so that my story can play it?

Best Answer

  • The <<audio>> macro documentation lists all of the possible actions, of which one is mute. In this case, however, it sounds more like you want to give the player the option to disable audio, rather than simply mute it.

    The latter can be accomplished by adding a setting to the Settings menu (via the Setting API), which you'd do by placing something like the following in your Story JavaScript:
    var audioEnabledHandler = function () {
    	if (!settings.audioEnabled) { // is false
    		/*
    			Stops all audio currently playing, via `<<audio>>` or `<<playlist>>`,
    			when the setting is disabled.
    		*/
    		new Wikifier(null, "<<stopallaudio>><<playlist stop>>");
    	}
    };
    Setting.addToggle("audioEnabled", {
    	label    : "Enable audio within the story?",
    	default  : true,
    	onInit   : audioEnabledHandler,
    	onChange : audioEnabledHandler
    });
    
    And then checking the setting whenever you wish to play audio. For example:
    /* To play a single track. */
    <<if settings.audioEnabled>><<audio "someTrack" play>><</if>>
    
    /* To play the playlist. */
    <<if settings.audioEnabled>><<playlist play>><</if>>
    

Answers

  • Thank you very much!
  • Hey TheMadExile, super good answer. I just copied/past it to my game :) Worked like a charm, after i had spent 2 hours on my own, struggling with variables and what-not... and trying to understand why the "back" button would undo a remember variable command :P Again great stuff
  • Buja wrote: »
    . So where can I upload music for free so that my story can play it?

    Github pages allows you to create a repository that can include pretty much anything, including audio and images, and host everything using relative paths.

    I can't confirm, but I've also heard that itch.io allows you to upload and access external resources as well.

  • Magma wrote: »
    Hey TheMadExile, super good answer. I just copied/past it to my game :) Worked like a charm, […]
    The answer is well over a year old. While it does still work, I'd write it differently today—post-v2.8.0, where the <<masteraudio>> macro and <<createplaylist>> macro exist.

    Story JavaScript:
    var audioEnabledHandler = function () {
    	if (!settings.audioEnabled) { // is false
    		/*
    			Stops all audio currently playing, via <<audio>> or <<playlist>>,
    			when the setting is disabled.
    		*/
    		new Wikifier(null, "<<masteraudio stop>>");
    	}
    };
    Setting.addToggle("audioEnabled", {
    	label    : "Enable audio within the story?",
    	default  : true,
    	onInit   : audioEnabledHandler,
    	onChange : audioEnabledHandler
    });
    
    And checking the setting wherever you wish to play audio. For example:
    /* To play a single track. */
    <<if settings.audioEnabled>><<audio "someTrack" play>><</if>>
    
    /* To play a playlist. */
    <<if settings.audioEnabled>><<playlist "somePlaylist" play>><</if>>
    


    Magma wrote: »
    […] and trying to understand why the "back" button would undo a remember variable command :P
    Because as the documentation for the <<back>> macro notes, it undoes history.

    Also, it doesn't, not fully. Depending on how you use it the <<remember>> macro really consists of two components: the optional ability to set variables and storing the current value of the given variables so they're automatically reinitialized at story startup. Undoing the moment where the <<remember>> was invoked only reverts the former, not the latter.

    Unless you restarted the story while you were attempting to use <<remember>>, you probably never noticed that the storage for automatic reinitialization part was, in fact, working.

    That said, as the documentation for the <<remember>> macro notes, you rarely need, or want, to use <<remember>>, as it is only useful in very specific circumstances and problematic in most others. Unless you know that you need to use it, you very likely do not.
Sign In or Register to comment.