SFML community forums

Help => Window => Topic started by: sandwich hoop on July 04, 2024, 05:03:16 pm

Title: Two instances of game both accepting same input.
Post by: sandwich hoop on July 04, 2024, 05:03:16 pm
Hi,
When I have two instances of my game open, and try to press A to move left (which does work) both instances of the game accept the A input. My question is, is there any way to create a window which won't overlap with other instances so that if I press A in one, it doesn't impact the other? Hope that makes sense, I can clarify things if needed, hopefully, I'm just overlooking something simple.

Edit: I can't just run it on another computer because what I'm testing is the network connection, but I want to get everything else working before I figure out how to do it between computers.

:)
Title: Re: Two instances of game both accepting same input.
Post by: eXpl0it3r on July 04, 2024, 10:37:41 pm
If you use events, then only the window with the focus will get the input.

That means, don't use sf::Keyboard::isKeyPressed() or sf::Mouse::getPosition() or sf::Mouse::isButtonPressed().

Check the official tutorial on events: https://www.sfml-dev.org/tutorials/2.6/window-events.php
Title: Re: Two instances of game both accepting same input.
Post by: Me-Myself-And-I on July 05, 2024, 04:20:44 am
Or as an alternative you could put the code in an if statement checking window.hasFocus().
Title: Re: Two instances of game both accepting same input.
Post by: sandwich hoop on July 05, 2024, 09:04:23 pm
Exactly what I was looking for! Thanks! This is such a useful little feature I wish I knew about sooner!
:)