Yolo Joe 11 Posted July 6, 2015 (edited) When I run this script: engineOn.sqf _sounds = ["sound1","sound2","sound3", "sound4", "sound5"]; darter2 say3D (_sounds select ([0,(count _sounds)-1] call BIS_fnc_randomInt)); All the sounds are played at the same time. How can I make it pick a random sound, wait for it to finish and then play the next random sound? The script is called from a trigger with condition: if (isEngineOn darter2) then {execVM "engineOn.sqf"}; Edited July 6, 2015 by Chillroy Share this post Link to post Share on other sites
Grumpy Old Man 3551 Posted July 6, 2015 as long as you know how long each sound lasts you could setup a second array and use that to determine the sleep duration. Like this: while {isengineOn darter2} do { _sounds = ["sound1","sound2","sound3", "sound4", "sound5"]; _soundsleep = [3,5,2,4,4.5]; _rndsound = _sounds call BIS_fnc_selectRandom; _getsleepIndex = _sounds find _rndsound; _getsleep = _soundsleep select _getsleepIndex; darter2 say3d _rndsound; sleep _getsleep; }; cheers Share this post Link to post Share on other sites
Yolo Joe 11 Posted July 6, 2015 as long as you know how long each sound lasts you could setup a second array and use that to determine the sleep duration.Like this: while {isengineOn darter2} do { _sounds = ["sound1","sound2","sound3", "sound4", "sound5"]; _soundsleep = [3,5,2,4,4.5]; _rndsound = _sounds call BIS_fnc_selectRandom; _getsleepIndex = _sounds find _rndsound; _getsleep = _soundsleep select _getsleepIndex; darter2 say3d _rndsound; sleep _getsleep; }; cheers It's still overlapping with the sounds.. :confused: Share this post Link to post Share on other sites
Larrow 2828 Posted July 6, 2015 Tigger code runs unscheduled, due to the sleep you will need to spawn a new thread. nul = [] spawn { while {isengineOn darter2} do { _sounds = ["sound1","sound2","sound3", "sound4", "sound5"]; _soundsleep = [3,5,2,4,4.5]; _rndsound = _sounds call BIS_fnc_selectRandom; _getsleepIndex = _sounds find _rndsound; _getsleep = _soundsleep select _getsleepIndex; darter2 say3d _rndsound; sleep _getsleep; }; }; Share this post Link to post Share on other sites
Yolo Joe 11 Posted July 6, 2015 Tigger code runs unscheduled, due to the sleep you will need to spawn a new thread. nul = [] spawn { while {isengineOn darter2} do { _sounds = ["sound1","sound2","sound3", "sound4", "sound5"]; _soundsleep = [3,5,2,4,4.5]; _rndsound = _sounds call BIS_fnc_selectRandom; _getsleepIndex = _sounds find _rndsound; _getsleep = _soundsleep select _getsleepIndex; darter2 say3d _rndsound; sleep _getsleep; }; }; It's still happening.. Is there something more I have to do in the description.ext...? Description.ext class CfgSounds { sounds[] = { sound1, sound2, sound3, sound4, sound5}; class sound1 { name = "sound1"; sound[] = {\sound\Sound1.ogg, db+0, 1.0}; titles[] = {}; }; class sound2 { name = "sound2"; sound[] = {\sound\Sound2.ogg, db+0, 1.0}; titles[] = {}; }; class sound3 { name = "sound3"; sound[] = {\sound\Sound3.ogg, db+0, 1.0}; titles[] = {}; }; class sound4 { name = "sound4"; sound[] = {\sound\Sound4.ogg, db+0, 1.0}; titles[] = {}; }; class sound5 { name = "sound5"; sound[] = {\sound\Sound5.ogg, db+0, 1.0}; titles[] = {}; }; }; Share this post Link to post Share on other sites
Larrow 2828 Posted July 6, 2015 Sorry i missed this bit from the original post.. if (isEngineOn darter2) then {execVM "engineOn.sqf"}; So you where already running your code in a thread. Are you sure that your trigger is only firing once? Is the only other thing i can think of off the top of my head. Insert a systemChat in the code before the while loop to make sure. Share this post Link to post Share on other sites
Yolo Joe 11 Posted July 6, 2015 Here is a WIP video of what I'm making.. If anyone is interested :P Share this post Link to post Share on other sites
Grumpy Old Man 3551 Posted July 7, 2015 Here is a WIP video of what I'm making.. If anyone is interested :P Looking good, definitely gonna give it a try, heh. Did you adjust the values of the sleep array? The numbers have to match the duration of the sound for the script to work. like in my example it will make the script sleep for 3 seconds if the first sound is played, 4.5 seconds if it's the last one. Just tested say3D (I always use say, no idea if there's any difference between them, even I have a hard time to tell, sound the same to me), neither say3D and say make sounds overlap. Test it in the debug console, write: player say "sound1"; player say "sound1"; player say "sound1"; will make the player say the sound 3 times in a row. Same for say3d. So even running the script multiple times should not cause overlapping sounds. Like Larrow (needless to say he's one of the most experienced scripters on this forum) I got no idea what could be causing your issue either. Error shouldn't be in the description.ext, as long as the sounds are playing it's fine, it's somewhere on the way the loop is being called. Try checking everything leading to the execution of the sound loop. Cheers Share this post Link to post Share on other sites
Larrow 2828 Posted July 7, 2015 Just tested say3D, neither say3D and say make sounds overlap. Huh never noticed that before, :/ is that new-ish? Just surprised ive never noticed sounds said from the same source dont overlap and, like your suggestion Grumpy, I remember many times storing play lengths, mainly for conversations i think until i learnt how to use the kbTell system. I know having no default feedback for a sounds length has always seem to me a weakness/annoyance within Arma scripting. All those sounds commands with no return, you would of thought it possible to return an expected length from the engine even if it meant the sound had to be precached first to do so. needless to say he's one of the most experienced scripters on this forum Thank you for the kind words. Dont know about 'one of the most experienced', if it were not for some of the crazy ideas the people on this forum come up with that pique my interest I would know a lot less than i do. So to all you crazy torturous bar stewards out there that want to leave the Arma engine crying in a heap of melted PC, .... I THANK YOU. Sorry Chillroy still no clue on your problem, just Grumpy's reply leading me scrambling for the editor going whaattt! :) 1 Share this post Link to post Share on other sites
Yolo Joe 11 Posted July 7, 2015 Is there any difference between the "random" command and "BIS_fnc_selectRandom"? Share this post Link to post Share on other sites
Grumpy Old Man 3551 Posted July 7, 2015 Is there any difference between the "random" command and "BIS_fnc_selectRandom"? random is working with numbers and BIS_fnc_selectRandom is working with array elements, no matter the typename. Then there's BIS_fnc_selectRandomWeighted, selecting random elements from an array with a reference weight. array = ["apples","oranges","bananas","tomatos"]; fruits = array select floor random count array is basically the same as fruits = ["apples","oranges","bananas","tomatos"] call BIS_fnc_selectRandom without all the unreadable stuff around. this will return an equal distribution. fruits = [["apples","oranges","bananas","tomatos"],[0.125,0.125,0.25,0.5]] call BIS_fnc_selectRandomWeighted will rarely return apples and oranges, more often bananas and most likely tomatos. Let us know if you tried something different or came to a solution with your issue. Did you try using the script Larrow posted last in the editors debug window to see if it still happens? As far as I know this overlapping shouldn't happen at all. Cheers Share this post Link to post Share on other sites
Yolo Joe 11 Posted July 9, 2015 Ok, I inserted a systemChat before the while command, and the script is running over and over again as seen here: http://i.gyazo.com/70ffb7862c8951c8fde7a4dadd0cfff1.gif (5116 kB) Share this post Link to post Share on other sites
Grumpy Old Man 3551 Posted July 9, 2015 Ok, I inserted a systemChat before the while command, and the script is running over and over again as seen here:http://i.gyazo.com/70ffb7862c8951c8fde7a4dadd0cfff1.gif (5116 kB) If it's before the while loop then the script is being called more than once, leading to the (still strange) overlapping of sounds. How are you calling this script? Try to look for an error there, maybe insert an exitWith and execute the .sqf from inside the exitWith statement, as a temporary solution. The error is definitely within the routine that's calling the engineOn.sqf On a sidenote you could totally call that script from an engine eventhandler, would actually be the preferred way here. Cheers Share this post Link to post Share on other sites
Yolo Joe 11 Posted July 9, 2015 If it's before the while loop then the script is being called more than once, leading to the (still strange) overlapping of sounds.How are you calling this script? Try to look for an error there, maybe insert an exitWith and execute the .sqf from inside the exitWith statement, as a temporary solution. The error is definitely within the routine that's calling the engineOn.sqf On a sidenote you could totally call that script from an engine eventhandler, would actually be the preferred way here. Cheers As mentioned in the first post, The script is called from a trigger with condition: if (isEngineOn darter2) then {execVM "engineOn.sqf"}; Share this post Link to post Share on other sites
Yolo Joe 11 Posted July 9, 2015 Can anyone help me with this eventHandler thing? :cool: Share this post Link to post Share on other sites
dreadedentity 278 Posted July 10, 2015 This may be what you are looking for: isMusicPlaying = false; //toggle to let scripts know if music is already playing _musicStartEH = -1; _musicStopEH = -1; while {_musicStartEH == -1} do { _musicStartEH = addMusicEventHandler ["MusicStart", { isMusicPlaying = true; systemChat format ["%1 has started playing", (_this select 0)]; }]; }; //safely add event handler, loop will continue to run until the event handler is added while {_musicStopEH == -1} do { _musicStopEH = addMusicEventHandler ["MusicStop", { isMusicPlaying = false; systemChat format ["%1 has finished playing", (_this select 0)]; }]; }; //safely add event handler, loop will continue to run until the event handler is added { waitUntil {!isMusicPlaying}; //halt loop until music is finished playMusic _x; } forEach ["MySound1","MySound2","MySound3"]; //Play 3 sounds from CfgMusic; MySound1, MySound2, MySound3 Do note that this was hastily-written and not tested, so it may contain errors or simply not work at all. Also, since it uses waitUntil, it's probably best to run this code in a scheduled environment (like from init.sqf) Share this post Link to post Share on other sites
Ranwer135 308 Posted July 10, 2015 EventHandler (multiplayer) player addMPEventHandler ["MPRespawn", {_this execVM "yourscript.sqf"}; yourscript.sqf: mysounds = {_list = playSound ["sound1","sound2"...] call BIS_fnc_selectRandom;waitUntil {scriptDone _list};[] spawn mysounds}; [] spawn mysounds; Something like that, i'm on mobile at the moment. :) Share this post Link to post Share on other sites