But what happens when the user uses a key command to turn the autopilot off? Or uses a key command to turn the yaw damper on or off?
As they are most likely to do on an approach where the last thing they want to be doing is mousing about looking for a switch. Your L var and what is happening in the sim will become disconnected.
In this particular case I would try something like this in the Animation code:
Code:
<Animation>
<Parameter>
<Code>
(A:AUTOPILOT MASTER,bool)
if{ 50 }
els{
(A:AUTOPILOT YAW DAMPER,bool)
if{ 25 }
els{ 0 }
}
</Code>
<Lag>25</Lag>
</Parameter>
</Animation>
If the AP is on then it will animate to keyframe 50 and assume the Yaw damper is also on (see below for more on how to ensure the yaw damper is on).
If the AP is off it will check if the yaw damper is on or off and set the switch to either 25 or 0 depending on the situation.
If you want to ensure everything is in line and doing what it is supposed to do you can add a validation routine to an XML gauge to check if the AP is on and the Yaw Damper is off and if so turn the Yaw damper back on. Like:
(A:AUTOPILOT MASTER,bool) (A:AUTOPILOT YAW DAMPER,bool) ! and if{ (>K:YAW_DAMPER_ON) }
This will check 18 frames per second if you have it in a XML gauge. It is not very processor intensive and will only fire the YAW DAMPER ON if it is in a position it shouldn't be. Compared to using tens of thousands of trig functions to draw a bunch of polygons on screen every single frame it has miniscule effect.
You could throw the above line into the Animation Code if you really do not want to use a separate XML gauge but this would be a last resort option.
The mouse rect function could look something like this (borrowing Bill's original code he posted earlier)
Code:
<MouseRect>
<Cursor>Hand</Cursor>
<TooltipText>Fuel Select Left</TooltipText>
<MouseFlags>LeftSingle+RightSingle+Wheel</MouseFlags>
<CallbackCode>
(M:Event) 'LeftSingle' scmp 0 ==
(M:Event) 'WheelDown' scmp 0 == or
if{
(A:AUTOPILOT MASTER,bool)
if{ (>K:AP_MASTER) }
els{ (>K:YAW_DAMPER_OFF) }
}
(M:Event) 'RightSingle' scmp 0 ==
(M:Event) 'WheelUp' scmp 0 == or
if{
(A:AUTOPILOT YAW DAMPER,bool) !
if{ (>K:YAW_DAMPER_ON) }
els{
(A:AUTOPILOT MASTER,bool) !
if{ (>K:AP_MASTER) }
}
}
</CallbackCode>
</MouseRect>
Left click will check if the AP is ON, if so it will turn it off (and we assume our validation code will have already set the yaw damper to on). If the AP is already OFF then we can assume the yaw damper is either on or off. It makes no difference as if it is ON we just turn it off. If it is off we can still turn it off again without anything bad happening.
Right click checks the Yaw damper is OFF. remember, our validation routine will only allow the yaw damper to be off if the AP is off. If it is off then turn it on. Otherwise the yaw damper is ON. The AP can then be either on or off. If it is off then turn the AP on.
I like to use AP_MASTER toggle as I think (can't remember exactly) AUTOPILOT_OFF and AUTOPILOT_ON generate a contenterrorlogging report in P3D for some silly reason.
Using the above will keep your AP switch in sync with anything the user key commands as well as avoiding any L vars.
Disclaimer: This code has not been tested in sim, it may contain syntax (most likely) or logic errors (I hope not).