NotAvailable Posted December 17, 2010 Share Posted December 17, 2010 Hi, Just a little question: I want to make random sounds Eg. I have 22 .mp3 files. Every 1 or 2 minutes u hear a random sound. How do i create that? Regards, Jesseunit Link to comment
Discord Moderators Zango Posted December 17, 2010 Discord Moderators Share Posted December 17, 2010 (edited) name them a number from 0 to 22 INTERVAL = 2 function playRandomSound () playSound (math.random (0, 22)..'.mp3', false) end setTimer (playRandomSound, INTERVAL * 60000, 0) assuming you have 22 mp3's defined with 1.mp3, 2.mp3 etc. Edited December 17, 2010 by Guest Link to comment
Deltanic Posted December 17, 2010 Share Posted December 17, 2010 (edited) Place this where you want it: local soundsTable = {} sounds[1] = "somemusic.mp3" sounds[2] = "somesound.mp3" -- And go on like that, until you got 22 mp3 files. -- Ofcourse add those sounds to your meta. And use this in your timer function: local rand = math.random ( 1, #sounds ) playSound ( sounds[rand] ) @ Zango: That's a way too, but you will need to call your file with a number. So you have to know which number is what sound. Edited December 17, 2010 by Guest Link to comment
Castillo Posted December 17, 2010 Share Posted December 17, 2010 oh lol, i was going to post same idea remi Link to comment
Discord Moderators Zango Posted December 17, 2010 Discord Moderators Share Posted December 17, 2010 Well, if you need to keep track of what sound is what - use Remi's If you want the shit to be done fast, use mine Link to comment
Aibo Posted December 17, 2010 Share Posted December 17, 2010 and if want you to combine the two ways: parse meta.xml nodes with mp3's into a table on resource start. that way you wont have to edit the table when you add/delete mp3 files from meta/resource. function getRandomSounds() local t = {} local meta = xmlLoadFile("meta.xml") for i, node in ipairs(xmlNodeGetChildren(meta)) do if xmlNodeGetName(node) == "file" and xmlNodeGetAttribute(node, "src"):find("someRandomSoundsFolder/.+\.mp3") then table.insert(t, xmlNodeGetAttribute(node, "src")) end end xmlUnloadFile(meta) return t end Link to comment
NotAvailable Posted December 17, 2010 Author Share Posted December 17, 2010 Thanks everybody Link to comment
NotAvailable Posted December 17, 2010 Author Share Posted December 17, 2010 Hmmm, its still not working This is the code(Client): local soundsTable = {} sounds[1] = "richt1.mp3" sounds[2] = "richt2.mp3" sounds[3] = "richt3.mp3" sounds[4] = "richt4.mp3" sounds[5] = "richt5.mp3" sounds[6] = "richt6.mp3" sounds[7] = "richt7.mp3" sounds[8] = "richt8.mp3" sounds[9] = "richt9.mp3" sounds[10] = "richt10.mp3" sounds[11] = "richt11.mp3" sounds[12] = "richt12.mp3" sounds[13] = "richt13.mp3" sounds[14] = "richt14.mp3" sounds[15] = "richt15.mp3" function randomSound() local rand = math.random ( 1, #sounds ) playSound ( sounds[rand], false ) setSoundVolume(sounds, 0.9) end setTimer(randomSound, 10000, 0) meta.xml: "JESSEUNIT" version="1" type="script" /> "richt1.mp3" /> "richt2.mp3" /> "richt3.mp3" /> "richt4.mp3" /> "richt5.mp3" /> "richt6.mp3" /> "richt7.mp3" /> "richt8.mp3" /> "richt9.mp3" /> "richt10.mp3" /> "richt11.mp3" /> "richt12.mp3" /> "richt13.mp3" /> "richt14.mp3" /> "richt15.mp3" /> Link to comment
Castillo Posted December 17, 2010 Share Posted December 17, 2010 i'm not sure but i think if you create a table called soundsTable then you must do like soundsTable[1] etc (i'm not sure as i said) Link to comment
NotAvailable Posted December 17, 2010 Author Share Posted December 17, 2010 i'm not sure but i think if you create a table called soundsTable then you must do like soundsTable[1] etc (i'm not sure as i said) Yeah, i thought that to. Link to comment
Castillo Posted December 17, 2010 Share Posted December 17, 2010 then try to remplace all sounds[1], sounds[2].. etc with soundsTable[1] Link to comment
NotAvailable Posted December 17, 2010 Author Share Posted December 17, 2010 then try to remplace all sounds[1], sounds[2].. etc with soundsTable[1] k ill try it Link to comment
NotAvailable Posted December 17, 2010 Author Share Posted December 17, 2010 Ty it works! Link to comment
Castillo Posted December 17, 2010 Share Posted December 17, 2010 great, now i'm sure it was like i said, lol. Link to comment
Arran Posted December 18, 2010 Share Posted December 18, 2010 This is how I'd have done the table: local soundsTable = { "richt1.mp3", "richt2.mp3", "richt3.mp3", "richt4.mp3", "richt5.mp3", "richt6.mp3", "richt7.mp3", "richt8.mp3", "richt9.mp3", "richt10.mp3", "richt11.mp3", "richt12.mp3", "richt13.mp3", "richt14.mp3", "richt15.mp3", } You don't even have to specify the index because its added automatic. math.random(#soundsTable) to get one, and then to use it playSound(soundsTable[math.random(#soundsTable)], false) Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now