Sync P2P player positions?

Thanks to an example by The_angryraph (Gdevelop P2P exemple by The_angryraph).

I know that it is 100% possible and works very well. But I can’t understand how they did this though. And I don’t exactly want to plagiarize someone else’s lines of code.

Is there any other simple way to do this or should I put in the work to figure this out?

Here’s their code by the way.

They’ve written there’s no copyright, so it won’t be plagarism.

As to how how it’s done :

At some point at the start of the game it’s determined whether the game being run is player 1 or player 2, and set a global boolean variable for this (the variable is named “Player1”. A better name would have been “IsThisPlayer1”. but I digress)

The event where Event x”` is received, the data passed is put into x1 if this game is played as player 2, because the data received is from the other client which must be player 1. Or the data passed is put into x2 if the game is played as player 1, because the data received must be from a client playing as player 2. Same story for the y event and values.

In the first 3 events of your screen shot, the check is made if the game is player as player 1 or 2. If it’s player 1, then the Player2 position is updated with the x2 and y2 values (see previous note on that), and player 1’s position is sent to the other client (which will be the game for player 2). The same thing happens for player 2.

Hopefully that explains it. But holler if you have more questions.

Does the scene variables being named “x1” or “y1” etc. important to achieve this?

Not really, they could be named anything. With this naming convention it’s easier to see that x1 & y1 are variables used for Player1’s position, and likewise x2 & y2 for Player2. But otherwise they could be named anything, though the name will need to be changed in all events.

Note that Player1’s variables are not visible to Player2. There’s no mysterious/magic connection that lets one player see what variables the other player has. Imagine you’re running the game. The variables you have only contain values your program sets. So if you are Player1, you determine the values of x1 and y1, while the events that are triggered by Player2, via the P2P connection, set the values of x2 and y2.