Jump to content
The simFlight Network Forums

Lua script very slow


Recommended Posts

First I am a very beginner in programming in LUA. I have made a script for my Captain sim 767. That script reads the Lvars from different lights, to see their status. And writes them to some FSUIPC offsets. This script is attached to this topic. In the script there is a function for checking these lights.

At the end of the script there is a event.timer to restart the function every 1 ms. But when I execute the script and a light in p3D turns on, sometimes it lastst till 2 seconds before the light turns on in my homecockpit. Am I right that this delay is caused by the time that the length of the script needs to fully execute? If so, what can I do to speed it up?

Thanks in advance.

 

Kees

76_lua_lights.lua

Link to comment
Share on other sites

12 hours ago, Kees69 said:

But when I execute the script and a light in p3D turns on, sometimes it lastst till 2 seconds before the light turns on in my homecockpit. Am I right that this delay is caused by the time that the length of the script needs to fully execute? If so, what can I do to speed it up?

How are you executing the script? If doing this on a button or key-press. then it will be compiled each time before it is ran which also takes time. Lua scripts based in events should be started from the [Auto] section, or a profile specific [Auto.xxx] section.

Also, a 1ms timer seems far too fast - do you really want to do this 1000 times a second? Try using a reasonable number (e.g. 100 for 10 times a second) and you will probably get better performance. You can also try activating debugging for lua scripts which would give you the timestamps for each line executed, which would tell you what is taking the time. I expect it will be reading the lvars, as this is delegated to the the panels.dll in P3D.

You are also using offsets reserved for other use - PMDG (0x6200 - 0x62FF) and Traffic Board (0x6300 - 0x63FF). You should be ok if you don't use those, but better to use the offsets assigned as free for general use if you can.

John

 

Link to comment
Share on other sites

Posted (edited)

John,

Thank you for your fast reply.

11 hours ago, John Dowson said:

How are you executing the script? If doing this on a button or key-press. then it will be compiled each time before it is ran which also takes time. Lua scripts based in events should be started from the [Auto] section, or a profile specific [Auto.xxx] section.

The lua files are indeed started from the Auto section:

[Auto]
1=Lua 76_lua_display
2=Lua 76_lua_lights

Quote

Also, a 1ms timer seems far too fast - do you really want to do this 1000 times a second? Try using a reasonable number (e.g. 100 for 10 times a second) and you will probably get better performance.

I have now set it to 100 ms, but there is no significant performance change.

Quote

You can also try activating debugging for lua scripts which would give you the timestamps for each line executed, which would tell you what is taking the time. I expect it will be reading the lvars, as this is delegated to the the panels.dll in P3D.

Thanks for the tip of debugging. I now see that the total script needs about 5.5 seconds to fully execute. And yes, it is because of reading all those Lvars. A second Lua script that is running and reads the displays in the MCP is much quicker. And needs only 0.4 seconds to fully execute. Is it an idea to divide the script over several scripts?

Quote

You are also using offsets reserved for other use - PMDG (0x6200 - 0x62FF) and Traffic Board (0x6300 - 0x63FF). You should be ok if you don't use those, but better to use the offsets assigned as free for general use if you can.

I used the because I saw in the documentation that they were reserved and at thios moment not used. I didn't see the PMDG file with the ofssets for the PMDG. But I don't use PMDG (and think will never use it). So for now it is ok. But you are right, maybe it is better to use the once for general use. When I have some time, I will rewrite the code.

Kind regards,

Kees

Edited by Kees69
Link to comment
Share on other sites

13 hours ago, Kees69 said:

Is it an idea to divide the script over several scripts?

Maybe worth a try.

13 hours ago, Kees69 said:

I used the because I saw in the documentation that they were reserved and at thios moment not used.

Reserved means that that the offset is being used for other purposes, maybe allocated for certain internal functions or allocated for specific add-ons. In general, you shouldn't use them. I will update the offset document at some point to specify if the offset area is used for a specific add-on, and then that area can be used if not using that add-on (I have already done this for the FSUIPC7 version og this document).

Regards.

John

Link to comment
Share on other sites

Hi Kees,

Looking at your code you could write the LUA script much simpler. I have attached a template implementing the first blocks of your code. This code should execute quick enough. Take this as an starting point to finish your script.

Btw: in your loop from 7 to 26 you are creating offset values, which waste the space from A to F. So you have unused gaps. Offsets addressed by 0x62.. etc. are hex values.

And as John said, you shouldn't use these offsets...

Rgds
Reinhard

 

 

 

76_lua_lights_simple.lua

Link to comment
Share on other sites

8 hours ago, John Dowson said:

Reserved means that that the offset is being used for other purposes, maybe allocated for certain internal functions or allocated for specific add-ons. In general, you shouldn't use them.

Yes I understand. And with the simplified script that Reinhard posted above, I will also adjust the offsets and use the ofssets for general use. Look, I've learned something new. 😉

Thanks,

Kees

Link to comment
Share on other sites

Hi Reinhard,

I understand your modified script and that is indeed a lot simpler and therefore much clearer. I had some programming experience in the past (Visual Basic and Php), but was unfamiliar with Lua. And with some trial and error I made that script and since it worked anyway, I left it that way. I knew that I skipped all offsets with A through F. However, that was a bit easier for programming with the loops.


As I mentioned in my answer to John above, I'm going to rewrite my script with your tips and then use the correct offsets right away.


Thanks for taking the timeto simplify my script.

Kind regards,

Kees

Link to comment
Share on other sites

Hi Reinhard,

I understand your modified script and that is indeed a lot simpler and therefore much clearer. I had some programming experience in the past (Visual Basic and Php), but was unfamiliar with Lua. And with some trial and error I made that script and since it worked anyway, I left it that way. I knew that I skipped all offsets with A through F. However, that was a bit easier for programming with the loops.


As I mentioned in my answer to John above, I'm going to rewrite my script with your tips and then use the correct offsets right away.


Thanks for taking the timeto simplify my script.

Kind regards,

Kees

Link to comment
Share on other sites

Hi John and Reinhard,

I just spent a few hours reprogramming my script. I have now done it as Reinhard had indicated. In addition, I divided the script into 3 lua files. In the old script of mine, lua took about 5.5 seconds to complete the script. Now every script only needs between 1.2 and 1.5 seconds. So a very nice gain of time and the lights now react clearly with less delay.

I now use the offsets starting at 0x4200.

Thanks for your help!

Kees

Link to comment
Share on other sites

10 hours ago, Kees69 said:

I now use the offsets starting at 0x4200.

That offset area is also reserved, for the sound playing  interface. You should use offsets starting from 0x66C0 (64 bytes),  0x7C40 (704 bytes) or 0xA000 (512 bytes).

John

Link to comment
Share on other sites

11 hours ago, John Dowson said:
22 hours ago, Kees69 said:

I now use the offsets starting at 0x4200.

That offset area is also reserved

Are you sure? In the manual of FSUIPC6 I read that the offsets from 0x4200 (size 256) are free for general use. See this screenshot:

Screenshot

 

Kees

Link to comment
Share on other sites

I think you may be using an older version of FSUIPC6, the latest is v6.1.8, although the offset status document is valid for 6.1.6 and later.
I remember I did recently document a few offsets as free in one version, but on reflection I decided that they were best kept as reserved for the time being until I look further  into why they are reserved. Most of these were allocated to 3rd party apps many years ago (before my time!), but some reserved areas are also used internally.

Link to comment
Share on other sites

3 hours ago, John Dowson said:

I think you may be using an older version of FSUIPC6, the latest is v6.1.8, although the offset status document is valid for 6.1.6 and later.

Indeed, I still had version 6.0.3. Now I updated to version 6.1.8 and see now that in the documentation the offsets beginning at 0x4200 are reserved. OK I will spend again a few hours programming to correct the offsets 🙂. But again I learned something.... Look much often for updates...

Link to comment
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...
 Share

×
×
  • 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.