• "playsound" as a playlist playing thing.
    5 replies, posted
Hey lads, I'd like to make a Server, but with good ambience music which plays using the "Playsound" command. Mostly songs from the CnC 3 Game and probably RF: Guerilla. How could I make it a loop, meaning when one songs ends, there is a small pause in between and then it plays the next song and it repeats itself when the playlist is finished? I have basic LUA knowledge, but would like a very simple Code. Is that possible?
So, write something before asking for help and maybe someone will help you. Its easy to say[QUOTE=Toedeli;51972947]I have basic LUA knowledge, but would like a very simple Code.[/QUOTE] And yes, its possible with basic LUA knowledge.
What you could do is use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Global/SoundDuration]SoundDuration[/url] and [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/timer/Simple]timer.Simple[/url] Example: [CODE] local soundind = 0 -- the current sound being played (at the moment it's 0 since the function increases it to 1, meaning the first sound in the playlist) local playlist = { -- put all sounds you want to play in here 'garrysmod/balloon_pop_cute.wav', -- example sound 1 'garrysmod/content_downloaded.wav', -- example sound 2 'garrysmod/ui_click.wav' -- example sound 3 } local function PlayStuff() -- run this function to start playing the stuff soundind = soundind + 1 -- this makes it go on to the next thing in the playlist if soundind > #playlist then soundind = 1 end -- this makes it go back to the start of the playlist if it ends surface.PlaySound( playlist[ soundind ] ) -- play the current sound by its index timer.Simple( SoundDuration( playlist[ soundind ] ), PlayStuff ) -- this makes the function run itself, meaning it repeats all this again for the next sound end PlayStuff() -- start the music [/CODE] The code may look a bit confusing but at least it works :P You can have as many sounds as you want, just add them to the playlist, and remember to run PlayStuff() If you want to add a little pause before it goes on to the next track then you just need to add something in the timer.Simple part, for example: [CODE] timer.Simple( SoundDuration( playlist[ soundind ] ) + 1, PlayStuff ) -- this makes it go 1 second after the previous [/CODE] If you're not sure what this code is doing then [URL="https://www.lua.org/pil/2.5.html"]this may help you a little[/URL]
[QUOTE=MPan1;51976439]What you could do is use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Global/SoundDuration]SoundDuration[/url] and [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/timer/Simple]timer.Simple[/url] Example: [CODE] local soundind = 0 -- the current sound being played (at the moment it's 0 since the function increases it to 1, meaning the first sound in the playlist) local playlist = { -- put all sounds you want to play in here 'garrysmod/balloon_pop_cute.wav', -- example sound 1 'garrysmod/content_downloaded.wav', -- example sound 2 'garrysmod/ui_click.wav' -- example sound 3 } local function PlayStuff() -- run this function to start playing the stuff soundind = soundind + 1 -- this makes it go on to the next thing in the playlist if soundind > #playlist then soundind = 1 end -- this makes it go back to the start of the playlist if it ends surface.PlaySound( playlist[ soundind ] ) -- play the current sound by its index timer.Simple( SoundDuration( playlist[ soundind ] ), PlayStuff ) -- this makes the function run itself, meaning it repeats all this again for the next sound end PlayStuff() -- start the music / earrape [/CODE] The code may look a bit confusing but at least it works :P You can have as many sounds as you want, just add them to the playlist, and remember to run PlayStuff() [editline]18th March 2017[/editline] If you're not sure what this code is doing then [URL="https://www.lua.org/pil/2.5.html"]this may help you a little[/URL] [editline]18th March 2017[/editline] If you want to add a little pause before it goes on to the next track then you just need to add something in the timer.Simple part, for example: [CODE] timer.Simple( SoundDuration( playlist[ soundind ] ) + 1, PlayStuff ) -- this makes it go 1 second after the previous [/CODE][/QUOTE] Thanks mate! I have some questions though. 1 ) When I make "stopsound" in console, will I have to restartto hear again or will the Playlist continue on the next track? 2) I have an addon installed for simple music. When I play one of them, will this script stop until it is done? 3)Will it automatically start each time I launch the server? If no, what to do to get it like that?
All my thing does is a really basic method to play sounds in a loop - it just plays the first sound and then makes a timer that plays the next sound after the first. [B]1)[/B] If you run stopsound it'll stop the currently playing sound but it won't stop the timer, which means the 'playlist' (or rather the timer) will keep going. This means that if you ran 'stopsound' the next sound would still play after the duration of the first one, even if the first one isn't playing. It's a bit annoying and I didn't really add an easy method to stop the sounds. If you wanted a good way to stop the sounds you'd probably have to use timer.Create instead of timer.Simple so the timer had an identifier, meaning that you could use timer.Remove on it [B]2)[/B] The thing I posted doesn't have anything to do anything with other addons. It won't stop or anything like that if another addon plays a sound or does anything. If you want it to do that then you'll have to figure out how the other addon works. [B]3)[/B] It starts whenever you call PlayStuff(). If you want it to start when you launch the server then you just have to call it when the player initializes. You could try just running it at the bottom of the script like I did but a better way would be to use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/PlayerInitialSpawn]GM:PlayerInitialSpawn[/url] and the [URL="https://wiki.garrysmod.com/page/Net_Library_Usage"]net library[/URL] E.G. serverside: [CODE] util.AddNetworkString( 'playmusic' ) hook.Add( 'PlayerInitialSpawn', 'playmusic', function( ply ) net.Start( 'playmusic' ) net.Send( ply ) end ) [/CODE] clientside: [CODE] local soundind = 0 local playlist = { 'garrysmod/balloon_pop_cute.wav', 'garrysmod/content_downloaded.wav', 'garrysmod/ui_click.wav' } local function PlayStuff() soundind = soundind + 1 if soundind > #playlist then soundind = 1 end surface.PlaySound( playlist[ soundind ] ) timer.Simple( SoundDuration( playlist[ soundind ] ), PlayStuff ) end net.Receive( 'playmusic', function() PlayStuff() end ) [/CODE] [editline]18th March 2017[/editline] If you want this thing to interact with other addons then it'll start to get more complicated - and in your first post you wanted simple code, and this is as simple as I can think at the moment
Wow mate... you are fucking great. Last question: For my Server, where to put the Data? In the LUA folder?
Sorry, you need to Log In to post a reply to this thread.