Help with plotscripting

Talk about things that are not making games here. But you should also make games!

Moderators: Bob the Hamster, marionline, SDHawk

Post Reply
AzureGames
Red Slime
Posts: 49
Joined: Thu Oct 29, 2015 8:18 pm

Help with plotscripting

Post by AzureGames »

How do I make my song "Fade out", or in other words, make it get quieter and quieter until you can no longer hear it?? :???:
User avatar
Gizmog
Metal King Slime
Posts: 2622
Joined: Tue Feb 19, 2008 5:41 am

Post by Gizmog »

SetMusicVolume command and a for loop. It'll look something, but not exactly, like this:

Code: Select all


GlobalVariable (1,MusicVolume)

plotscript,FadeMusicOut,begin
variable (Loop)
MusicVolume := GetMusicVolume
for (Loop,MusicVolume,0,-1)
do (
     SetMusicVolume (Loop)
     wait (1)
    )
end

plotscript,FadeMusicIn,begin
variable (Loop)
for (Loop,0,MusicVolume,1)
do (
     SetMusicVolume (Loop)
     wait (1)
     )
end
User avatar
Taco Bot
Meat, Cheese, and Silicon
Posts: 484
Joined: Fri Jul 18, 2014 12:15 am
Location: Santa Cruz
Contact:

Post by Taco Bot »

Also be aware that the music volume will be different depending on game settings and what backend of the OHR you're using. You'd probably be best saving the volume to a variable before fading out so you can return it to its previous level.
Sent from my iPhone
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Music volume shouldn't* depend on the backend ... with the possible exception of MIDI volume, which is quirky (and I can't remember the quirks anymore). But yes, because the player can adjust the volume you should fade to/from whatever the initial volume level is. Which is exactly what the scripts posted by Giz do.

However, music volume has a range of 0 to 255, defaulting to 128, so that script is going to take 128 ticks to fade, which is 7 seconds. You might want to speed that up (this does it in 3 seconds):

Code: Select all

DefineConstant (54, MusicFadeTicks)  # 18 * 3 seconds = 54 ticks
GlobalVariable (1,MusicVolume)

plotscript,FadeMusicOut, begin
variable (Loop)
MusicVolume := GetMusicVolume
for (Loop,MusicFadeTicks,0,-1)
do (
     SetMusicVolume (MusicVolume * Loop / MusicFadeTicks)
     wait (1)
    )
end

plotscript,FadeMusicIn, begin
variable (Loop)
for (Loop,0,MusicFadeTicks,1)
do (
     SetMusicVolume (MusicVolume * Loop / MusicFadeTicks)
     wait (1)
     )
end 
(Why did I use a constant? Because script default arguments are ignored when triggering a script from a textbox)

* (Boring technical stuff) By "shouldn't" I mean, we don't intend for it to, and you can assume that it doesn't, since everyone uses music_sdl. To my surprise music_native/native2 don't even support changing the volume, which is one reason why we stopped suggesting anyone use it.
Last edited by TMC on Thu Jan 05, 2017 11:07 pm, edited 4 times in total.
User avatar
Taco Bot
Meat, Cheese, and Silicon
Posts: 484
Joined: Fri Jul 18, 2014 12:15 am
Location: Santa Cruz
Contact:

Post by Taco Bot »

Oh shoot, looks like I didn't read through the script as thoroughly as I thought...

Sorry, I need to get more sleep before attempting to make forum posts.
Sent from my iPhone
User avatar
Gizmog
Metal King Slime
Posts: 2622
Joined: Tue Feb 19, 2008 5:41 am

Post by Gizmog »

TMC's script is pretty good, but look at how much code he duplicates! So sloppy! You can also do it this way, which looks fancier and uses fewer lines of code.

Code: Select all

DefineConstant (54, MusicFadeTicks)  # 18 * 3 seconds = 54 ticks
GlobalVariable (1,MusicVolume)  

plotscript,FadeMusicOut,begin
MusicVolume := GetMusicVolume
FadeMusic (MusicFadeTicks,0,-1)
end

plotscript,FadeMusicIn,begin
FadeMusic (0,MusicFadeTicks,1)
end

script,FadeMusic,Start,Finish,InOrOut=1,begin
for (Loop,Start,Finish,InOrOut) 
do ( 
     SetMusicVolume (MusicVolume * Loop / MusicFadeTicks) 
     wait (1) 
     ) 
end
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Don't make me pull my trump card, and build it into the engine!
Last edited by TMC on Fri Jan 06, 2017 9:05 am, edited 1 time in total.
AzureGames
Red Slime
Posts: 49
Joined: Thu Oct 29, 2015 8:18 pm

Post by AzureGames »

Gizmog wrote:SetMusicVolume command and a for loop. It'll look something, but not exactly, like this:

Code: Select all


GlobalVariable (1,MusicVolume)

plotscript,FadeMusicOut,begin
variable (Loop)
MusicVolume := GetMusicVolume
for (Loop,MusicVolume,0,-1)
do (
     SetMusicVolume (Loop)
     wait (1)
    )
end

plotscript,FadeMusicIn,begin
variable (Loop)
for (Loop,0,MusicVolume,1)
do (
     SetMusicVolume (Loop)
     wait (1)
     )
end
Post Reply