0

I have a member thread for an object running that also contains a RawGameController.

The thread is in a while loop and should constantly update check the controller, but when I check the values nothing changes. I can confirm the controller is there in windows and the buttons work. (it's xbox 360 wired, but it needs to support many different controllers, so I can't use Gamepad object.)

 public class myController
    {
    public RawGameController rgController;
    public Thread Poller;


public void ControllerPoller()
    {
        bool[] buttonArray = new bool[rgController.ButtonCount];
        GameControllerSwitchPosition[] switchArray = new GameControllerSwitchPosition[rgController.SwitchCount];
        double[] axisArray = new double[rgController.AxisCount];

        bool[] oldButtonArray = new bool[rgController.ButtonCount];
        GameControllerSwitchPosition[] oldSwitchArray = new GameControllerSwitchPosition[rgController.SwitchCount];
        double[] oldAxisArray = new double[rgController.AxisCount];

        while (true)
        {
            //roughly polling 60hz so...
            using (var waitHandle = new ManualResetEventSlim(initialState: false))
            {
                waitHandle.Wait(TimeSpan.FromMilliseconds(1000 / 60));
            }
            rgController.GetCurrentReading(buttonArray, switchArray, axisArray);
            if (buttonArray.Equals(oldButtonArray) && switchArray.Equals(oldSwitchArray) && axisArray.Equals(oldAxisArray))
            {
                continue;
            }

I then deal with the input changes further on.

Any help would be great.

Viché
  • 23
  • 4
  • Do you mean when you used your Xbox360 controller to test the above code, GetCurrentReading() method did not respond, but another 3rd party controller could? – Faywang - MSFT Apr 30 '20 at 07:21

1 Answers1

0

Array.Equals is an identity check (are they referencing the same object). It will always be false for your arrays since they are different objects:

using System;

namespace Test
{
  public class Program
  {
    public static void Main(string[] args)
    {
      var nums1 = new int[] { 1, 2, 3 };
      var nums2 = new int[] { 1, 2, 3 };
      var nums1ref = nums1;

      Console.WriteLine(nums1.Equals(nums2));    // False
      Console.WriteLine(nums1.Equals(nums1ref)); // True
    }
  }
}

You can find out examples of how to check for array contents equality here.

Also, once you have the correct test for equality, you probably don't want to use && (and) but instead use || (or) in your checks -- you most likely care if any of the array contents have changed, not that all of them have changed at once.

Peter Torr - MSFT
  • 11,824
  • 3
  • 18
  • 51
  • Thanks for this. I found that the arrays were actually the same address in memory, so when a change was being picked up it would change both. Whilst this fixes my array problem (Thank you again.) it doesnt tell me why GetCurrentReading is not responding. Only for an Xbox360 controller. I have another 3rd party controller that works fine... – Viché Apr 28 '20 at 08:33