Sim Dave Posted November 5, 2017 Report Posted November 5, 2017 I'm trying to get my head around triggering sounds in FSX & FSUIPC v4.959 with the following .lua script function playme (path) if(sound.query(path)) then -- do nothing else PumpSound = sound.play(path) end end -- external toggle switch if ipcPARAM == 1 then -- Toggle ON playme("ElectricPump.wav") end if ipcPARAM == 0 then -- Toggle OFF sound.stop("ElectricPump.wav") end The sound plays ok, but I cant stop it, nor can I stop it from starting multiple times. what I am missing? I started with sound.play(ElectricPump.wav) sound.stop(ElectricPump.wav) it plays, but can't be stopped! the .wav file is about 7 seconds long
Pete Dowson Posted November 6, 2017 Report Posted November 6, 2017 23 hours ago, Sim Dave said: The sound plays ok, but I cant stop it, nor can I stop it from starting multiple times. what I am missing? You need to refer to the Lua library documentation! Both sound.query and sound.stop need the REFERENCE to the current sound you started, not the wave path! i.e. in your case PumpSound. Pete
Sim Dave Posted November 6, 2017 Author Report Posted November 6, 2017 I have spent hours on something I thought would be a 5-minute job! having looked again at the log files I have spotted that the sound.query creates an entry 'LUA: Global: Pumpsound = 1' next time the .lua has trigged this increments to 2 and so on. effectively the sound.stop is never going to work the way I'm trying.
Thomas Richter Posted November 9, 2017 Report Posted November 9, 2017 Hi, but did you read Pete's answer and changed your code corresponding with a Reference to the sound instead? Thomas
Sim Dave Posted November 10, 2017 Author Report Posted November 10, 2017 Yes Thomas. The problem is when you toggle the switch the sound starts and .lua finishes, when you toggle the switch off the .lua triggers but it has lost all reference to the sound so can't stop it or starts another instance. Well that is what I think is happening by integrating the sound.query log entry. I did wonder if I tied the sound start in a loop and then stop the sound and killed the loop with the toggle off might work, but have not gone back to it as it annoyed the hell out of me ;)
spokes2112 Posted November 11, 2017 Report Posted November 11, 2017 Dave, You need to use "listening" code or a loop to keep the lua alive. The best way is a listener, it's all part of the Event Library in the Lua Library.pdf. The one drawback is that it should be started via the [Auto] section in the .ini . Although it could be started in some other fashion. There should be no need for a query since the flag can only be 1 (sound playing) or 0 (sound off). Maybe this will help - taken from a previous code project (it works) and not tested in this example. Roman -- Use (LuaSet "lua name") to start the sound and (LuaClear "lua name") to stop it. -- The parameter for the FSUIPC UI in this code example is zero -- The lua should be started via the [Auto] section in the FSUIPC#.ini -- OPTION, adjust path as needed, if wanted -- sound.path("G:/FSX/Modules") local flagged = 0 function Sound_Control(flag) flagged = ipc.testflag(0) if flagged == true then -- OPTION, play sound once -- MySound = sound.play("ElectricPump.wav") -- OPTION, loop the sound continuously until turned off MySound = sound.playloop("ElectricPump.wav") else sound.stop(MySound) end end event.flag(0,"Sound_Control")
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