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

Author Topic: How to handle multiple event for multi player ?  (Read 4911 times)

0 Members and 1 Guest are viewing this topic.

Elfayer

  • Newbie
  • *
  • Posts: 42
    • View Profile
How to handle multiple event for multi player ?
« on: March 19, 2013, 11:55:46 am »
Hi,
I'm trying to do a multi player game. The player1 use:  Left, Up, Right, Down ;  and the player2 use Q, Z, D, S (for example).

I have one handler event function :
Quote
void    LibrarySFML::handleEvent(eInput& input1, eInput& input2)
{
  while (window.pollEvent(event))
    {
      if ((event.type == sf::Event::Closed) || (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape))
        input1 = QUIT;
      else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Left)
        input1 = LEFT;
      else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Right)
        input1 = RIGHT;
      else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Up)
        input1 = UP;
      else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Down)
        input1 = DOWN;
      if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Q)
        input2 = LEFT;
      else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard:: D)
        input2 = RIGHT;
      else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Z)
        input2 = UP;
      else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::S)
        input2 = DOWN;
    }
}

The thing is that, this way, if the player1 push Left then Up, he'll go Up. And even if I take the first input, he'll go Left (great), but the Up event will be unstacked. The best way to handle it is to do so:
Quote
void    LibrarySFML::handleEvent()
{
  while (window.pollEvent(event))
    {
      if ((event.type == sf::Event::Closed) || (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape))
        return (QUIT);
      else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Left)
        return (LEFT);
      else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Right)
        return (RIGHT);
      else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Up)
        return (UP);
      else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Down)
        return (DOWN);
    }
}

This way the Left will be return and the next turn in will return Up ! But I'm handling event for two player so I can't do that this way, or they won't be able to move in the same time together.

Any advice ?
Thanks in advance.

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: How to handle multiple event for multi player ?
« Reply #1 on: March 19, 2013, 12:27:51 pm »
I´m not sure what´s your problem but i try to help you  8)

*Do you want that the play can walk left and up and the same time?
Then make 2variables one for the x-axis (left/right) and one for the y-axis(up/down) and change the if´s:

if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Left)
        movementX = LEFT; // movementX for the x-axis
else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Right)
        movementX = RIGHT;

else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Up)
        movementY = UP; // movementX for the y-axis
else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Down)
        movementY = DOWN;

//The same for the second player
//So the Problem in your second code is also fixed...

 

Btw please use the codetag (its in the second line right a dropdown menu


AlexAUT
« Last Edit: March 19, 2013, 12:30:24 pm by AlexAUT »

Elfayer

  • Newbie
  • *
  • Posts: 42
    • View Profile
Re: How to handle multiple event for multi player ?
« Reply #2 on: March 19, 2013, 01:23:03 pm »
Quote
Do you want that the play can walk left and up and the same time?
No.

For example:
If the event stack (I know it is not a stack, but really looks like the same) is : Left, Q, Up.
Left and Up is the player1 input. Q is the player2 input.
In the case I showed, the player1 will go up and player2 left. I could handle it so that the player1 go left and player2 still left, but the up event will be unstacked. This way the player1 won't go left then up, I only can make him go left or up (first or last input of the player).

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: How to handle multiple event for multi player ?
« Reply #3 on: March 19, 2013, 05:51:10 pm »
Ok i think i got your Problem  :)

Make 2functions, one for each player with a return value

enum Movement
{
   left,
   right,
   up,
   down
};

// Functions prototypes
Movement EventPlayer1(sf::Event& event);
Movement EventPlayer2(sf::Event& event);

// Game loop
   //...
   while(window.pollEvent(e))
   {
       // Close,resize,lostfocus....

       movementP1 = EventPlayer1(event);
       movementP2 = EventPlayer2(event);
   }
//


Movement EventPlayer1(sf::Event& event)
{
   // if left is pressed: return left ...
}
Movement EventPlayer2(sf::Event& event)
{
   // if Q is pressed: return left...
}

 

This way you have 2vars wich hold the current movement...


AlexAUT

Elfayer

  • Newbie
  • *
  • Posts: 42
    • View Profile
Re: How to handle multiple event for multi player ?
« Reply #4 on: March 20, 2013, 01:39:49 am »
Hmm not sure that'll work... I already tried something similar :
Quote
eInput  LibrarySFML::handleEvent1()
{
  while (window.pollEvent(event))
    {
      if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Left)
        return (LEFT);
      else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Right)
        return (RIGHT);
      else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Up)
        return (UP);
      else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Down)
        return (DOWN);
    }
}

void    LibrarySFML::handleEvent2()
{
  while (window.pollEvent(event))
    {
      if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Q)
        input2 = LEFT;
      else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::D)
        input2 = RIGHT;
      else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Z)
        input2 = UP;
      else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::S)
        input2 = DOWN;
    }
}

In this code, if I call handleEvent1 before handleEvent2, the window.pollEvent is going to "unsack" all the events of the window. Then in the handleEvent2, there won't have any event.

Khnemu

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: How to handle multiple event for multi player ?
« Reply #5 on: March 20, 2013, 02:44:37 am »
What about keeping 2 vectors of player inputs (one for each player). You push keys in them as you unstack events, depending on which player is concerned.
Then you just pop the keys once at a time when dealing with it in your main loop.

If i understood correctly what you want to do, that should do it.
« Last Edit: March 20, 2013, 02:46:29 am by Khnemu »

Elfayer

  • Newbie
  • *
  • Posts: 42
    • View Profile
Re: How to handle multiple event for multi player ?
« Reply #6 on: March 20, 2013, 02:54:22 am »
Yep, I had thought of it. But I was wondering if there were no other way to handle it.  ;D Anyway thanks for answering, I'll probably do that way.

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: How to handle multiple event for multi player ?
« Reply #7 on: March 20, 2013, 04:33:40 am »
On each of your Input checks you instantly return, therefore not allowing any further events you might have to be processed. One way I like to handle multiple inputs (especially for complex button presses) is to create a set of booleans

bool p1keyLeft, p1keyRight, p1keyUpt, p1keyDown, p2keyLeft, p2keyRight, p2keyUp, p2keyDown,

This way, once you do your poll event, you can just assign which keys get pressed during this event loop.

while (window.pollEvent(event))
    {
      if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Left)
          p1keyLeft = true;
      else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Right)
          p1keyRight = true;
      else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Up)
          p1keyUp = true;
      else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Down)
          p1keyDown = true;
    }

Once you have these booleans set, you can use them any way you want, check each one and move your character accordingly. Remember to reset them to false at the beginning of your input though.

zoran404

  • Newbie
  • *
  • Posts: 41
    • View Profile
Re: How to handle multiple event for multi player ?
« Reply #8 on: March 20, 2013, 09:43:06 am »
It realy irritates me when I see people use if else insted of switch and here you even have the same first conditions.  >:(

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: How to handle multiple event for multi player ?
« Reply #9 on: March 20, 2013, 11:44:33 am »
Woops! These should'nt be
if else()
they should all be just be
if()
( we want to check each one, since each key can be applied ) sorry about that.

Guess that's what I get for programming at 3am, mistakes are bound to happen  ;D

Khnemu

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: How to handle multiple event for multi player ?
« Reply #10 on: March 20, 2013, 07:04:20 pm »
It wouldn't work anyway. You'd lose the input order.