Christian Clavet Posted July 3, 2017 Share Posted July 3, 2017 Hi, I've tried lots of things to make the background music change during gameplay and I'm still failing to do it properly and now getting out of ideas. I'm using the flowgraph editor to start the music when the player take ammunition and this part work correctly. The part that is failing is that when a designated NPC is killed, I want to pause the current music and start a new music. The pause of the current music works, but the new music if failing to start. I'm using NOISE.lua and modified it slightly with monster.lua to give them the output to flowgraph. I've applied the noise.lua script to 2 pivot. One for each song. Set the script for each song. I was able to make them play both by making them play at start. The second song refuse to start when the NPC is killed, the pause work, not the other song. Here is a screenshot of my flowgraph setup: The only thing I could think of now, is look at the LUA code for noise.lua and modify it to handle 2 tracks so everthing is handled in the same script. BTW, the flowgraph editor is really powerful!! Too bad we can't group items and zoom-in/zoom-out... Quote Link to comment Share on other sites More sharing options...
Christian Clavet Posted July 3, 2017 Author Share Posted July 3, 2017 (edited) Ok. I decided to create a new script based on noise.lua. Called it noisemix.lua. If you look from the screenshot, it provide an input for switching from the first song to the second. And it finally work! When the NPC goes in chase mode after the player (modified monster.lua to have an output for this), it trigger the switch to the other song. All the songs are loaded at start, and the first one is discarded before the second start. Fell free to give any suggestions! Here is the new script: Quote Script.soundfile=""--path "Track 1" "All Supported Files:ogg,wav;Waveform Audio File Format (*.wav):wav;Ogg Vorbis (*.ogg):ogg|Sound" Script.soundfile2=""--path "Track 2" "All Supported Files:ogg,wav;Waveform Audio File Format (*.wav):wav;Ogg Vorbis (*.ogg):ogg|Sound" Script.playing=true--bool "Playing" Script.enabled=true--bool "Enabled" Script.range=50--float "Range" Script.volume=100--int "Volume" Script.pitch=1.0--float "Pitch" Script.loop=true--bool "Loop" Script.currenttrack=1 function Script:Start() self.source = Source:Create() self.source:SetVolume(self.volume/100) self.source:SetPitch(self.pitch) self.source:SetLoopMode(self.loop) self.source:SetRange(self.range) self.source:SetPosition(self.entity:GetPosition(true)) local sound = Sound:Load(self.soundfile) self.source2 = Source:Create() self.source2:SetVolume(self.volume/100) self.source2:SetPitch(self.pitch) self.source2:SetLoopMode(self.loop) self.source2:SetRange(self.range) self.source2:SetPosition(self.entity:GetPosition(true)) local sound2 = Sound:Load(self.soundfile2) if sound~=nil then self.source:SetSound(sound) if self.playing==true and self.enabled==true then self.source:Play() end sound:Release() sound=nil end if sound2~=nil then self.source2:SetSound(sound2) sound2:Release() sound2=nil end end function Script:Switch()--in self.currenttrack=2 if self.source then self.source:Pause() self.source:Release() self.source=nil end if self.source2 then self.source2:Play() end end function Script:Play()--in if self.enabled and self.currenttrack==1 then self.source:Play() end self.component:CallOutputs("Play") end function Script:Enable()--in self.enabled=true end function Script:Disable()--in self.enabled=false self.component:CallOutputs("Disable") end function Script:Pause()--in if self.currenttrack==1 then self.source:Pause() end self.component:CallOutputs("Pause") end function Script:Release() if self.source and currenttrack==1 then self.source:Release() self.source=nil end if self.source2 and currenttrack==2 then self.source2:Release() self.source2=nil end end Edited July 3, 2017 by Christian Clavet Updated script to account for track changes Quote Link to comment Share on other sites More sharing options...
brndx Posted July 4, 2017 Share Posted July 4, 2017 Rather than just switching the music right away you could decrement the volume value then pause the first sound and play the second sound then increment the volume value. Quote Link to comment Share on other sites More sharing options...
GarlicWaffle Posted July 4, 2017 Share Posted July 4, 2017 I just made something like this for my game, and the way that I did it was creating two sound sources (one with scary music and one with normal music) in a single script attached to a pivot, and setting the volume of the scary music to zero. When a monster spots you, the volume of the scary music increases to 100 while the volume of the normal music falls to zero, and they switch again if the monster goes away. 2 Quote Link to comment Share on other sites More sharing options...
Christian Clavet Posted July 4, 2017 Author Share Posted July 4, 2017 Hi! I'll try to see if I can modify the script to have a transition time. Checked Aggror video tutorial and learned much more on how to use Lua scripts.. Thanks! 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.