0

I tried to impliment a toggle into the Left D-pad but it doesn't work

private void SerialTimer_Tick(object sender, EventArgs e)
    {
        bool PreviousCarHeadlights = false;

        if (controls.DPad.Left & !PreviousCarHeadlights)
        {
            CarHeadlightsCheckBox.Checked = !CarHeadlightsCheckBox.Checked;
            PreviousCarHeadlights = !PreviousCarHeadlights;
        }
    }

Please could someone look at this and help.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364

1 Answers1

1

Go through your code sequentially, the answer is quite obvious:

First, you set PreviousCarHeadlights to false, next you check whether controls.DPad.Left is pressed AND PreviousCarHeadlights is false (which it is, since you just set it to false).

In other words: your headlights will always toggle as long as your DPad is pressed regardless of its previous state, since if (!PreviousCarHeadlights) is a tautology (meaning, it will always resolve to true)

The answer given here: Toggle an input is all you'll ever need when dealing with toggle. Keeping track of your gamepad's previous state means that you'll always know which button was pressed previously. It is a lot better than keeping track of many bool for each button that can be toggled.

Community
  • 1
  • 1
Nolonar
  • 5,962
  • 3
  • 36
  • 55