Jump to content
The simFlight Network Forums

First attempt with Lua


Jacob Wiqvist

Recommended Posts

Hi all!

 

This is my first attempt with lua, or any code for that matter.

 

I know nothing about programing so pleas bare with me.

 

My first attempt is to get one FSUIPC Offset to change another offset from 0 to 1. But this dose not seem to work.

 

I use MSFS2020 and are using FSUIPC7

I have pot the "test.lua" text file in the root of FSUIPC folder.

 

This is the code I am trying.

If (ipc.readUW(0x67E7) == 1) then

ipc.writeUB (0x66E2, 1)

end

 

 

 

Edited by Jacob Wiqvist
Link to comment
Share on other sites

This is what FSUIPC log say:

 

      234 -------------------------------------------------------------------
      266 LogOptions=00000000 00000001
     3156 Simulator detected
     8187 SimConnect_Open succeeded
     8187 Running in "KittyHawk", Version: 11.0.282174.999 (SimConnect: 11.0.62651.3)
     8187 MSFS version = 11.0.282174.999
     8187 Initialising SimConnect data requests now
     8203 C:\Users\jacob\AppData\Local\Packages\Microsoft.FlightSimulator_8wekyb3d8bbwe\LocalState\MISSIONS\Custom\CustomFlight\CustomFlight.FLT
     8203 SimObjects\Airplanes\prosim_b738\aircraft.CFG
     8203 User Aircraft ID 1 supplied, now being used
     8281 Aircraft loaded: running normally now ...
     8953 Aircraft="Prosim_B738 2021a KLM"
     8953 System time = 14/02/2021 14:07:51, Simulator time = 13:18:48 (12:18Z)
    16016 -------------------- Starting everything now ----------------------
    16094 LUA.1: beginning "E:\FSUIPC7\test.lua"
    16094 LUA.1: E:\FSUIPC7\test.lua:1
    16109 LUA.1: Global: ipcPARAM = 0
    16125 LUA.1: E:\FSUIPC7\test.lua:5
 

Link to comment
Share on other sites

Whats the issue? The log shows your lua is running ok.
However, you are reading from 0x67E7, which is in a reserved area. Are you sure thats correct?

Other than that, your lua is running ok - it will just run and exit.
If you want to keep it running, and update an offset when another offset changes, you probably want to ne using the event.offset function. Then the lua thread will keep running, waiting for that offset to change, and then calling the event function, in which you can write to your other offset.

If thats not what you need, then maybe activate the lua on a button or key press rather than have it auto-started.

38 minutes ago, Jacob Wiqvist said:

This is what I have written in the config file in the bottom of fsuipc.


[Auto]
1=lua test

[LuaFiles]
1=test

Don't write the [LuaFiles] section. This is constructed automatically from the lua files (old/removed and new) and shouldn't be manually changed. Just add your luas to the [Auto] section when you want them auto-started.

John

 

Link to comment
Share on other sites

1 minute ago, John Dowson said:

Whats the issue? The log shows your lua is running ok.
However, you are reading from 0x67E7, which is in a reserved area. Are you sure thats correct?

Other than that, your lua is running ok - it will just run and exit.
If you want to keep it running, and update an offset when another offset changes, you probably want to ne using the event.offset function. Then the lua thread will keep running, waiting for that offset to change, and then calling the event function, in which you can write to your other offset.

If thats not what you need, then maybe activate the lua on a button or key press rather than have it auto-started.

Don't write the [LuaFiles] section. This is constructed automatically from the lua files (old/removed and new) and shouldn't be manually changed. Just add your luas to the [Auto] section when you want them auto-started.

John

 

Ofcourse I want to use free ofssets. 

 

Can you change the code with a free offest and event.offset function on the correct place.

 

I changed, to this:

if (ipc.readUB(0x67E7) == 1) then

   ipc.writeUB (0x66E2, 1)

end

 

I changed offset and WB to UB, I hope that is correct?

 

 

Link to comment
Share on other sites

9 minutes ago, John Dowson said:

Whats the issue? The log shows your lua is running ok.
However, you are reading from 0x67E7, which is in a reserved area. Are you sure thats correct?

Other than that, your lua is running ok - it will just run and exit.
If you want to keep it running, and update an offset when another offset changes, you probably want to ne using the event.offset function. Then the lua thread will keep running, waiting for that offset to change, and then calling the event function, in which you can write to your other offset.

If thats not what you need, then maybe activate the lua on a button or key press rather than have it auto-started.

Don't write the [LuaFiles] section. This is constructed automatically from the lua files (old/removed and new) and shouldn't be manually changed. Just add your luas to the [Auto] section when you want them auto-started.

John

 

I also removed the: 

 

[LuaFiles]
1=test

 

Link to comment
Share on other sites

2 minutes ago, Jacob Wiqvist said:

I also removed the: 

[LuaFiles]
1=test

Why? As I said, just don't touch that section. It is managed by FSUIPC - leave it alone!

6 minutes ago, Jacob Wiqvist said:

Can you change the code with a free offest and event.offset function on the correct place.

 

I changed, to this:

if (ipc.readUB(0x67E7) == 1) then

   ipc.writeUB (0x66E2, 1)

end

 

I changed offset and WB to UB, I hope that is correct?

Why did you change it? What are you trying to read? You use the read function corresponding to the data type of the data held in the offset you want to read.

Please see the lua documentation for event.offset, in the Lua Library document. Also, take a look at the example lua files provided - there should be a zip file containing examples in your FSUIPC documents folder.

Link to comment
Share on other sites

2 minutes ago, John Dowson said:

Why? As I said, just don't touch that section. It is managed by FSUIPC - leave it alone!

Why did you change it? What are you trying to read? You use the read function corresponding to the data type of the data held in the offset you want to read.

Please see the lua documentation for event.offset, in the Lua Library document. Also, take a look at the example lua files provided - there should be a zip file containing examples in your FSUIPC documents folder.

I thought you ment that:

[LuaFiles]
1=test

Needed too be removed.

I added those lines my self. bothe [Auto] and [LuaFiles]

I tried to read page 29 of 40: 

event.offset(offset, "type", "function-name") event.offset(offset, "STR", length, "function-name") Your processing function: function-name(offset, value)

Executes the named function (named as a string, “...”), which must be defined before this line, when the specified FSUIPC offset changes The function is also executed initially, when the plugin is first run, in order to initialise things. This saves using an explicit call to do the same. The offset can be specified in Lua format hexadecimal, e.g. 0x0AEC, or in decimal, or as a string e.g. “0AEC”. The type is one of these:

UB unsigned 8-bit byte

UW unsigned 16-bit word

UD unsigned 32-bit dword

SB signed 8-bit byte

SW signed 16-bit word

SD signed 32-bit dword

DD signed 64-bit value

DBL 64-bit double floating point

FLT 32-bit single floating point

STR string of ASCII characters

The length parameter is omitted (or ignored) except for the “STR” type, where it can optionally define the string length (max 256). If the length is omitted for the STR type then the string will be zero terminated and will have a maximum length of 255 not including the final zero. The function is called with the offset, so that the same function can, if desired, be used for more than one such event, and also the current (new) value in that offset. This will be a Lua number for all types except STR where it will be a string

 

I read this thread: https://www.flightsimsweden.se/knowledge-base/lua/


Maybe it is better I explain what I want to do.

I want to read 2 offsets and when they bothe are more then 10000 in value, I want another offset to change from 0 to 1. 

The 2 offset should be betwen -16383 to + 16383

If bothe are under 10000 an offset change to 0 and if bothe are over 10000 an offset change to 1.

 

I just wanted with this first test to se If I could get one offset to change another offsets value with a lua script.

 

Regards.

Link to comment
Share on other sites

2 minutes ago, Jacob Wiqvist said:

I thought you ment that:

[LuaFiles]
1=test

Needed too be removed.

No. For the last time, this is auto-generated. Just leave it alone.

Why are you quoting the user manual at me?

3 minutes ago, Jacob Wiqvist said:

I want to read 2 offsets

Ok. Which offsets? What do they hold?

If you want to do this for two offsets, then depending upon how quick you want your offset to change, there are different methods you can use.
The easiest way is just to use a timer (event.timer). In the timer, read the offset values, do the comparison and then write your value when needed.
The second would involve to event.offset functions, each monitoring one of your 'read' offsets, and setting a global flag when your condition is true.
Each offset function should then check both global flags, and call write to your offset if your condition is met.

There are plenty of lua examples, both included with FSUIPC7 and available in the user Contributions sections here. I suggest you take a look at some.
I really haven't got time for basic lua lessons, sorry. Just look at the documentation and examples and try things out.
 

Link to comment
Share on other sites

2 minutes ago, Jacob Wiqvist said:

If someone els could help me I would realy apriciate the help.

Why don't you just try? If you try yourself and post if you have errors, you are more likely to get a response.
Try something like:

Quote

 

function myFunction(time)

    -- read 1st offset
   val1  = read??(????)
   val2 = read??(????)

   if val1 > 10000 and val2 > 10000
        ipc.writeUB (0x66E2, 1)
  else
        ipc.writeUB (0x66E2, 0)
  end

end

 

event.timer(200, "myFunction")

 

That will be called 5 times a second, and will change the value in offset 0x66E2 to match your criteria (i.e. values in the other two as-of-yet unspecified offsets are > 10000 then 1, else 0).

Replace the ? with the correct offsets/types you are reading. Still don't know what those are.

Can you see how that works?
Also, note that you can use ipc.log to log a message, which can help you to debug and also see how your lua is working.

Link to comment
Share on other sites

I think I use the wrong offset typ/format in the script, berceuse Offset 0x66E2 is relesed when 

0x66E1 go to 0 not over 10000 or 15000 like I want.

FSUIPC AXIS write to x66E1, Offset Dword Set

ProSim brake axis read 0x66E6 (FSUIPC 16 Bit S) 

I want the script to read 66E1 (The brake axis)

And if the value goes over 15000 send and keep the Offset 0x66E2 to 1 and if the brake axis go under say 15000 I want the value to go to 0.

 

This dose not seem to work?

 

function myFunction(time)

    -- read 1st offset
   val1 = readUD (0x66E1)
   val2 = readUD (0x66E1)

   if val1 > 15000 and val2 > 15000 then
        ipc.writeUB (0x66E2, 1)
  else
        ipc.writeUB (0x66E2, 0)
  end

end

 

event.timer(200, "myFunction")

 

Regards Jacob
 

Link to comment
Share on other sites

Maybe this is more clear:

What I want is:

1: 2 axis prograned to two FSUIPC offsets. Let's say 0x66E1 

 

2: I want the Lua script to read the two offsets, continuously several times a second.

 

3: When bothe of the offsets read a value over 15000 I want another offset to change from 0 to 1.

 

4: When bothe or any of the offsets go below 15000 I want the offset that changed from 0 to 1, to go to 0.

 

5: I want this to happen in real time.

 

I have tried to make a script but it doesn't work and it write a value of -64 to + 64 to the offset 0x66E2, instead of 0 or 1.

Link to comment
Share on other sites

14 hours ago, Jacob Wiqvist said:

This dose not seem to work?

How do you know it doesn't work?  What was the result?

It will not be as you want because you are using 0x66E1 to 0x66E4 as a 4 byte offset (UD = unsigned DWORD, which is 32 bits or 4 bytes), and you are writing 0 or 1 to 0x66E2, which is in the middle of the UD value in 0x66E1.

What is supposed to be in the 4 byte offset 0x66E1?  You said 

Quote

The 2 offset should be betwen -16383 to + 16383

So you want a 2 byte SIGNED word (SW) not an UNsigned double word!

You must make sure the offsets used do not overlap, like your 4 byte 0x66E1 and one byte 0x66E2.

And if you want to check the value is between -15000 and +15000 you need to revise your test, obviously:

if (val1 < -15000) or (val1 > 15000) then ...

etc.

Pete

 

Link to comment
Share on other sites

14 hours ago, Jacob Wiqvist said:

FSUIPC AXIS write to x66E1, Offset Dword Set

If you are writing a double word (4 bytes) to 0x66E1. then that value is held in 4 bytes from that offset, so in 0x66E1-0x66E4 inclusive.
However, you are overwriting that value here: ipc.writeUB (0x66E2, 1).
Try using 0x66E5 instead.

Also, is the value signed or unsigned? This is important when reading. I assume signed below - change to unsigned if needed.

Using event.timer was also just an example to get you going. Its probably more efficient to use event.offset, and you can use the same handling function for multiple offsets.

4 hours ago, Jacob Wiqvist said:

1: 2 axis prograned to two FSUIPC offsets. Let's say 0x66E1 

But that is one offset. What is the second offset you are using? And always specify the size of the value you are writing to that offset. If you are writing an axis value of as a dword, then it will take 4 bytes. If thats one axis, then you can write the second axis starting at 0x66E5.

4 hours ago, Jacob Wiqvist said:

3: When bothe of the offsets read a value over 15000 I want another offset to change from 0 to 1.

Which offset? If you want them consecutive, the next 'free\ one is 0x66E9, so why not use that?

So, assuming your two axis values are written to 0x66E1 and and 0x66E5, and the flag we are using is in 0x66E9, try

Quote

function myFunction(offset, val1)

    -- read 1st offset
  if offset == 0x66E1 then
     val2 =  readSD (0x66E5)
  else if offset == 0x66E5 then
    val2 =  readSD (0x66E1)
  end

   if val1 > 15000 and val2 > 15000 then
        ipc.writeUB (0x66E9, 1)
  else
        ipc.writeUB (0x66E9, 0)
  end

end

 

event.offset(0x66E1, "SD", "myFunction")

event.offset(0x66E5, "SD", "myFunction")

 

Later...I see Pete also responded at the same time!

Link to comment
Share on other sites

2 hours ago, John Dowson said:

But that is one offset. What is the second offset you are using? And always specify the size of the value you are writing to that offset. If you are writing an axis value of as a dword, then it will take 4 bytes. If thats one axis, then you can write the second axis starting at 0x66E5.

I only tested with one offset and one pedal to see if I could get that working.

 

What in the code is specifying the size of the value? UB, SW and so on?

 

Any way so the code you sent will send a 0 or 1 to 0x66E9 depending on if 0x66E1 and 0x66E5 is above or below 15000 on the input axis.

So -16383 to 14999 on the input axis will give 0 on 0x66E9.

And 15000 to 16383 will give 1 on the 0x66E9 Offset?

And it will do this in real time?

I mean that it will continue to be 1 or 0 as long as the value is over 15000?

Regards Jacob!

 

 

Link to comment
Share on other sites

4 hours ago, Jacob Wiqvist said:

Any way so the code you sent will send a 0 or 1 to 0x66E9 depending on if 0x66E1 and 0x66E5 is above or below 15000 on the input axis.

So -16383 to 14999 on the input axis will give 0 on 0x66E9.

And 15000 to 16383 will give 1 on the 0x66E9 Offset?

And it will do this in real time?

I mean that it will continue to be 1 or 0 as long as the value is over 15000?

You can set what you want on any condition you want. I have provided you with a simple script that you should be able to use and update, depending on the type and size of the offsets you are using. First, determine the types you need - as Pete says, probably a SW or signed word, rather than a double word that you originally set you were using to write to the offset. Update that small test script I provided accordingly, so you are reading the same type as you are writing, and try it.
Add log statements to see the values if needed.

Please try things for yourself rather than asking such questions all the time. We have given you the information you need to do what you want to achieve. Take time to digest and understand and try things out for yourself.

Link to comment
Share on other sites

11 hours ago, John Dowson said:

If you are writing a double word (4 bytes) to 0x66E1. then that value is held in 4 bytes from that offset, so in 0x66E1-0x66E4 inclusive.
However, you are overwriting that value here: ipc.writeUB (0x66E2, 1).
Try using 0x66E5 instead.

Also, is the value signed or unsigned? This is important when reading. I assume signed below - change to unsigned if needed.

Using event.timer was also just an example to get you going. Its probably more efficient to use event.offset, and you can use the same handling function for multiple offsets.

But that is one offset. What is the second offset you are using? And always specify the size of the value you are writing to that offset. If you are writing an axis value of as a dword, then it will take 4 bytes. If thats one axis, then you can write the second axis starting at 0x66E5.

Which offset? If you want them consecutive, the next 'free\ one is 0x66E9, so why not use that?

So, assuming your two axis values are written to 0x66E1 and and 0x66E5, and the flag we are using is in 0x66E9, try

 

Later...I see Pete also responded at the same time!

I changed a few things an now it works fairly good.

function myFunction(offset, val2)

    -- read 1st offset
  if offset == 0x66E1 then
     val1 = ipc.readSD(0x66F5)
   elseif offset == 0x66F5 then
     val2 = ipc.readSD(0x66E1)
  end

   if val1 > 14000 and val2 > 14000 then
        ipc.writeUB(0x66E9, 1)
  else
        ipc.writeUB(0x66E9, 0)
  end

end

event.offset(0x66E1, "SD", "myFunction")

event.offset(0x66F5, "SD", "myFunction")

Link to comment
Share on other sites

10 hours ago, Jacob Wiqvist said:

I changed a few things an now it works fairly good.

function myFunction(offset, val2)

    -- read 1st offset
  if offset == 0x66E1 then
     val1 = ipc.readSD(0x66F5)
   elseif offset == 0x66F5 then
     val2 = ipc.readSD(0x66E1)
  end

   if val1 > 14000 and val2 > 14000 then
        ipc.writeUB(0x66E9, 1)
  else
        ipc.writeUB(0x66E9, 0)
  end

end

But that won't work correctly as you are not setting a value for val1 when the function is fired for a change in offset 0x66F5 (and you are overwriting the input value.

To simplify, you could just read each offset directly and not use the input parameter if this is confusing you:

Quote

function myFunction(offset, value)

    -- read 1st offset
     val1 = ipc.readSD(0x66F5)
    -- read 2nd offset
     val2 = ipc.readSD(0x66E1)
 

   if val1 > 14000 and val2 > 14000 then
        ipc.writeUB(0x66E9, 1)
  else
        ipc.writeUB(0x66E9, 0)
  end

end


And why change from 0x66E5 to 0x66F5? Ots a good idea to group the user offsets you use consecutively, but  its not a functional issue.

Link to comment
Share on other sites

50 minutes ago, John Dowson said:

And why change from 0x66E5 to 0x66F5? Ots a good idea to group the user offsets you use consecutively, but  its not a functional issue.

Something ells is writing to 0x66E5, but I have no Ide what. It may be something inside ProSim, have to check. But that offset did not work.

I will test the latest script you sent. Much easier for me to understand. Thank you.

I would also like to get into the script that GS must be below 20 knots to be able to activate 0x66E9 to 1, how can I do that?

I read this:

02B4    4    GS: Ground Speed, as 65536*metres/sec. Not updated in Slew mode!

https://forum.simflight.com/topic/5924-fsuipc-need-help-with-ground-speed-offset-02b4/

65536*10,29 =674365,44 about= 674365

or should I devide it?

 

can I write like this:

function myFunction(offset, value)

    -- read 1st offset
     val1 = ipc.readSD(0x66F5)
    -- read 2nd offset
     val2 = ipc.readSD(0x66E1)
    -- read 3ed offset
     val3 = ipc.readSD(0x02B4)

   if val1 > 14000 and val2 > 14000 and val3 < 674365 then
        ipc.writeUB(0x66E9, 1)
  else
        ipc.writeUB(0x66E9, 0)
  end

end

 

Best regards!

 

 

 

Link to comment
Share on other sites

1 hour ago, Jacob Wiqvist said:

or should I devide it?

 

yes:

Quote

function myFunction(offset, value)

    -- read 1st axis offset
     val1 = ipc.readSD(0x66F5)
    -- read 2nd axis offset
     val2 = ipc.readSD(0x66E1)
    -- read Ground Speed offset
     groundSpeed = ipc.readSD(0x02B4)
     -- convert groundSpeed to Knots
     groundSpeed = (groundSpeed / 65536) * 1.94384

   if val1 > 14000 and val2 > 14000 and groundSpeed < 20 then
        ipc.writeUB(0x66E9, 1)
   else
        ipc.writeUB(0x66E9, 0)
   end

end

 

Link to comment
Share on other sites

10 minutes ago, Jacob Wiqvist said:

The code disregard the and function, between 66E1 and 66F5. 

The strange thing is that the And function between 66F5 and Speed work?

What does this mean? What is an 'And function'?

If a lua script doesn't work, activate logging for lua plugins and try and determine what the problem is from the log output.

 

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.