Help with plotscripting
Moderators: Bob the Hamster, marionline, SDHawk
-
- Red Slime
- Posts: 49
- Joined: Thu Oct 29, 2015 8:18 pm
Help with plotscripting
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??
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
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):
(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.
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
* (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.
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
-
- Red Slime
- Posts: 49
- Joined: Thu Oct 29, 2015 8:18 pm
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