P-38L Posted September 21 Report Posted September 21 Hello I am programming in LUA and I want to read or write the value of the variable (A:CIRCUIT CONNECTION ON:37) which belong to the analog KingAir. Inside the directory F:\SteamLibrary\Community\fsuipc-lvar-module I only have the "modules" directory and three files namely: FSUIPC_WASM.ini layout.json manifest.json In the LUA programming it tells me that I cannot read variables of type "A". What should I do or what am I missing to be able to achieve this? To find out if the LUA program I'm writing is working, I wrote the following line: ipc.keypressplus(76) which uses the letter "L" to turn the lights on or off. This works fine from my joystick Here is my code: local circuitState = ipc.readAvar("A:CIRCUIT CONNECTION ON:37", "Bool") ipc.display("Circuit Breaker 37: " .. circuitState) local state = ipc.readLvar("L:Circuit37", 1) if state == 0 then ipc.writeLvar("L:Circuit37", 1) else ipc.writeLvar("L:Circuit37", 0) end But it doesn't work Any help will be greatly appreciated.
John Dowson Posted September 21 Report Posted September 21 There is no such function as ipc.readAvar - and such a function is nit possuble. To read an a-type variable, also known as a simvar, there are two ways to do this: 1. Add the simvar to a free/spare FSYUPC offset and use the read/write offset lua functions in the ipc library. See the Advanced User guide on how to add sumvars to offsets. 2. Write the simvar value to an lvar and read the value from the lvar, e.g. ipc.execCalcCode("A:CIRCUIT CONNECTION ON:37 (>L:CIRCUIT_CONNECTION_ON_37)") -- write the simvar value to an lvar ipc.reloadWASM(); -- reload the WASM to receive the new lvar ipc.sleep(50); -- wait for the lvar to be received circuitState = ipc.readLvar("L:CIRCUIT_CONNECTION_ON_37") To write to a simvar, there are also two ways: 1, Add to an offset and write the value to the offset 2. Use calculator code, e.g. ipc.execCalcCode("5 (>A:CIRCUIT CONNECTION ON:37)") would set the simvar value to 5.
P-38L Posted September 21 Author Report Posted September 21 Thank you for your kind reply. My bad, I thought the variable was only (A:CIRCUIT CONNECTION ON:37), but looking closely I found that the variable has a very different presentation. Using the mouse when the Relay is pushed, these are the values it presents: And when the Relay is pulled, these are the values: I don't know how to change the values indicated here using LUA programming. I've been experimenting with other variables and they work fine. For example, using the programming you kindly shared with me, I was able to see the behavior of assigning new variables. In my FSUIPC7.ini file I have the following written: And without any problems I was able to read values that I myself wrote from the LUA programming. My experiment worked thanks to your help. My big problem is that I don't know how to solve the problem of the variable that appears as I indicated in the first two images. I appreciate your valuable help.
John Dowson Posted September 22 Report Posted September 22 15 hours ago, P-38L said: In my FSUIPC7.ini file I have the following written: And without any problems I was able to read values that I myself wrote from the LUA programming. My experiment worked thanks to your help. Ok, but probably better to add the simvar to an offset and use that. It seems a bit convoluted to write the simvar to an lvar and then add the lvar to an offset, and you need to keep (manually) updating the lvar to keep in sync with the simvar. 15 hours ago, P-38L said: My big problem is that I don't know how to solve the problem of the variable that appears as I indicated in the first two images. I appreciate your valuable help. Sorry but I can't really help with this - I am no expert in complex calculator code, and looking at the existing presets for the King Air, this seems like quite a complex aircraft to control in this way. For assistance with calculator code (and with help defining presets), you should ask on the MFSF2020 channel on the MobiFlight Discord server. Maybe a good idea to tag the author of the existing presets for the King Air (Stefan Kelley).
P-38L Posted September 22 Author Report Posted September 22 Dear Mr. Dowson, I am very grateful to you for taking the time to help me with this task. I will continue to investigate and do trial-and-error tests. If I manage to figure it out, I will share it. Thank you very much.
John Dowson Posted September 23 Report Posted September 23 Ok, no problem. Looking at those screenshots, those seem to be from the behaviors tab - I don't know how to interpret that (I am not a gauge programmer!). Maybe try looking at the code in the Input Events tab instead, if there is any.... See this article in how to discover input events via the behaviors tools: https://www.badcasserole.com/uncovering-input-events-using-the-msfs2020-model-behavior-dialog/
John Dowson Posted September 23 Report Posted September 23 (edited) I see you have posted in the MSFS devsupport forums on this issue. Sorry, but your post there doesn't make much sense... Quote FSUIPC7 only has a few available. Not sure what you mean by this... FSUIPC makes available what is available in the aircraft, nothing more and nothing less... Quote And this plane only has the Avionics Relay available in L:var. If it is already in an lvar, then just use that lvar... Quote I’m interested in being able to convert the landing gear Relay which has the following structure to L:var: You cannot convert code to an lvar, you can only set a value to an lvar...i,e, you can store the result of executing the calc. code in an lvar Quote This is presented when the Relay is pushed: 1 Load int constant 20 2 Load int constant 13 3 (>A: BUS LOOKUP INDEX:1) (13) 4 (A:CIRCUIT CONNECTION ON:37) 5 => Loads value 1. 6 Load int constant 100 7 * (1., 100) 8 - (20, 100) But that doesn't tell you what is happening with the resulting value... if you want to store the resulting value from that code in an lvar, execute the code and save the value to an lvar. e.g. this is the code 20 13 (>A: BUS LOOKUP INDEX, Number) (A:CIRCUIT CONNECTION ON:37, BOOL) 100 * - To execute that and save the result in an lvar, in lua, you would do: ipc.execCalcCode("20 13 (>A: BUS LOOKUP INDEX, Number) (A:CIRCUIT CONNECTION ON:37, BOOL) 100 * - (>L:My_Relay_Result)"); and then the result of executing that code will be available, for whatever you want to do with it, in the new lvar My_Relay_Result. The pull is executing the exact same code - but you see a different value (0 instead of 1) as the value of the simvar A:CIRCUIT CONNECTION ON:37 is different. John P.S. What exactly are you trying to achieve here? Your initial question: Quote want to read or write the value of the variable (A:CIRCUIT CONNECTION ON:37) which belong to the analog KingAir. is straightforward - you add that simvar to a free/spare FSUIPC offset, and then you can read the value from the offset, and update the simvar by writing to the offset. Edited September 23 by John Dowson PS added
P-38L Posted September 25 Author Report Posted September 25 Thank you for your kind reply. The process you indicate I have written in LUA and it actually works, but the variable (>L:My_Relay_Result) only shows its value when I execute the joystick action assigned to that LUA command. For example, if the Relay "jumps" automatically for some reason due to a failure in the plane, the variable (>L:My_Relay_Result) does not show any change in value unless I press the Joystick button assigned to the joystick, and this is when the value is updated. Is there any way for the variable to show its value automatically when it senses a change in the plane, without me having to press any button for its value to change?
John Dowson Posted September 25 Report Posted September 25 3 hours ago, P-38L said: Is there any way for the variable to show its value automatically when it senses a change in the plane, without me having to press any button for its value to change? The value returned by executing that calc code only changes when the value held in the simvar CIRCUIT CONNECTION ON:37 changes, so you need to run that calc code to update the lvar value when that simvar changes value, The easiest way to do this is to add that simvar to an FSUIPC offset, viai the myOffsets.txt file - see the Advanced User guide on how to do this but basically you just create that file and add the following lines (or append the line if the file already exists): // offset, size, simvar, type, units [, w] 0x66C0, 1, CIRCUIT CONNECTION ON:37, I32, Bool, w That will add the simvar CIRCUIT CONNECTION ON:37 to offset 0x66C0 (for read/write) as a 1 byte bool. You can then use a lua script (that must be running, usually via auto-start) to monitor that offset and update the lvar when the value changes, i.e.: function updateMyLvar(offsetm value) ipc.execCalcCode("20 13 (>A: BUS LOOKUP INDEX, Number) (A:CIRCUIT CONNECTION ON:37, BOOL) 100 * - (>L:My_Relay_Result)"); end event.offset(0x66C0, "UB", "updateMyLvar") John
P-38L Posted September 25 Author Report Posted September 25 Inside the FSUIPC7.ini program I have written: And in the same folder of the FSUIPC7 I have a file "Ejecutar.lua" But the value written in 66C0 doesn't change at all. I don't know if I'm misinterpreting the variables. To make sure it was working, I did a test with an L type variable, and in this case, it does work. Here the value written to 66C0 is always "looking" at the value of the variable. Honestly, I don't know what to do.
John Dowson Posted September 26 Report Posted September 26 9 hours ago, P-38L said: And in the same folder of the FSUIPC7 I have a file "Ejecutar.lua" But the value written in 66C0 doesn't change at all. I don't know if I'm misinterpreting the variables. That is never going to work - ipc.execCalcCode does NOT return any value (it cannit as its asyncronous). As I have said, to get the value returned by executing calculator code, you have to write the value to an lvar and read the value from the lvar. But that calc code string returns one of two values, -80 and 20, depending on the value held by A:CIRCUIT CONNECTION ON:37 (which is always 0 or 1). so if you know the value of that variable, you know the value that executing that calc code string will return. So why don't you just add that simvar to an fsuipc offset, as I showed in my previous comment? If you really want the value of that calc code string in an offset, then change the calc code string to send the value to an lvar, then either have an event.lvar function that picks-up any change to the lvar value and write it to an offset, or add the lvar to an offset via the [LvarOffsets] ini file section. 9 hours ago, P-38L said: To make sure it was working, I did a test with an L type variable, and in this case, it does work. Here the value written to 66C0 is always "looking" at the value of the variable. That will work as ipc.readLvar will return a value - lvar values are held in FSUIPC and so this is a synchronous call. Please use the lua library documentation - you will see that there is no return code/parameter/value from ipc.execCalcCode. John
P-38L Posted September 26 Author Report Posted September 26 A little progress. I wrote the following line of code based on what you shared with me. Due to the Relay's behavior, the values are 20 and -80 according to the table. And the value that it gives me at address 66C0 is indeed the number 20, but even if the Relay is pushed or pulled, the value will always be 20. Even if the Relay is Pulled and I restart the FSUIPC7, the value will always be 20. It does not show me the -80 If I write the following in the myOffests.txt file, the value will always be 0, that's why I had to remove this line of code.
John Dowson Posted September 26 Report Posted September 26 First, you are now using A:CIRCUIT CONNECTION ON:137 now when it should be A:CIRCUIT CONNECTION ON:37 - so try with the correct index number. 5 minutes ago, P-38L said: I wrote the following line of code based on what you shared with me. Due to the Relay's behavior, the values are 20 and -80 according to the table. And the value that it gives me at address 66C0 is indeed the number 20, but even if the Relay is pushed or pulled, the value will always be 20. Yes, that is because nothing is changing the value in offset 0x66C0. You write the value of am lvar to that offset, although the lvar will be undefined at this point, so you will be writing a nil value. The offset is then never updated. 10 minutes ago, P-38L said: If I write the following in the myOffests.txt file, the value will always be 0, that's why I had to remove this line of code. That is because A:CIRCUIT CONNECTION ON:137 doesn't exist. You should ALWAYS check your log file for any errors when adding to offsets, and you can also use logging to debug your lua code. Correct that index number in your myOffsets.txt file and then log the offset value (using FSUIPC's offset logging facilities) to verify that it is changing when you change the relay. Once that is done, try the lua I already suggested if you want the value of executing the calc. code to be stored in an lvar. Here is an improved version:: -- Offset event handling function: -- here we execute the calculator code and store the value in our Lvar -- Note the offset should hold the value of the simvar CIRCUIT CONNECTION ON:37 -- which should be added via the myOffsets.txt file function updateMyLvar(offsetm value) ipc.execCalcCode("20 13 (>A: BUS LOOKUP INDEX, Number) (A:CIRCUIT CONNECTION ON:37, BOOL) 100 * - (>L:My_Relay_Result)"); end -- Lvar event function: -- here we will just log the lvars value when it changes function lvarUpdated(varname, value, userParameter) ipc.log("Lvar updated: L:My_Relay_Result=" .. value) end -- First, lets create our lvar with an initial value of 0 ipc.createLvar("L:My_Relay_Result", 0) -- Now wait until FSUIPC has received this lvar so it can be used for an event -- ipc.reloadWASM() -- this shouldn't be needed while ipc.getLvarId("L:My_Relay_Result") == nil do ipc.sleep(20) -- wait for 20ms end -- wait for an event in the offset holding the simvar, which will get triggered when the simvar/offset changes value event.offset(0x66C0, "UB", "updateMyLvar") -- Monitor the lvar value to log any changes event.Lvar("L:My_Relay_Result", 100, "lvarUpdated") ipc.log("Lua script to update Relay lvar My_Relay_Result is now running") Also, it would be easier for me if you could just attach lua/log files or paste code (as I do) rather than use images.
P-38L Posted September 27 Author Report Posted September 27 Hello Mr. Dowson I finally did it... in a different way, but I managed to do it. I'm going to share with you the method I did. I used MobiFlight and assigned an action to an axis of my joystick (Saitek X52 Pro) that is always generating "electronic noise", in this case it is the Axis Slider 1 and I wrote the following code: 20 13 (>A:BUS LOOKUP INDEX,number) (A:CIRCUIT CONNECTION ON:137, Bool) 100 * - (>L:CIRCUIT_CONNECTION_ON_37) I am using (A:CIRCUIT CONNECTION ON:137) instead of 37, because the command line indicates this number. If I move the Relay with the mouse, the value automatically changes from 20 to -80, even if I do it with the button assignment on the joystick it also makes this change. So far so good... but unfortunately I don't know how to make MobiFlight always "watch" the value of a variable without having to assign it to any button or axis. Luckily, the Axis Slider 1 has a damaged potentiometer and generates that "electronic noise"; so, in this way, it is always "reading" and "writing" the values. I am very grateful for your valuable help, your dedication and time, and for your admirable patience. PD: If you know the trick I need in MobiFlight, I would really appreciate it. Regards Alejandro Gómez
John Dowson Posted September 27 Report Posted September 27 3 hours ago, P-38L said: I am using (A:CIRCUIT CONNECTION ON:137) instead of 37, because the command line indicates this number. Ah yes, I hadn't noticed that.... I don't understand why its 37 when executed: Quote 1 Load int constant 20 2 Load int constant 13 3 (>A: BUS LOOKUP INDEX:1) (13) 4 (A:CIRCUIT CONNECTION ON:37) 5 => Loads value 1. 6 Load int constant 100 7 * (1., 100) 8 - (20, 100) This is presented when the Relay is pulled: 1 Load int constant 20 2 Load int constant 13 3 (>A: BUS LOOKUP INDEX:1) (13) 4 (A:CIRCUIT CONNECTION ON:37) 5 => Loads value 0. 6 Load int constant 100 7 * (0., 100) 8 - (20, 0) 3 hours ago, P-38L said: PD: If you know the trick I need in MobiFlight, I would really appreciate it. No. I do not use or support MobiFlight. John
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now