Jump to content
The simFlight Network Forums

Cannot get Lua events working with P3d


Recommended Posts

*** Moved from FAQ Support Forum ***

Dear fellow simmers,

it is a long time since I last modified my flight simulator setup, about 10 years and more! I had built my own console with switches and LEDs, to interface with FSX. It was working until now with FSX and Windows 7. I had programmed it myself using FSUIPC (4 I think) and Visual Basic.

Now, just 2 weeks ago I installed Windows 10 and also P3D V4.5. When trying my old program (called RealSwitches) some switches still brought the correct reactions in P3D as they did before in FSX. For example gear up and down still worked. However many things are not working.

So I decided to go ahead and write a new program for my old switch console. Reading the FSUIPC documentation I discovered Lua and did some experimenting with it. It generally works, and I tried some of the examples.

But what I cannot get working is even the simplest routine using the Lua event library. No event fires. I tried for example timer events and key events.
I thought maybe someone here knows or knows where to look. Here is my Lua code, which should just react on pressing the "V" key. From looking at the log file for the lua script I can see that the event function KeyReceived() is never called. The same was the case when I tried with timer events.


ipc.display("eins")
gCount = 10

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

function KeyReceived()
  ipc.log("gotcha")
  gCount = gCount-1
end
 
-------------------------  init ------------------------

event.key(TIC_MS, 86, 0, "KeyReceived")  -- Key v (normally make screenshot)
event.cancel("KeyReceived")
ipc.sleep(50)  
event.key(TIC_MS, 86, 0, "KeyReceived")  -- Key v (normally make screenshot)

---------------------------- loop, event handlers doing their job
while gCount>0 do
  ipc.sleep(50)  
end
---------------------------- close

sOutput = "Fertig"
ipc.display(sOutput)

ipc.exit()


Any help would be most appreciated.
Thank you
Detlef

 

Edited by John Dowson
Moved to support forum
Link to comment
Share on other sites

Hi Detlef,

how are you activating this lua? As you are using events, it should be activated from the [Auto] section of your ini file.

However, this line
    ipc.exit()
will cause your lua program to exit and no longer respond to events.

You also have:
    event.key(TIC_MS, 86, 0, "KeyReceived")  -- Key v (normally make screenshot)
    event.cancel("KeyReceived")

where you are asking a function to be called on a keycode (of TIC_MS, which is not a valid keycode) with shifts of CTRL-ALT-TAB-APPS (=86...really?), and are then cancelling this event straight away, so it will never be called.

Probably quite a few other mistakes - I haven't checked it all. I think you need to review the examples again, especially on events. Basically when you add an event (and don't cancel it or stop the lua!), the lua sits in the background waiting for the event, and then calls the appropriate function.

Also, lua logging is your friend - try activating this to find errors and see what your lua is doing.

Cheers,

John

 

P.S. You posted in the FAQ - I moved your post to the support forum - please post in this forum for all support requests,

Link to comment
Share on other sites


Hi John,
thank you very much for your fast answer. It got me thinking some more. Yes I am aware of the logging features.
I start a Lua script by setting a key (L in my case) to start the script. So if my script has the name kevent.lua I set the key L to the control Lua kevent.
I think that works fine. Thank you for pointing me to the fsuipc.ini file, though. I tried that too now.

I found out, regardless whether I start a Lua script via [Auto] section or via pressing the L key:
The events work when I just start the event at the end of a Lua program. But if I initiate the events, and wait in a loop afterwards in the same lua file for the events happening, the events do not fire.

For example this works:

ipc.display("Timer event test")
gCount = 10

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

function MyTimer()
  gCount = gCount-1
end
 
-------------------------  init ------------------------

event.timer(100, "MyTimer")

 

But this won't work: (the events don't fire)

ipc.display("Timer event test")
gCount = 10
-----------------------------------------------------
function MyTimer()
  gCount = gCount-1
end
-------------------------  init ------------------------
event.timer(100, "MyTimer")
--------------- waiting
while 1 do
    ipc.display(gCount)
    ipc.sleep(50)
end

 

So it seems I need to do that in different lua files. I now need to figure out how I can use global variables, that are global across different Lua files.
... Or use other mechanisms. I am a Lua beginner, so still learning here.


Thank you
Detlef

Link to comment
Share on other sites

Hi Detlef,

the second lua script does not end as you have an infinite loop, so the timer is never called. Try this instead:

ipc.display("Timer event test")
gCount = 10
-----------------------------------------------------
function MyTimer()
  gCount = gCount-1
  ipc.display(gCount)
end
-------------------------  init ------------------------
event.timer(100, "MyTimer")

 

13 hours ago, Detlef_747 said:

I start a Lua script by setting a key (L in my case) to start the script

Ok, then you don't need to auto-start.

13 hours ago, Detlef_747 said:

So if my script has the name kevent.lua I set the key L to the control Lua kevent.

So, after starting a script with the 'L'  key, you then wait for an 'L' key event? Sounds a strange thing to do, but ok if thats what you want....

13 hours ago, Detlef_747 said:

But if I initiate the events, and wait in a loop afterwards in the same lua file for the events happening, the events do not fire.

The script needs to end/finish (not exit! That will kill the lua), then it will react on the events. It won't react to events if the script is still running (i.e. you are in an internal loop).

13 hours ago, Detlef_747 said:

I now need to figure out how I can use global variables, that are global across different Lua files.

There are various ways to achieve this, although the recommended way using FSUIPC is to use the provided ipc.get and ipc.set functions - see the lua library documentation.

Cheers,

John

Link to comment
Share on other sites

Hi John,

thank you again.

8 hours ago, John Dowson said:

So, after starting a script with the 'L'  key, you then wait for an 'L' key event? Sounds a strange thing to do, but ok if thats what you want....

🙂 No, starting a script per L key was just a convenient way for me for my first steps with Lua. My flight sim computer is in one room, and I am sitting more comfortable in another room at a Laptop, which is connected per Windows 10 Remote Desktop . Actually I am planning to react on input from the serial interface (COM3) which is my switch console. And also on timer events. My time is limited so it will take quite a while till I get there and further with the project.

8 hours ago, John Dowson said:

The script needs to end/finish (not exit! That will kill the lua), then it will react on the events. It won't react to events if the script is still running (i.e. you are in an internal loop).

Yes, thanks for pointing that out. I think I understand that now.

Again, thanks for your help. I will do more trying and work as I find time. I attach a picture of my RealSwiches console, almost 20 years old now.

Detlef

TinylRealSwitchesConsole.jpg.0f1e5fd1031f0218764c535258c170c7.jpg

 

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • 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.