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

Author Topic: Two instances of game both accepting same input.  (Read 3194 times)

0 Members and 1 Guest are viewing this topic.

sandwich hoop

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Two instances of game both accepting same input.
« 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.

:)
« Last Edit: July 04, 2024, 05:10:03 pm by sandwich hoop »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Two instances of game both accepting same input.
« Reply #1 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
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Two instances of game both accepting same input.
« Reply #2 on: July 05, 2024, 04:20:44 am »
Or as an alternative you could put the code in an if statement checking window.hasFocus().

sandwich hoop

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Two instances of game both accepting same input.
« Reply #3 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!
:)

 

anything