Jump to content
The simFlight Network Forums
Sign in to follow this  
delta757

Turbulence LUA for FS2004

Recommended Posts

Hello Pete.

I am attempting to write a LUA script that will detect turbulence (in and around the aircraft I'm flying) and trigger a wav file to rattle the tactile transducers attached to the simulator platform. Conversely, the same script will stop the sound wav once the aircraft exits the turbulent area.

I have read and re-read the documentation and looked at others scripts for guidance. Below is the script that I have currently. Will this in your opinion function?

ipcready.lua

-- FS2004 Turbulence Loop WAV (Tactile Transducers)

-- Initializing flags --

sndflg=0 -- turbulence sound flag

-- Function to play when turbulence is detected --

function turbulence(offset,value)

ground=ipc.readUW(0x366)

if (value > 0) and (sndflg == 0) and (ground == 0) then

ref=sound.playloop("d:\\esound\sounds\turbrattle.wav")

sndflg=1

ipc.sleep(1000)

end

-- Function to terminate WAV once turbulence subsides --

if (value == 0) and (sndflg == 1) then

sound.stop(ref)

sndflg=0

end

end

-- FSUIPC LUA events

event.offset(0x0E88,"UW","turbulence")

event.offset(0x0E98,"UW","turbulence")

Share this post


Link to post
Share on other sites

Below is the script that I have currently. Will this in your opinion function?

Testing is needed as well as inspection, but at a short read my only comments would be:

1. As well as "sndflg", ref needs pre-defining before the function, because the way you have it it is local to the function and therefore created and destroyed on each call because of the event.

2. All '\' characters in the path string need to be \\, not just the first, because \ is an escape character used also to define other control characters, like \n for new line

3. You don't really need the sleep call unless you want the sound to play for 1 second minimum

Regards

Pete

Share this post


Link to post
Share on other sites

Thank you Mr. Dowson for the reply. Forgive me for not testing as I am away on work and attempting to write this script as I have downtown (and peace) which allows me to concentrate more on the documentation.

I searched and found documentation on "pre-defining" functions but am a bit confused as a novice. I concluded on adding ref=0 towards the top of the script which I'm sure is probably not what you were referring to in point #1.

Regarding point #2, in the below pasted (updated) script, I have added the tandem backslashes.

Regarding point #3, the sleep call has been removed.

UPDATED SCRIPT for FS2004 TURBULENCE: (ipcready.lua)

(BEGIN)

-- Turbulence looping sound

-- Initializing flags --

sndflg=0 -- turbulence sound flag

ref=0

-- Function to play sound when turbulence is detected --

function turbulence(offset,value)

ground=ipc.readUW(0x366)

if (value > 0) and (sndflg == 0) and (ground == 0) then

ref=sound.playloop("d:\\esound\\sounds\\turb.wav")

sndflg=1

end

if (value == 0) and (sndflg == 1) then

sound.stop(ref)

sndflg=0

end

end

-- FSUIPC LUA events

event.offset(0x0E88,"UW","turbulence")

event.offset(0x0E98,"UW","turbulence")

(END)

Share this post


Link to post
Share on other sites

I searched and found documentation on "pre-defining" functions but am a bit confused as a novice. I concluded on adding ref=0 towards the top of the script which I'm sure is probably not what you were referring to in point #1.

Er, pre-defining is not a "technical term" to be looked up, it is basic English. It simply means "defining beforehand". In the context it was used, obviously simply before the function. I did actually say this, you did the same for sndlg and I said "As well as "sndflg" ... " if you look back!

Pete

Share this post


Link to post
Share on other sites

Ok, thank you. In an effort not to repeat what others have slaved over, I went to the "Contributions" folder and retrieved the turbulence script from another gentlemen. He however is using FSX while I am using FS2004. As I understand the documentation however, the offsets are now the same or have been added for FS2004.

I assume since this script has been approved for the "Contributions" folder, it must work.

I have pasted the script below for review.

When I returned home an hour ago or so, this is what I did in order:

1. Went to the Contributions folder and copied cellular55's script and pasted it into Windows Notepad. I then saved it as ipcready.lua. 2. I then copied the ipcready.lua into the FS2004 modules folder.

3. Installed the "payed" version of FSUIPC (3.999).

4. I then created a directory called D:/esound/sounds and placed a turb.wav file within.

Please note that I did not add all the other sound wav files called for in the script as I assume LUA will look for the files and if it is absent will merely overlook it. The "turb.wav" file is there.

5. Started FS2004. Got airborne, and cleared all weather.

6. Then changed the theme to "Building Storms" and the airplane began to rock.

However, the script as is did nothing. No sound file played.

I'm confused. I'm a novice to this script writing and I am damned to learn it one way or the other.

------------------------------------------------------------------------------

-- Turbulence looping sound -- Seat Belts Messages -- ATC Radio Chatter

-- Initializing flags --

sndflg=0 -- turbulence sound flag

seatfl=0 -- seat belt flag

-- Function to play sound if and until turbulence is detected --

function turbulence(offset,value)

ground=ipc.readUW(0x366)

if (value > 0) and (sndflg == 0) and (ground == 0) then

ref=sound.playloop("c:\\esound\\sounds\\turb.wav")

sndflg=1

ipc.sleep(1000)

if (seatfl == 2) or (seatfl == 4) then

sound.play("c:\\mysound\\paxsign.wav")

ipc.sleep(1000)

end

sound.play("c:\\mysound\\fastenwind.wav.wav")

end

if (value == 0) and (sndflg == 1) then

sound.stop(ref)

sndflg=0

if (seatfl == 2) or (seatfl ==4) then

sound.play("c:\\mysound\\paxsign.wav")

ipc.sleep(1000)

sound.play("c:\\mysound\\unfastenseatbelt.wav")

end

end

end

-- Function to manage messages when seat belts sign changes - welcome on board -- descent --- on cruise ---- parked at the arrival gate

function seatbelt(offset,value)

if (value == 1) and (seatfl == 0) then

sound.play("c:\\mysound\\paxsign.wav")

ipc.sleep(1000)

sound.play("c:\\mysound\\welcomeonboard.wav")

seatfl=1

end

if (value == 1) and (seatfl == 2) then

sound.play("c:\\mysound\\paxsign.wav")

ipc.sleep(1000)

sound.play("c:\\mysound\\descent.wav")

seatfl=3

end

if (value == 0) and (seatfl == 1) then

sound.play("c:\\mysound\\paxsign.wav")

ipc.sleep(1000)

sound.play("c:\\mysound\\levelautobelt.wav")

seatfl=2

end

if (value == 0) and (seatfl == 3) then

sound.play("c:\\mysound\\paxsign.wav")

ipc.sleep(1000)

sound.play("c:\\mysound\\atgate.wav")

seatfl=4

end

end

-- Function to play ATC chatter - COM2 freqs 118.10 and 118.20 - 70 wavs for each freq - y var to make the frequency of chat random - x var to make the messages random

function ATC_Radio()

-- Radio squelch if a valid freq is selected

n = ipc.readUW(0x3118)

if (n == 0x1810) or (n == 0x1820) then

if (n1 ~= n) then

sound.play("c:\\mysound\\Radio.wav")

n1=n

end

end

-- Radio chatter play routine

y= math.random(1,5)

if (y == 2) or (y == 4) then

if (n == 0x1810) then

x=math.random (1,70)

suono = "c:\\mysound\\11810\\"..x..".wav"

sound.play(suono)

end

if (n == 0x1820) then

x=math.random (1,70)

suono = "c:\\mysound\\11820\\"..x..".wav"

sound.play(suono)

end

end

end

-- FSUIPC LUA calls for the different events -- timer to exec the ATC function every 1 min

event.offset(0x0E88,"UW","turbulence")

event.offset(0x0E98,"UW","turbulence")

event.offset(0x341D,"UB","seatbelt")

event.timer(60000,"ATC_Radio")

------------------------------------------------------------------------

Share this post


Link to post
Share on other sites

4. I then created a directory called D:/esound/sounds and placed a turb.wav file within.

...

However, the script as is did nothing. No sound file played.

...

I'm confused. I'm a novice to this script writing and I am damned to learn it one way or the other.

Okay. At a quick perusal, not checking everything for you, why create a directory called D:\esounds\sounds when you have the instruction pointing to a different drive altogether? See:

ref=sound.playloop("c:\\esound\\sounds\\turb.wav")

Incidentally, why not just put whatever sounds you want into FS's "sounds" folder, which is the default path for the Lua sound library, and so save yourself all this directory hassle in the first place? Then you simply need

ref=sound.playloop("turb.wav")

And what was wrong with your original posted earlier? Do you also want all that other stuff?

Programming in any sort of language, whether an easy scripting one like Lua or a complex one like C++ is an activity demanding a level of care and attention not often needed in normal pursuits. This isn't just a matter of expertise or experience but patience ans examnation. Inspecting each line of code is an important part of checking and debugging. Ask youself, at each step, whether it makes sense, is it going to do what you actually want.

Regards

Pete

Share this post


Link to post
Share on other sites

Okay. At a quick perusal, not checking everything for you, why create a directory called D:\esounds\sounds when you have the instruction pointing to a different drive altogether? See:

Incidentally, why not just put whatever sounds you want into FS's "sounds" folder, which is the default path for the Lua sound library, and so save yourself all this directory hassle in the first place? Then you simply need

ref=sound.playloop("turb.wav")

And what was wrong with your original posted earlier? Do you also want all that other stuff?

Programming in any sort of language, whether an easy scripting one like Lua or a complex one like C++ is an activity demanding a level of care and attention not often needed in normal pursuits. This isn't just a matter of expertise or experience but patience ans examnation. Inspecting each line of code is an important part of checking and debugging. Ask youself, at each step, whether it makes sense, is it going to do what you actually want.

Regards

Pete

Great advise Peter and thank you much for taking the time to respond. I know you often feel like your pulling your hair out with some of us. Just the same, my project would never be where it is without FSUIPC and Wideclient. Thank you ever so much.

Yes, I will point the sound to the FS sound directory as per your example above.

  • Upvote 1

Share this post


Link to post
Share on other sites

Ok Pete, I think I have it now. My changes are in Red. Would like your review before I add it to Contributions folder:

ipcready.lua

-- Turbulence looping sound --

-- Initializing flags --

sndflg=0 -- turbulence sound flag

ref=0

-- Function to play sound if and until turbulence is detected --

function turbulence(offset,value)

ground=ipc.readUW(0x0366)

if (value > 0) and (sndflg == 0) and (ground == 0) then

ref=sound.playloop("turb.wav")

sndflg=1

end

if (value == 0) and (sndflg == 1) then

sound.stop(ref)

sndflg=0

end

end

end

-- FSUIPC LUA events

event.offset(0x0E88,"UW","turbulence")

event.offset(0x0E98,"UW","turbulence")

Share this post


Link to post
Share on other sites

Ok Pete, I think I have it now. My changes are in Red. Would like your review before I add it to Contributions folder:

Have you tested it?

Counting "end"s, isn't there one too many? One for each "if then" makes 2 and one to end the function makes 3 in total, yet there are 4.

Pete

Share this post


Link to post
Share on other sites

Pete,

your right and I got ahead of myself. And you were just educating me earlier on "patience and examination". Ergh.

Yes I will thoroughly test this morning and report back my results. I did in fact have it working nicely last night although with a slightly different code - the one mentioning atc chatter etc. I re-wrote after testing and omitted comments that were not desired. But yes, I will remove the 4th reference "End", test again, and report back.

On another note, now that I have the main FS PC playing this loop, which activates the tactile transducers for turbulence thumps, I would like to run the same code on a WideFS PC which will play a slightly different sound - rattling & shaking.

On PC2, I have WideFS working beautifully with Radar Contact. In the WideFS folder where Wideclient is located, I copied in a LUA file (the one I tested and had working last night) but I was unsure where the sound file should go. Should the sound file reside in the same folder with Wideclient and the LUA script, or should the script be written to point at a sound file in the main FS sound folder?

Share this post


Link to post
Share on other sites

On PC2, I have WideFS working beautifully with Radar Contact. In the WideFS folder where Wideclient is located, I copied in a LUA file (the one I tested and had working last night) but I was unsure where the sound file should go. Should the sound file reside in the same folder with Wideclient and the LUA script, or should the script be written to point at a sound file in the main FS sound folder?

Wherever you put it, it will play on the client not on the main FS PC. I don't recall offhand what the default sound folder is for WideClient -- I think it is a Sound subfolder in the same folder as Wideclient -- but if you've already run that WideClient, just look in its INI file. The current default sound path is there, in the [sounds] section.

Pete

Share this post


Link to post
Share on other sites

Pete,

Having tested for the better part of the last hour, I can confirm the below code does successfully work and achieve the desired purpose for which it was written - to detect turbulence within FS2004 and trigger a preferred sound file. At the conclusion or absence of further turbulence, the file successfully ceases or stops. Many thank you's for your patience and guidance.

-- Turbulence looping sound --

-- Initializing flags --

sndflg=0 -- turbulence sound flag

ref=0

-- Function to play sound if and until turbulence is detected --

function turbulence(offset,value)

ground=ipc.readUW(0x0366)

if (value > 0) and (sndflg == 0) and (ground == 0) then

ref=sound.playloop("turb.wav")

sndflg=1

end

if (value == 0) and (sndflg == 1) then

sound.stop(ref)

sndflg=0

end

end

-- FSUIPC LUA events

event.offset(0x0E88,"UW","turbulence")

event.offset(0x0E98,"UW","turbulence")

Share this post


Link to post
Share on other sites

Having tested for the better part of the last hour, I can confirm the below code does successfully work and achieve the desired purpose for which it was written - to detect turbulence within FS2004 and trigger a preferred sound file. At the conclusion or absence of further turbulence, the file successfully ceases or stops. Many thank you's for your patience and guidance.

Excellent. And I see you posted it in User Contributions. Thanks!

best Regards

Pete

Share this post


Link to post
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.