Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [solved] joystick detection  (Read 1404 times)

0 Members and 1 Guest are viewing this topic.

djarkan

  • Newbie
  • *
  • Posts: 16
    • View Profile
[solved] joystick detection
« on: October 13, 2024, 05:22:35 pm »
hi

i detect if a joystick is plugged, if not, i loop to detect the plugged one. but detects nothing

thanks

void Player::setJoystickID()
{
    unsigned int pluggedJoystickNumber{0};
    while(pluggedJoystickNumber == 0) {
        sf::Joystick::update();
        for (auto i = 0; i < sf::Joystick::Count; ++i) {
            if (sf::Joystick::isConnected(i)) { ++pluggedJoystickNumber; }
        }
        if (pluggedJoystickNumber == 0) {
            std::cout << "no joystick plugged" << std::endl;
            sf::sleep(sf::milliseconds(1000));
        }
        if(pluggedJoystickNumber == 1) { m_joystickID = 0; }
    }
}
 
« Last Edit: October 14, 2024, 06:53:51 pm by djarkan »

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: joystick detection
« Reply #1 on: October 14, 2024, 03:30:02 pm »
I don't know which OS you are on, but at least for Windows SFML only detects new joysticks being plugged in when the window receives a WM_DEVICECHANGE event. sf::Joystick::update() doesn't look for new ones, it only updates the state of existing ones.

Window events are only received when you call the window's pollEvent() method. Since your code is busy looping waiting for a joystick, it will never poll for events and detect a joystick was added.

djarkan

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: joystick detection
« Reply #2 on: October 14, 2024, 06:53:21 pm »
thanks !!!!!!!!!!!

didn't noticed JoystickConnected

works perfect  8)