SFML community forums

Bindings - other languages => DotNet => Topic started by: kudely on November 07, 2014, 07:59:11 am

Title: Issue using Joystick static methods
Post by: kudely on November 07, 2014, 07:59:11 am
Whenever I press a button with my controller, the e.JoystickId that gets passed in when subscribing to JoystickButtonPressed is always 0. That is fine, however, if I call Joystick.IsConnected(0) it will always return false, as if the Joystick isn't plugged in. I noticed this because Joystick.IsButtonPressed() was always returning false for me whenever I would pass in the JoystickId along with the button that was just pressed. Am I doing something wrong or is this a known issue?

Example:
// My event handler
private void OnJoystickButtonPressed(object sender, JoystickButtonEventArgs e)
{
       _joystickId = e.JoystickId; // This always ends up being 0 for me. But the event does fire whenever I press a button, so it's not like my joystick isn't detected
       _joystickButtonEvents = true;
       _pushedButtons.Add(e.Button);
}

// This gets called every frame update until all button events are dequeued
private void CheckButtonState()
{
     foreach (uint button in _pushedButtons)
     {
         if (Joystick.IsConnected(_joystickId)) // This always returns false. I even tried all combinations (0-7) and it always returned false
         {
             if (Joystick.IsButtonPressed(_joystickId, button)) // This also always returns false but will never make it here since it doesn't get past the first check to see if the joystick is connected.
             {
                 PerformButtonAction(button);
             }
         }
     }
}

 
Title: Re: Issue using Joystick static methods
Post by: Laurent on November 07, 2014, 08:06:00 am
When you use CheckButtonState(), you have an event loop running somewhere else, right? Because if you never call window.DispatchEvents(), you need to call Joystick.Update() to update the joysticks state.
Title: Re: Issue using Joystick static methods
Post by: kudely on November 07, 2014, 08:14:24 am
Yeah, I'm calling window.DispatchEvents() in my main game loop. But, calling Joystick.Update() at the beginning of each check fixed it.

Thanks for the quick help. You can close this now.
Title: Re: Issue using Joystick static methods
Post by: Laurent on November 07, 2014, 08:52:35 am
You're not supposed to need Joystick.Update() if you have an event loop.

Could you write a complete and minimal code that reproduces this behaviour, so that I can check if there's a bug in SFML?
Title: Re: Issue using Joystick static methods
Post by: kudely on November 09, 2014, 09:10:43 pm
Here's my main game loop:

private void GameLoop()
{
    // Grab input
    _window.DispatchEvents();
    _inputProcessor.Update();

    // Update
    _world.Step(Constants.FPS);
    UpdateEntities();

    // Draw
    _window.Clear(Color.Black);
    DrawEntities();
    _window.Display();
}
 

_inputProcessor.Update() ends up calling CheckButtonState() shown in the post above.
Title: Re: Issue using Joystick static methods
Post by: Laurent on November 09, 2014, 10:16:53 pm
I don't want incomplete parts of your game, but rather a new small code that reproduces the problem and that I can quickly compile and test. And that doesn't contain all this stuff that we don't care about ;)
Title: Re: Issue using Joystick static methods
Post by: kudely on November 10, 2014, 12:11:46 am
Here's an example:

class Program
    {
        private static uint _joystickId;
        private static bool _buttonPressed;

        public static void Main(string[] args)
        {
            RenderWindow window = window = new RenderWindow(new VideoMode(800, 640), "Test");
            window.JoystickButtonPressed += OnJoystickButtonPressed;

            while (window.IsOpen())
            {
                window.DispatchEvents();
                if (_buttonPressed)
                {
                    Update();
                }
                window.Clear(Color.Black);
                window.Display();
            }
        }

        private static void OnJoystickButtonPressed(object sender, JoystickButtonEventArgs e)
        {
            // Once we press a button, set the joystick ID
            _joystickId = e.JoystickId;
            _buttonPressed = true;
        }

        private static void Update()
        {
            // Check to see if the joystick that was last used is connected
            if (Joystick.IsConnected(_joystickId))
            {
                Debug.WriteLine("Controller is connected.");
            }
            else
            {
                Debug.WriteLine("Controller is not connected.");
            }
        }
    }
 

Start the program with a controller connected, press a button with the window in focus, and notice that the "Controller is not connected" is always printed out into the debug output.
Title: Re: Issue using Joystick static methods
Post by: Laurent on November 10, 2014, 09:14:08 am
Thanks. I can reproduce the problem with the latest sources, but in C++ the same code works as expected. So it may be that the CSFML DLLs used by SFML.Net are not up-to-date; which version do you use?
Title: Re: Issue using Joystick static methods
Post by: kudely on November 10, 2014, 03:05:10 pm
I use version 2.1.
Title: Re: Issue using Joystick static methods
Post by: kudely on January 08, 2015, 04:46:56 am
Was this issue ever resolved?
Title: Re: Issue using Joystick static methods
Post by: kazyamof on August 29, 2016, 08:57:17 pm
Sorry ressurrect the post, but I'm with the same problem here. When I create a new custom Gamepad object, it looks through Joystick.IsConnected(0) but always receive false. Using current .net version 2.2 on Windows 10 (connecting XBOX One controller). On C++ version runs ok.

This is my custom Gamepad class:

public class Gamepad {

        public Gamepad(uint index) {
                this.index = index;
                if (Joystick.IsConnected(this.index)) Console.WriteLine(String.Format("Gamepad Connected\nID:{0}\n", Joystick.GetIdentification(this.index)));
                else Console.WriteLine("No gamepad connected.");
        }
}
 

On my Game class:

public class Game {
   
        public Game(string title) {
                this.windowTitle = title;
                this.window = new RenderWindow(new VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), windowTitle);
                this.window.SetFramerateLimit(maxFPS);

                this.player = new Player();
                this.world = new World();
                this.gamepad0 = new Gamepad(0);
        }

        //....
}