Solved! How To Use Dual Controls In X-Plane 11

* NOTE: I updated this post on 1/16/17 to clarify how you find the initial assignments of axes and buttons.

The one lingering barrier to my complete switch to X-Plane was the inability to use both sets of the Basement Sim’s controls. It seems that in X-Plane if roll/pitch/yaw are assigned to more than one controller, only one will work and X-Plane disregards the other. (This is not the case with FSX or P3D, which simply read whichever control provides the most recent or largest input.) Only one set of working controls is a real problem for me, as being able to fly with family, friends, etc. is a big part of what I enjoy about the Basement Sim.

There is an answer, though, and it turns out it is Lua. From the Lua website:

Lua is a powerful and fast programming language that is easy to learn and use and to embed into your application. Lua is designed to be a lightweight embeddable scripting language and is used for all sorts of applications from games to web applications and image processing.

 

Lots of simmers and cockpit builders use Lua to write little bits of computer code, called “scripts,” that allow them to execute unique commands (like having the buttons they installed in their panel turn on the cabin lights). Lua scripts are like little apps you can run to get things done that the sim software won’t do itself.

X-Plane is very easy to integrate with Lua thanks to the FlyWithLua plugin. I had posted on the X-Plane.org forums that I was hoping the final release version of X-Plane 11 would allow multiple sets of controls, and one of the very helpful folks in that community — Teddii — responded that a Lua script might be the answer. He said I should be able to write a script for one of the buttons on my yoke where if I flip the button to the left the left controls are live, and if I flip it to the right the right controls are live. He even volunteered possible code.

Getting the code to work meant finding out which axis numbers X-Plane was associating with each of the yokes. Fortunately every time you launch X-Plane FlyWithLua puts a little .TXT file called “initial_assignments.txt” in its plugin folder that shows initial joystick and button assignments. To find my axes and button assignment numbers I first went into X-Plane’s joystick configuration screen and assigned the axes of BOTH yokes to pitch and yaw. This gives you a warning that you have dual assignments in X-Plane, but that’s OK. I also assigned the button that I wanted to use to pass control of the airplane to a command I would recognize (in my case, calling ATC). Looking at the initial_assignments.txt file I could see the pitch, roll, and yaw axes for both yokes and the button numbers for the left / right switch on my Yoko yoke (which X-Plane reads as two buttons – one for each position – rather than as one, and I had assigned BOTH to call ATC in the joystick configuration screen). You will see commands like this in the initial_assignments.txt file:

set_axis_assignment( 0, "roll", "normal" )
set_axis_assignment( 1, "pitch", "normal" )
set_axis_assignment( 3, "yaw", "normal" )

… and …

set_button_assignment( (4*40) + 1, "sim/operation/contact_atc" )

You need to do a little math for the button assignment: (4*40)+1, for example, is 161. That’s the button number for the MyAirplaneYourAirplane code. You may also need to guess which set of axis assignments are the left and right controls, but that’s easy to change if you get it wrong.

I added these variables to Teddii’s initial code, put the “My Airplane Your Airplane.txt” script file in the FlyWithLua scripts folder, and gave it a go. Partial success: it worked in swapping to the right controls, but would not swap them back to the left. Looking at the joystick configuration window in X-Plane I could see that I again had conflicting yaw / pitch / roll controls, and deduced that while the swap from left to right worked, the script did not clear the assignments for the left yoke, resulting in the same problem I had started with (dual-assigned controls).

So I just modified the script a little bit, adding lines to assign the axes that I’m switching away from to “none.” SUCCESS! It worked, and now I’m able to easily pass control of the airplane to a passenger by flipping the yoke switch to the right, and take it back by flipping it back to the left.

The final code follows, and you are welcome to use it if you like. Thanks again for Teddii for his help as I would not have been able to do this without his initial and very helpful code. Also, there is a nice primer on getting started with FlyWithLua here.


 

My Airplane / Your Airplane FlyWithLua Script

-- axis numbers for left yoke
L_axisPitch = 75
L_axisRoll  = 76
L_axisPedal = 52

-- axis numbers for right yoke
R_axisPitch = 25
R_axisRoll  = 26
R_axisPedal = 2

function yoke_switch()
    if button (486) and not last_button(486)
    then
        -- activate right yoke
        set_axis_assignment(R_axisPitch, "pitch", "normal" ) --"normal" or "reverse"
        set_axis_assignment(R_axisRoll,  "roll", "normal" )
        set_axis_assignment(R_axisPedal, "yaw",  "normal" )
        -- DEactivate left yoke
        set_axis_assignment(L_axisPitch, "none", "normal" ) --"normal" or "reverse"
        set_axis_assignment(L_axisRoll,  "none", "normal" )
        set_axis_assignment(L_axisPedal, "none", "normal" )
    end
    if button (480) and not last_button(480)
    then
        -- activate left yoke
        set_axis_assignment(L_axisPitch, "pitch", "normal" ) --"normal" or "reverse"
       set_axis_assignment(L_axisRoll,  "roll", "normal" )
       set_axis_assignment(L_axisPedal, "yaw",  "normal" )
       -- DEactivate right yoke
       set_axis_assignment(R_axisPitch, "none", "normal" ) --"normal" or "reverse"
       set_axis_assignment(R_axisRoll,  "none", "normal" )
       set_axis_assignment(R_axisPedal, "none", "normal" )
    end
end

-- check for the switch button every frame ...
do_every_frame("yoke_switch()")

-- end

40 thoughts on “Solved! How To Use Dual Controls In X-Plane 11

    1. Hi, I have 2 Saitek yokes and 2 set of rudder pedals and a throttle quadrant. After much research and not being able to write code I had a simple idea. I ordered a USB hub from Amazon with 6 USB slots that can be turned off and on. Pulled yokes and pedals and throttle. So flying with someone and the other takes the controls ” my plane” and hit the USB controller switch turn my yoke and pedals off turn the others on. Works great.

  1. good day
    I try to sort out this dual controls on xplane

    got 2 yokes and 2 pedals – Saitek
    I installed lua in plugin folder and myplaneyourplane.txt into lua scripts subfolder.

    when I start xplane get an error written in green on top right of screen – and red message lua stopped
    this look like resoled if I restart lua

    how do you assign the flip / switch command to the yoke buttom ? to switch between the two ?
    tx
    ray

  2. good day
    ok installed lua and script

    running 2 Saitek Cessna yoke and 2 saitek pedals

    how do I assign the switch button – to switch between the two controls ?
    tx
    ray

    1. As I wrote in my post, you need to modify the script to have the correct axes numbers and button number for your configuration on X-Plane. I describe how to do this in the post.

  3. as we are in plugin theme
    i installed on the ipad the Simionic Simulator for Garmin G1000
    it is working fine in windows 7 and xp11 first release beta

    on the cockpit pc use windows 10 can not click on on the plugin ‘start button’ to start the server- it is basically unclickable – button is stubborn.

    did you come across this problem ?
    thanks
    ray

  4. Good day
    When starting xplane11 can not see in assignments any function yoke_switch and neither i can not see it in the initialassigments.txt. Because of that do not know the number of the button to type into the script
    How do you find out the button number?
    After you start xplane do you have to configure anything?
    Thanks
    Ray

    1. I went into X-Plane and assigned the button to a specific function (like calling up the ATC window). I then exited X-Plane and looked at the “initial_assignments.txt” file in the FlyWithLua plugin folder. Note the button number assigned to the command you assigned to that button, and that’s the button number for the code. Note that you need to do a little math to get the correct number.

      Here’s an example:

      set_button_assignment( (4*40) + 6, “FlyWithLua/autopilot/activate_autopilot” )

      That’s for having the autopilot on command set to the button. If you calculate (4*40)+6, you get 166, which is the button number to assign in the my airplane your airplane code. It doesn’t matter which command you assign to the button – you’re just doing it to identify which button code it is. Once I had the number I went back into the joystick config in X-Plane and deleted the assignment.

      1. know im using arduino boards to controls my custom cockpit!!! and they send data to xplane via ardsim plugin!! and i dont know how to make xplane 11 and your excellent script! to read my jokes 🙁

        but really good work!!

  5. Thank you very much for posting these tips..
    While I ‘still’ don’t have a dual yoke cockpit in the garage, I do have a TM Warthog for helicopters and a Saitek yoke for GA.
    Why LR don’t offer this kind of dual programming as standard is beyond me but I’m sure they have reasons.
    Thanks again for helping me sort out my controls.
    Toby

  6. starting from your script i made mine with my axis and button number, but it keep returning me an error when loading the script.. The error is “then” expected near ” on the line 13, so on the first then… Do you have any idea or advise please?

      1. Thanks it works now, but i don’t know why, when i run x-plane with that script if during a flight i open the x-plane main setting windows it will crash… I have X-Plane v11.05 and Flyng with Lua v2.6.3, what your version, please?

  7. Am I the only one that thinks this is a HUGE FAIL on XPlanes part. I mean…WHY does FSX (a 12plus year old program) allow for multiple inputs and a brand new simulator program does not!? I am NOT a programmer, and even doing this “SIMPLE” script has not worked for me….

    I guess, I am just going to have to keep using FSX. Its a shame, because the visuals line up so much better with xplane….I am using our home cockpi to help train my Son. You cannot have a positive training experience with one set of controls. AND switching using a button, is not the same either…..So dissapointed in Laminar for this…. 🙁

      1. Hey, I am trying to find a solution for having two Yokes work in the FSX. Did I get your comments right, that this is no problem in FSX? Or do I have to try to copy this Script in FSX?

        Thanks a lot!

  8. Thanks, working but… there is no other way to detect the change button than “on every frame”? The lua manual say that this function is not recomended, since can slow down the simulator… anything like “do_on_keystroke” function?

  9. Hi OTG,

    I read this post some time back when I started my cockpit project, with the aim of implementing it.

    Last night, finally, the project reached the point where dual control is now going in (a young friend of mine wants to learn to sim fly so it went up the list of priorities!!)

    Initially I looked at applying your script, or a few others that attempt to allow dual control, but I’ve found a way that allows both controllers to send inputs without the need to switch and wanted to share it –

    Theres an application called vJoy, it installs a virtual joystick that appears in XP11 as an assignable controller. However configuration of this app is a pain. Then I found a program called UCR (Universal controller remapper) that uses vJoy and has a very easy to use menu that lets you choose eg Joystick 1, X axis and Joystick 2 X axis and assign BOTH to Virtual Joystick 1, X Axis. Etc etc. Tried it last night and it works a treat. Of course one would want to avoid inputs from both at the same time, but no need to switch!

    All the best,

    Rictus

    1. Rictus,
      I tried your method with vjoy and UCR, and got both yokes mapped and working just fine, then when I quit and came back in, they both stopped working. When I went back in to UCR, I could see my mapping and both yokes worked ok there, but not in x-plane. Did you see this, or know what I might be missing?

      Thanks

  10. i figured out the toe breaks I included my code:

    — axis numbers for left yoke
    L_axisPitch = 1
    L_axisRoll = 0
    L_axisPedal = 175
    L_axisLeftToeBreak = 176
    L_axisRightToeBreak = 177
    — axis numbers for right yoke
    R_axisPitch = 100
    R_axisRoll = 101
    R_axisPedal = 125
    R_axisLeftToeBreak = 126
    R_axisRightToeBreak = 127

    function yoke_switch()
    if button (336) and not last_button(336)
    then
    — activate right yoke
    set_axis_assignment(R_axisPitch, “pitch”, “normal” ) –“normal” or “reverse”
    set_axis_assignment(R_axisRoll, “roll”, “normal” )
    set_axis_assignment(R_axisPedal, “yaw”, “normal” )
    set_axis_assignment(R_axisLeftToeBreak, “left toe brake”, “reverse” )
    set_axis_assignment(R_axisRightToeBreak, “right toe brake”, “reverse” )
    — DEactivate left yoke
    set_axis_assignment(L_axisPitch, “none”, “normal” ) –“normal” or “reverse”
    set_axis_assignment(L_axisRoll, “none”, “normal” )
    set_axis_assignment(L_axisPedal, “none”, “normal” )
    set_axis_assignment(L_axisLeftToeBreak, “none”, “reverse” )
    set_axis_assignment(L_axisRightToeBreak, “none”, “reverse” )
    end
    if button (504) and not last_button(504)
    then
    — activate left yoke
    set_axis_assignment(L_axisPitch, “pitch”, “normal” ) –“normal” or “reverse”
    set_axis_assignment(L_axisRoll, “roll”, “normal” )
    set_axis_assignment(L_axisPedal, “yaw”, “normal” )
    set_axis_assignment(L_axisLeftToeBreak, “left toe brake”, “reverse” )
    set_axis_assignment(L_axisRightToeBreak, “right toe brake”, “reverse” )
    — DEactivate right yoke
    set_axis_assignment(R_axisPitch, “none”, “normal” ) –“normal” or “reverse”
    set_axis_assignment(R_axisRoll, “none”, “normal” )
    set_axis_assignment(R_axisPedal, “none”, “normal” )
    set_axis_assignment(R_axisLeftToeBreak, “none”, “reverse” )
    set_axis_assignment(R_axisRightToeBreak, “none”, “reverse” )
    end
    end

    — check for the switch button every frame …
    do_every_frame(“yoke_switch()”)

    — end

  11. Thanks for the article OTG.

    I use multiple control profiles with different planes. Will the Lua script make a permanent change to the current active profile so that when I start X-Plane again, my B-side controls are activated (e.g. if my A-side controls are assigned pitch and roll and I switch to B-side controls before I quit X-Plane)?

  12. Thanks OTG for the post!

    Just want to point out that you don’t need to assign a button for the .ini file in order to find out what button number you’ll have to deal with.

    FlyWithLua offers a macro that shows a little message on your screen with the desired button number.
    Just go to Plugins / FlyWithLua / FlyWithLua Macros / Show joystick button numbers.

    Then use the shown button number in the lua script, no math needed.
    That’s it :).

    Many happy landings!

Leave a Reply