Jump to content
The simFlight Network Forums

Lua program help


Recommended Posts

while 1 do
  event.key(122,0,"fnident")
  ipc.sleep(20)
end

The log shows 122 as the keypressed.

What am I doing wrong?

The problem is that the section shown stops the program ever exiting. It loops forever continually adding the function "fnident" as a routine to be called whenever keypress 122,0 occurs. It would be eating up your memory at the rate of something like 16 bytes per 20 mSecs until it crashed FS, eventually (it might take a long long time, though).

Nothing will ever call "fnident" because when the event occurs (i.e. you press the key), the Lua program is already running and the "fnident" function is not directly accessible.

It seems that you are mixing up two different ways of programming Lua for FSUIPC.

One is to have the program looping, testing something at intervals, as in:

while 1 do
   if  then

   end
   ipc.sleep(20)
end

This loops, never ending, and checks on some condition at intervals.

That is not the best way for many things, especially if the thing you are waiting for is a rare occurrence. Better by far is the Event system, in which your program merely tells FSUIPC what it wants it to monitor and what to call if it occurs, then ends. In your case, simply this:

event.key(122,0,"fnident")

In other words, just replace your entire while loop with the event.key call only.

Regards

Pete

Link to comment
Share on other sites

Ooops! I'm sorry, I edited your question with my reply, instead of adding myreply. One button difference! Apologies.

Here's the reply. Sorry but your original post is unrecoverable! :-(

while 1 do
  event.key(122,0,"fnident")
  ipc.sleep(20)
end

The log shows 122 as the keypressed.

What am I doing wrong?

The problem is that the section shown stops the program ever exiting. It loops forever continually adding the function "fnident" as a routine to be called whenever keypress 122,0 occurs. It would be eating up your memory at the rate of something like 16 bytes per 20 mSecs until it crashed FS, eventually (it might take a long long time, though).

Nothing will ever call "fnident" because when the event occurs (i.e. you press the key), the Lua program is already running and the "fnident" function is not directly accessible.

It seems that you are mixing up two different ways of programming Lua for FSUIPC.

One is to have the program looping, testing something at intervals, as in:

while 1 do
   if  then

   end
   ipc.sleep(20)
end

This loops, never ending, and checks on some condition at intervals.

That is not the best way for many things, especially if the thing you are waiting for is a rare occurrence. Better by far is the Event system, in which your program merely tells FSUIPC what it wants it to monitor and what to call if it occurs, then ends. In your case, simply this:

event.key(122,0,"fnident")

In other words, just replace your entire while loop with the event.key call only.

Regards

Pete

Link to comment
Share on other sites

Pete,

Thank you for the quick response.

I come from a VB background and it looks like Lua is a bit different.

See if I understand this correctly.

If I put this code in file Ipcready.lua, it will process the code when

the flight sim is ready to fly. Once it code is processed the function is created and the event

is created and is ready to use. Once the event is created I don't have to create a loop to

rescan it at given intervals.

Thanks again,

Mark

Link to comment
Share on other sites

If I put this code in file Ipcready.lua, it will process the code when

the flight sim is ready to fly. Once it code is processed the function is created and the event

is created and is ready to use. Once the event is created I don't have to create a loop to

rescan it at given intervals.

Yes, correct. The event function call causes the Lua program, ready compiled, to be sitting in its own thread waiting for the events. It would only ever terminate then if you expressly used the ipc.exit() function.

However, as a matter of convenience, you might want to retain the Lua as a separate file, and only put a call into an ipcReady.lua file to start it. Then, if you have other Lua actions to add later, you can keep them separate (better as they all run in their own threads, and easier to keep track of and debug etc).

To do this simply use the ipc.macro() facility. i.e. in an ipcReady.lua file just have a line like this:

ipc.macro("Lua xxxxx") where "xxxxx" is the name of your Lua file (without the .lua part).

Regards

Pete

Link to comment
Share on other sites

Pete,

However, as a matter of convenience, you might want to retain the Lua as a separate file, and only put a call into an ipcReady.lua file to start it. Then, if you have other Lua actions to add later, you can keep them separate (better as they all run in their own threads, and easier to keep track of and debug etc).

To do this simply use the ipc.macro() facility. i.e. in an ipcReady.lua file just have a line like this:

ipc.macro("Lua xxxxx") where "xxxxx" is the name of your Lua file (without the .lua part).

Super idea, Thanks again for your help.

Mark

Link to comment
Share on other sites

  • 2 weeks later...

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.