mustangsim Posted November 17, 2012 Report Posted November 17, 2012 I have an Lvar that needs to be triggered via a single push button and do the following, If it's a 0 then when I push the button it needs to change to a 1, and if it's 1 and I push the button it needs to change to 0. It is the ComPush button on the G1000 and it switches the radio from com1 to com2 and vice versa for tuning with one push. The Lvar is L:G1000ComPushPFD I am using my button mapped in fsuipc as a keysend. Can a LUA do this, check the current status of the Lvar and toggle it with every push of my hardware button that is sending a key press? Hope that all makes sense. Thanks, Rob
Pete Dowson Posted November 17, 2012 Report Posted November 17, 2012 I am using my button mapped in fsuipc as a keysend. Can a LUA do this, check the current status of the Lvar and toggle it with every push of my hardware button that is sending a key press? Yes, Lua can do the actions you require. But I think you must be confused about some things. "Keysend" is a way of sending a key number (0-255) to a Client PC over the Network using WideFS. The Keysend number is then translated into an action on the Client PC via parameters in the WideClient.INI file. Since local panel variables (LVars) cannot be useful outside of FS and its panels, I can't see why you are considering "KeySend" at all. You write your small Lua plug-in and simply assign your button to that plug-in. eg name the plug-in ComSwitch.lua and assign the button to "Lua ComSwitch" in the drop-down. If you save the plug-in whilst FS is running you'll need to press the "reload" button on the Buttons & Switches tab to get the addition listed, first. If you check the Lua library documentation you will see you can read LVars by using the ipc.readLvar function and write them using the ipc.writeLvar function. Regards Pete
mustangsim Posted November 17, 2012 Author Report Posted November 17, 2012 ipc.readLvar("L:G1000ComPushPFD") if (ipc.readLvar("L:G1000ComPushPFD") == 0) ipc.writeLvar("L:G1000ComPushPFD",1) else if (ipc.readLvar("L:G1000ComPushPFD") == 1) ipc.writeLvar("L:G1000ComPushPFD",0) endend [/sub][sub] [/sub][sub] [/sub][sub][/CODE][/sub][sub]That is my code i tried which runs when I hit shift+tab+c on my keyboard I have that keyprees assigned to run the above LUA named ComPushed It doesn't work and i'm not sure why.[/sub][sub] ipc.writeLvar("L:G1000ComPushPFD",1) or ipc.writeLvar("L:G1000ComPushPFD",0) by themselves in the lua work fine. This is tied to a push button in my sim on the G1000 hardware that generates a keystroke input to fsuipc.[/sub][sub]Thanks,[/sub][sub]Rob[/sub]
Pete Dowson Posted November 18, 2012 Report Posted November 18, 2012 That is my code i tried which runs when I hit shift+tab+c on my keyboard I have that keyprees assigned to run the above LUA named ComPushed It doesn't work and i'm not sure why. If you looked at the FSUIPC log file you would see why. The Lua code is not valid. The "if" statement takes the form if <condition> then You missed the "then"s off! When trying to do these things it is always a good idea to look at other examples, and plenty are provided in the ZIP file installed in your FSUIPC documents folder. There are also many examples in the User Contributions folder. Furthermore, FSUIPC will log any errors in trying to execute such things, and you can even turn on Debug logging in the FSUIPC logging options. Note also that even with the "then"s in place, your code is very inefficient: You are reading the LVar at least twice, and in the worst case three times. Why? Here, I've added comments. See? ipc.readLvar("L:G1000ComPushPFD") -- FIRST READ, result discarded!!!if (ipc.readLvar("L:G1000ComPushPFD") == 0) -- SECOND READipc.writeLvar("L:G1000ComPushPFD",1)elseif (ipc.readLvar("L:G1000ComPushPFD") == 1) -- If it was Zero, THIRD READ!ipc.writeLvar("L:G1000ComPushPFD",0)endend[/CODE]And you have a spare "end" which is another error.Look at this:[CODE]X = ipc.readLvar("L:G1000ComPushPFD"[b])[/b]if X == 0 then ipc.writeLvar("L:G1000ComPushPFD",1)else ipc.writeLvar("L:G1000ComPushPFD",0)end[/CODE]Isn't that more logical? Or even this more compact version, which assumes L:G1000ComPushPFD is definitely always either 0 or 1:[CODE]ipc.writeLvar("L:G1000ComPushPFD", 1 - ipc.readLvar("L:G1000ComPushPFD"))[/CODE]That one-line version writes 1 - the current value, so 1-1 = 0 or 1 - 0 = 1. Right?Please check the Log for errors each time you get an unexpected result!RegardsPete
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