0 votes
by (1.4k points)
Hello all!

SugarCube 2.21 of course: how do I make something which happens when a saved game loads but at no other time?

I tried setting a variable in StoryInit, but that is before the save is loaded, and when it loads it just puts the variable back to what it was before.

What I am trying to do is get the background sound to start when a saved game loads, but not have it restarting and restarting wherever you go.

Any ideas?

Thank you!

1 Answer

0 votes
by (68.6k points)
selected by
 
Best answer

You probably want to use the Config.saves.onLoad callback to invoke the <<audio>> macro via jQuery.wiki().  For example, something like the following:

Config.saves.onLoad = function () {
	$.wiki('<<audio "saveLoadSfx" volume 1 play>>');
};

 

by (1.4k points)
edited by
Works like a charm! (actually charms don't work, but this works perfectly.) Thank you for your help!

Another problem I am seeing now, because the audio is now playing, is that the Autosave does not save variable changes until you leave the passage, which is playing the wrong audio in places. Is there a way to change that? Should I make a new question?

Is there a way to set variables with this?

Would  $('#passage').wiki('somemacro'); work, or something along such lines?
by (68.6k points)

> Another problem I am seeing now, because the audio is now playing, is that the Autosave does not save variable changes until you leave the passage, which is playing the wrong audio in places. Is there a way to change that?

Changes to variables are not finalized until the active moment has passed into the history.  If you're attempting to play different audio upon loading via a variable, then that's going to be an issue.

Assuming you're simply occasionally changing which loading track is played, then a workaround would be to make the change one passage ahead of where you want it to begin playing.

 

> Would  $('#passage').wiki('somemacro'); work, or something along such lines?

No.

by (44.7k points)

Actually, there's a similar trick using Config.saves.onSave that you can use to make the "Save" button save the current values of variables, so you could use that for the variable that keeps track of your music.  If your variable for your music is $CurrentMusic then you code would look something like this:

Config.saves.onSave = function (save) {
	save.state.history[save.state.index].variables.CurrentMusic = State.variables.CurrentMusic;
};

Just be careful about using that trick for variables which have their current value modified by the passage, because when you load the passage they'll get modified again (e.g. if you save $X that way, and the passage does <<set $X = $X + 5>>, then $X will end up being 5 more than it should be when you load that save).  Variables that are only set to a value (e.g. <<set $X = 5>>) should be fine though, as long as there's no code that uses them prior to their being set.

by (63.1k points)
edited by

I feel like it'd be way easier to just do this: 

Config.saves.onLoad = function () {
    postdisplay["restart-audio"] = function (t) {
        delete postdisplay[t];
        $.wiki('<<audio $track volume 1 play>>');
    };
};

Then if you set the $track variable whenever you change songs, the right song will play, even if it's set in the save passage. 

by (1.4k points)

Basically I have different background sounds for different places, with a variable associated with each sound (though I think I could actually do with one variable, which would be simpler).

At a passage where you may come from a different place, I have it so that if that variable is 1 then it fades the sound out and sets the variable to 0.

At first when I would load a saved game, no sound would play until you went to a different passage where it was supposed to switch sounds. But when I added just this to the Javascript, the sound would play:

Config.saves.onLoad = function (save) {
	$.wiki('');
};

But then the problem was, because it did not save the variable change to 0, the one background sound would start at full volume and then fade out, which of course sounds very cheap.

So I used HiEv's suggestion to save the variable changes, but then the sound was silent again, because I have them start playing at volume 0 in StoryInit instead of trying to get them to start looping at all the first passages you might encounter them in. So I used this:

Config.saves.onLoad = function (save) {
	$.wiki('<<goto "load">>');
};

And in "load" I put:

<<set $wa to previous()>><<goto $wa>>

And then I could put any macros I wanted into the "load" passage and they would run only when the game was loaded, so I put in <<if>> macros saying to fade in a background sound if its variable was 1.

That may have been an absurdly roundabout workaround, but it works for what it's worth!

Chapel's answer does look easier, and I will certainly try it first in my future adventures.

...