Skip to content

Joystick won't update without Joystick.Update() even when calling window.DispatchEvents() #189

Description

@andre-sampaio

Despite polling window events the Joystick status is not updated unless explicitly calling Joystick.Update().

Minimal working sample (with one joystick connected):

        static void Main(string[] args)
        {
            var window = new RenderWindow(new VideoMode(200, 200), "Joystick doesn't read :(");
            
            while (window.IsOpen)
            {
                window.DispatchEvents();

                Console.SetCursorPosition(0, 0);

                for (uint i = 0; i < Joystick.Count; i++)
                {
                    var isConnected = Joystick.IsConnected(i);
                    Console.WriteLine($"{i}: {isConnected}");
                }
            }
        }

Expected result: One joystick is connected
Actual result: No joystick connected

Calling Joystick.Update before the loop will update the joystick status and will achieve the desired result.

The same applies to actually getting the Joystick state:

        static void Main(string[] args)
        {
            var window = new RenderWindow(new VideoMode(200, 200), "Joystick doesn't read :(");
            Joystick.Update();

            uint connectedJoystick = 0;
            for (uint i = 0; i < Joystick.Count; i++)
            {
                var isConnected = Joystick.IsConnected(i);
                if (isConnected)
                    connectedJoystick = i;
            }

            while (window.IsOpen)
            {
                window.DispatchEvents();

                Console.SetCursorPosition(0, 0);

                for (uint i = 0; i < Joystick.AxisCount; i++)
                {
                    var position = Joystick.GetAxisPosition(connectedJoystick, (Joystick.Axis)i);
                    Console.WriteLine($"Axis {(Joystick.Axis)i}: {position}");
                }
            }
        }

Expected behavior: Axis position gets tracked accordingly to controller
Actual behavior: All axis values stuck

Again, calling Joystick.Update() inside the event loop will update the values correctly.

IDE: Visual Studio 2019
.Net SDK: .NET Core 3.1.301
Platform: x64
Configuration: Debug
OS: Windows 10

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions