Jump to content
The simFlight Network Forums

How do you read the parameter value of a joy/button in a lua script?


Recommended Posts

I have been reading through the LUA library and other FSUIPC doc's to see if I can figure this out but so far it is eluding me.

If I enter a parameter value for example "2" on a button assignment in FSUIPC, let's say Joy 3 button 21 and use that button to launch a LUA script, how do I read that parameter in my lua script?

I am trying to write a common script to be used by several buttons and want to use if statements in the script to execute a variety of different actions based on the parameter value assigned to each button.

If someone can point me in the right direction that would be great.

Thanks!

Tom G.

Link to comment
Share on other sites

I would program the switches/buttons to send the parameter to one of the user-defined offsets (0x66C0 to 0x66FF)

and then use the Lua "event.offset(offset, "type", "function name") structure to launch your processing.

This passes the parameter that was placed in the offset to the processing function.

event.offset(0x66C0, "UB", My_Processing) would be the 'trigger' to launch the processing function when the data in the offset changed.

So function My_Processing(offset,value) would receive "0x66C0" as the passed offset and the parameter, in this case an unsigned byte,

as the value.

You can use multiple offsets to launch the same processing function and differentiate the source by looking at the passed offset

to determine 'who' triggered the event.

I use this feature to launch most of my Lua processing functions.

Another approach:

You could also send the parameter to an offset and trigger the processing using the event.button(joynum, button, "processing function")

You would then read the offset that you are using to receive the parameter using the ipc.readUB(offset) statement in your function.

The joy number and button number will be will be passed to your processing function as well so you can determine 'who' sent

the parameter to the offset.

Paul

Link to comment
Share on other sites

Paul,

Thanks! I'll give those ideas a test drive tonight.

I'm working on a script to interface my panels to a Carenado C90 and I have much of it working but the code is very slow since it all executes with each key press. Being able to detect a unique parameter from each of the buttons will streamline it quite a bit.

Tom G.

Link to comment
Share on other sites

Paul,

Thanks again for your help, I now have it working!!!

Tom G.

That's great, Tom. Glad I could help. Just keep those FSUIPC4 user documents handy! I refer to them

frequently when I am setting up my controllers for a new aircraft or tweaking the settings of 'old ones'. :)

Paul

Link to comment
Share on other sites

Paul,

If I might press you for just one more pointer. And, for this I have been reading the doc's and looking at other peoples scripts and I fear I'm just to thick headed for it to sink in. My question is with regard to the proper use of ipcparam and how to get FSUIPC to pass parameters to it, if in fact that is what it is for.

I've been looking at several sample scripts and reviewing them against the lua documentation in an attempt tp better educate myself. I'm seeing a number of scripts that appear to use ipcparam to collect some form of parameter. Am I correct about that? And, if so, what settings in FSUIPC would be used to get a button to pass a parameter to a script such that ipcparam could then read it?

Thanks!

Tom G.

Link to comment
Share on other sites

Tom, that is one feature I don't use and do not have a complete understanding of the methodology involved.

Gunter (guenseli) who is involved with the LINDA world uses that feature in his script implementations.

I THINK what is done is to assign that 'control' (ipcparam) to a joystick button or switch in the FSUIPC4

Buttons + Switches menu system just the same as you would assign any other FSX control. The assigned

parameter is then passed to the Lua script that uses the event.param("function-name") trigger instead of

say, the event.offset trigger.

In one of Gunter's scripts I have he looks at the passed parameter to determine which switch/button was activated

and then does whatever is dictated by that action.

Here is a partial list of one of his scripts:


-- A2A Spitfire accusim
-- LUA Script V 1.0
-- updated: Sunday, 24th Oct
--
-- (C) Guenter Steiner/guenseli
-- List of parameters
--
-- 1 Generator on
-- 2 Generator off
-- 3 Generator Toggle
--
-- 4 Primerstate open
-- 5 Primerstate closed
-- 6 Primerstate toggle
-- 7 Priming
-- 8 Primer releasing and priming
--
-- 9 open Lid and starter ON
-- 10 starter OFF and close Lid
--
-- 15 Boost cut out off
-- 16 Boost cut out on
-- 17 Boost cut out toggle
--
-- 20 Mixture ratio lever weak
-- 21 Mixture ratio lever rich
-- 22 Mixture ratio lever toggle
--
-- 23 Top (Left) Fuel Selector ON
-- 24 Top (Left) Fuel Selector OFF
-- 25 Top (Left) Fuel Selector toggle
--
-- 26 Bottom (right) Fuel Selector ON
-- 27 Bottom (right) Fuel Selector Off
-- 28 Bottom (right) Fuel Selector toggle
--
-- 30 Ventral recog light "Beacon" steady
-- 31 Ventral recog light "Beacon" OFF
-- 32 Ventral recog light "Beacon" morse
--
-- 33 dorsal recog light steady
-- 34 dorsal recog light OFF
-- 35 dorsal recog light morse
--
-- 36 morse key
--
-- 40 Landing light extending
-- 41 Landing light retracting
-- 42 Landing light ext/retr toggle
--
-- 43 Left Landing light ON
-- 44 Left Landing light OFF
-- 45 Right Landing light ON
-- 46 Right Landing light OFF
--
-- 50 Left Landing light extend and ON
-- 51 Left Landing light retract and OFF
-- 52 right Landing light extend and ON
-- 53 right Landing light retract and OFF
--
-- 55 LL Beam up
-- 56 LL Beam down
--
-- 60 Defroster inc
-- 61 Defroster dec
--
-- 70 Oxy Alt Valve set inc
-- 71 Oxy Alt Valve set dec
-------------------------------------------------------------- GEN
-- Generator ON
if ipcPARAM == 1 then

LVarSet = "L:Eng1_GeneratorSwitch"

ipc.writeLvar(LVarSet, 1)

end
-- Generator off
if ipcPARAM == 2 then

LVarSet = "L:Eng1_GeneratorSwitch"

ipc.writeLvar(LVarSet, 0)

end
-- Generator toggle
if ipcPARAM == 3 then

LVarSet = "L:Eng1_GeneratorSwitch"
val = 0
if ipc.readLvar(LVarSet) == 0 then
val = 1
end
ipc.writeLvar(LVarSet, val)

end
[/CODE]

It seems that each switch is assigned the same control, ipcparam, and a unique parameter to ID the function

of the switch as noted in his list of parameters vs functions.

I think the same results can be achieved with the method I noted earlier, using the event.offset to pass a

parameter to a given processing function in the lua script.

Here is a script I use for various actions for the A2A P-51D. Some functions interact with the aircraft code via L:Var's

while I use others to display data as a control is changed that may be interacting with the code directly via association with

normal FSX controls.

===============================================================

-- Script for the A2A Accu-Sim P-51D (Military)

-- Last modified on 09/21/2012 P.D. Strogen

function dispP51_CS(offs, val)

conset=val+1

ipc.setdisplay(650, 200, 500, 100)

if val == 0 then

ipc.display("INS LIGHTS ON _ BATTERY _ RECOG _ NAV LIGHTS _ LAND LIGHTS\nINS LIGHTS OFF _ AV MASTER _ GEN _ APU _ PANEL LIGHTS\nControl Set = "..conset, 10)

end

if val == 1 then

ipc.display("FUSELAGE _ LEFT MAIN _ RIGHT MAIN _ REL L DT _ REL R DT\nFUEL OFF _ LEFT AUX _ RIGHT AUX _ SPARE _ SPARE\nControl Set = "..conset, 10)

end

if val == 2 then

ipc.display("PRIME _ MAG INC _ START _ GUN SIGHT _ GUNS \nFUEL PUMP _ MAG DEC _ PITOT HEAT _ SIGHT GYRO _ SIGHT MODE\nControl Set = "..conset, 10)

end

if val == 3 then

ipc.display("GPS _ AP CHART _ TAGS _ WING LEVEL _ CANOPY\nRADAR _ MAP _ HEADPHONES _ SPARE _ OXYGEN\nControl Set = "..conset, 10)

end

if val == 4 then

ipc.display("COOLANT AUTO _ COOLANT OPEN _ OIL AUTO _ OIL OPEN _ AIL TRM RT\nCOOLANT MAN _ COOLANT CLOSE _ OIL MAN _ OIL CLOSE _ AIL TRM LT\nControl Set = "..conset, 10)

end

end

function dispFUEL_TANK(offs, dummy)

TANK =ipc.readLvar("L:FSelP51State")

if TANK == 2 then x = "R MAIN" end

if TANK == 1 then x = "L MAIN" end

if TANK == 0 then x = "FUSELAGE" end

if TANK == 4 then x = "R DROP" end

if TANK == 3 then x = "L DROP" end

ipc.display("FUEL TANK = "..x, 2)

-- ipc.display("L:FSelP51State = "..TANK, 2)

end

function dispFUEL_VALVE(offs, dummy)

STATE =ipc.readLvar("L:Eng1_FuelCutOffSwitch")

if STATE == 0 then y = "CUTOFF" end

if STATE == 1 then y = "CLOSED" end

ipc.display("FUEL VALVE = "..y, 2)

end

function Vent(offset, val)

Vent_Set =ipc.readLvar("L:CabinVent")

ipc.display("Vent = "..Vent_Set, 1)

end

function Headphones(control, on_off)

ipc.writeLvar("L:Headphones", on_off)

ipc.writeLvar("L:SystemCondSelectFSX", 57)

ipc.writeLvar("L:SystemCondValueFSX", on_off)

end

function Oxygen_Mask(control, Mask_on_off)

ipc.writeLvar("L:OxyMaskOn", Mask_on_off)

ipc.writeLvar("L:SystemCondValueFSX", Mask_on_off)

ipc.writeLvar("L:SystemCondSelectFSX", 58)

end

function GPU(control, GPU_on_off)

ipc.writeLvar("L:APUSwitch", GPU_on_off)

ipc.writeLvar("L:SystemCondSelectFSX", 53)

ipc.writeLvar("L:SystemCondValueFSX", GPU_on_off)

end

function Disp_El_Trim(control, Dummy)

Trim_in = ipc.readSW(0x0BC0)

if Trim_in > 0 then Trim_Out = (round(Trim_in/65.532))/10

else Trim_Out = (round(Trim_in/65.525))/10

end

ipc.display("El Trim = "..Trim_Out, 1)

end

function Disp_Rud_Trim(control,Dummy)

RTrim_in = ipc.readSW(0x0C04)

Rud_Trim = (round(RTrim_in/163.83))/10

ipc.display("Rud Trim = "..Rud_Trim, 1)

end

function Disp_Ail_Trim(control,Dummy)

ATrim_in = ipc.readSW(0x0C02)

Ail_Trim = (round(ATrim_in/163.83))/10

ipc.display("Ail Trim = "..Ail_Trim, 1)

end

-- rounds the integer

function round(num)

num = tonumber(num)

if num == nil then return 0 end

if num >= 0 then return math.floor(num+.5)

else return math.ceil(num-.5) end

end

event.offset(0x66C0,"UB","dispP51_CS")

event.offset(0x66C6,"UB","dispFUEL_TANK")

event.offset(0x66C5,"UB","dispFUEL_VALVE")

event.offset(0x66C4,"UB","Vent")

event.offset(0x66C7,"UB","Headphones")

event.offset(0x66C8,"UB","Oxygen_Mask")

event.offset(0x66C9,"UB","GPU")

event.control(65607,"Disp_El_Trim")

event.control(65615,"Disp_El_Trim")

event.offset(0x0C04,"SW","Disp_Rud_Trim")

event.control(66276,"Disp_Ail_Trim")

event.control(66277,"Disp_Ail_Trim")

===============================================

I also use one offset, 0x66CO, as a "Mode" indicator whereby I can have any number of "Modes" as the aircraft interface dictates.

I have dual Saitek throttles and use 2 switches of the 12 to change the mode up or down.

The other 10 switches have mode-dependent assignments.

In the case of the P-51D I have 5 modes. The "dispP51_CS(offs, val)" function in my script simply displays a message in the Lua display

for 10 seconds, showing me what controls are assigned to each switch for that mode. The actual assignments are made in the FSUIPC4.ini

file using the "Offset Conditions" of FSUIPC as show in the FSUIPC4 for Advanced Users.pdf...see pages 22 & 23.

This type of arrangement requires 'hand editing' of the ini file as the menu assignment feature can only assign 1 control to each button

when pressed and one when it is released.

Here is an example of my technique, showing 2 of my 5 mode settings for the P-51D.

[CODE]
!=3=//CONTROL SET 3
30=B66C0=2 PD,14,CM10:1,1 ;// PRIMER
31=B66C0=2 UD,14,CM10:1,0 ;// PRIMER RELEASE
32=B66C0=2 PD,18,CM10:2,1 ;// START
33=B66C0=2 UD,18,CM10:2,0 ;// START RELEASE
34=B66C0=2 PD,16,C65635,0 ;// MAG INC
35=B66C0=2 PD,17,C65634,0 ;// MAG DEC
36=B66C0=2 PD,15,C66237,0 ;// FUEL PUMP TOGGLE
37=B66C0=2 PD,19,C65858,0 ;// PITOT HEAT
38=B66C0=2 PA,0,CM10:16,2 ;// GUN SIGHT
39=B66C0=2 PA,1,CM10:18,0 ;// GUN SIGHT GYRO SWITCH
40=B66C0=2 PA,2,CM10:29,1 ;// FIRE GUNS
41=B66C0=2 UA,2,CM10:29,0 ;// CEASE FIRE
42=B66C0=2 PA,3,CM10:17,2 ;// GUN SIGHT MODE
!=4=//CONTROL SET 4
50=B66C0=3 PD,14,C65908,0 ;// GPS
51=B66C0=3 PD,15,C66506,440 ;// RADAR
52=B66C0=3 PD,16,C66506,14992 ;// AP CHART
53=B66C0=3 PD,17,C66506,14 ;// MAP
54=B66C0=3 PD,18,C66199,0 ;// TAGS
55=B66C0=3 PD,19,Cx0D0066C7,x01 ;//Headphones Toggle via Lua Event
56=B66C0=3 PA,0,C65727,0 ;// WING LEVEL
57=B66C0=3 PA,2,C66389,0 ;// CANOPY
58=B66C0=3 PA,3,Cx0D0066C8,x01 ;// Oxygen via Lua Event
[/CODE]

I use a mix of standard FSX controls "C65xxx", Macro file assignments (CM10:xx) and Lua event triggers (Cx0D0066C8,x01)

So, as you can see, there are 'many ways to skin a cat' :)

Hope this answers more questions than it creates!

Paul

Link to comment
Share on other sites

Thanks Very Much for the explanation and sample scripts!

I've got a long way to go on the lua learning curve, but you've certainly given me another push in the write direction. As of this morning I have many of the functions in my Carenado King Air C90 working thanks to your help. I'm still having trouble with a few they have that use timers, but thats not so much lua related as it is how Carenado has the application coded.

Again Thanks!

Tom G..

Link to comment
Share on other sites

Thanks Very Much for the explanation and sample scripts!

I've got a long way to go on the lua learning curve, but you've certainly given me another push in the write direction. As of this morning I have many of the functions in my Carenado King Air C90 working thanks to your help. I'm still having trouble with a few they have that use timers, but thats not so much lua related as it is how Carenado has the application coded.

Again Thanks!

Tom G..

I don't have that aircraft so I can't give you any specifics but you may want to use the FSUIPC4 control to "List Local Panel Variables" or

words to that effect. This will show you the internal L:Vars used in the code and if the coders used a naming convention that relates

to actual functions you may be able to do whatever you need done by acting on those L:Vars directly.

I assign that control temporarily to a button and activate it once I load the aircraft in question. The list goes out into the FSUIPC4.log file.

That is how I interface to the A2A Accusim aircraft in many instances since much of that simulation is 'outside' of the

'normal' FSX realm.

Paul

Link to comment
Share on other sites

  • 2 weeks later...

If I enter a parameter value for example "2" on a button assignment in FSUIPC, let's say Joy 3 button 21 and use that button to launch a LUA script, how do I read that parameter in my lua script?

The parameter is supplied in the variable named ipcPARAM, as documented. Not sure how you managed to miss that? The value is also set for a running Lua plug-in by the LuaValue control.

The other answers seem to be answering a different question altogether, one far more complex Is it me that's missing something?

Pete

Link to comment
Share on other sites

Pete,

I think in general I'm all set here. I was missing some obvious stuff. Sometimes the lua docs remind me of the phrase in my old college texts "The advances student will recognize..." That's not a reflection on the quality of the doc's, but more a recognition on my part that sometimes I'm a little thick headed.

And, yes there's more than my initial question about lua variables that found its way into this post, but I started a new post about the Carenado Timers to parse out that issue.

Thanks!

Tom G.

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.