-
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);
}
}
}
}
-
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.
-
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.
-
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?
-
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.
-
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 ;)
-
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.
-
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?
-
I use version 2.1.
-
Was this issue ever resolved?
-
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); } //....}