Detect other client in P2P

Hi! I’m making a sort of chess game. I recently started implementing P2P (Peer-2-Peer) and having trouble with it. I want to detect the other client and flip the board on its screen. But I don’t know how. (Detect a client, make it so that a random variable picks which client gets Black’s and who White’s) and I also don’t know how to flip the board. Please feel free to leave any suggestions or pieces of code to make the mechanics work. (Pick a random client or the flipping problem). If you need for me to post any of my code or game elements I’d be happy to, as long as it can help solve the issue.

Best regards,

I’m assuming you’ll use a variable IsClient track whether the program is running the client or the master. In your program, set IsClient to false.

In client A you’ll just be connecting and retrieving a session Id. In client B you’ll be entering the session Id to connect to the session made by client A. When you connect in client B, set the IsClient variable to true and pick which colour. Send the colour info to the Client A by triggering an event on all clients with variable.

To flip the board, you’re best have to have 2 sets of board piece placement events; one for when IsClient is true, the other for when it’s false. It’s the simplest way to do it.

Wow. I can’t believe I didn’t think of that before. I do have one question, how do you detect which is the client and who is the master?

The client will be the one where you enter the session Id :

Great! I’ll try it out and see if there’s any problem.

Hi! I “flipped” the board by moving, for example, the white pawn to the black pawn’s position and vice versa with every piece. Now I have the problem of moving. One direction for one client, and in the opposite for the other, and I don’t know how. I’m thinking comparing the difference between the moved piece position and the other, put it on a variable and apply it to the piece but in negative. Doing that with each piece seems a little extreme and I can’t think of any other option. Can you help me with this?

Edit: I can move both types of pieces at once because at the beginning of the scene I set to each piece, whites and blacks, designed variables with it’s positions, X and Y, and update it when they move. I thought that information could be useful. (The variables help the pieces not to move more than they should and send them back to the saved position).

If you consider the moves as to a square relative to a board corner, the whole movement part becomes a lot easier. I take it that each player’s pieces start along the bottom of the board. When they move to a square, get that squares position relative to the bottom left square. So say the move is the pawn from b2 to b3. The relative destination position would be 1,2 (1 along the y axis, and 2 up the x axis from the bottom left square).

So you send the y=1, x=2 to the other client. In the receiving client, you take the opponents’ piece position and place it relative to the top right square. So with the example it’d be 1 square to the left of the top right square, and 2 squares down (aka g6).

You’d include bit more info in the message - either an identifier of the object being moved, or the positions of all the pieces (saves having to faff around using identifiers for on the playing pieces).

Yeah… Thats the problem. You see, I said I was doing “a sort of chess game”, when I said that, I meant different pieces and, sadly, different board. My board is made of hexagons, I believe 9x9. Could this still apply? And could you show some code?

I would think so. You’d still be able to use the x & y tile positions. Or event the X and Y object position relative to a corner position.

Sorry, I don’t have any code for this. However, I think you’d be better off starting another thread about it (it’s kind-of tangented off the original topic), and someone else may have something they can share. Also include a screen shot of what the board layout looks like - that always helps.

Hi! Last question, I swear. I made two new events. One if IsClient is true and one if it’s not. In one, apart from other things, I have the variables with the positions of each piece, but with different names. So, I have variables for each piece, white and black, if it’s a client certain scene variables and if it’s not, different scene variables. Now, I don’t know how I can move one piece and tell it to the other “doppelgänger”. I’m thinking that if a piece moves, it sends an event which moves the other’s client piece to the same position. Sadly, the board is rotated, so I have to figure out a way of moving the piece to the right spot, not the actual position. Also I need to know if I setting a variable as a global variable makes it so that it can be accesed by another client (So that if a client moves, I can acces the client’s variable to move my piece in that position). Do you have any idea of how? Thanks in advance and if I have another question I’l make it an original topic.