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.
  • Hi again TheMadExile, i was thinking to myself...

    Can i create a "cheat" menu using your code, something like:

    Setting.addToggle("STREnabled", {
    label : "Add +20 Strenght?",
    default : false,
    onInit : add +20 to variable STR,
    });

    Then i would repeat the above for every other game variable needed.
    Not sure if i can use Javascript to mess directly with Twine's variables...

    Thanks in advance for your help.
  • I wouldn't advise it. Permanent modifications, which is what I assume you want, aren't really a good fit for the Setting API.

    You could/should simply create a passage to do so via <<link>> macros. For example, a passage named Cheats:
    !Cheats
    <<link "+20 Strength">><<set $str += 20>><</link>>
    <<link "+20 Intelligence">><<set $int += 20>><</link>>
    


    How you display that passage to the player is up to you. You could simply provide a link to it or you could display it via a dialog, like so:
    <<link "Cheats">>
    	<<script>>
    	Dialog.setup('Cheats');
    	Dialog.wiki(Story.get('Cheats').processText().trim());
    	Dialog.open();
    	<</script>>
    <</link>>
    
  • Thank you :) Worked like a charm.

    One more thing, if i wanted to "teleport" to a specific passage from inside the Cheats popup/passage would i still use the same code? Example below?

    <<link "Go to End Passage">>[[End Passage]]<</link>>

    I am asking because i tried, saw the link passage on twine from the Cheats Passage to the End Passage, but when clicking nothing happened. I assume the coding must be slightly different.
  • Please use the code tag—it's C on the editor bar—when posting code or markup.


    You don't need anything more than a wiki link (double square bracketed link) to do that. For example:
    [[Go to End Passage|End Passage]]
    


    If you wanted to perform other actions, as well as forwarding the player, then you could use the <<link>> macro like so:
    → Separate argument version
    <<link "Go to End Passage" "End Passage">>…other code here…<</link>>
    
    → Wiki link argument version
    <<link [[Go to End Passage|End Passage]]>>…other code here…<</link>>
    
  • edited April 21
    Many thanks once again. Worked perfectly. Cheat Menu is now working ;) Also as a side effect, helps with my test plays debug.
Sign In or Register to comment.