I am making a 2D game that involves all players being just a 2D square.
The players can move like a platformer, where it can jump and down, move left to right with collision detection.
Now to the main topic, what if I wanted to add another player that is controlled on a different network;
How would I replicate our player movements to the other person’s screen?
I did have a solution, but it is really unsafe:
- When you move your player to the right, it will fire a data to the server about your player moving the right.
- When you stop moving your player (which is unholding the D key), it would fire another data to the server about your player has stopped moving to the right.
The unsafe about this is that if you suddenly lagged while moving, and you stopped moving, the server will never get a signal about your player stopping it’s movement, therefore causing you to move forever on the other player’s screen.
And using remote events might be not the best idea as it can reach the limitation.
What would be the best approach in this problem?
2 Likes
Use remotes to communicate between the client and server, and have the server list player positions or just return the data to the other clients. I would recommend a RemoteEvent for this. Then I would do a quick tween on the client’s end to smooth out the position updates as they’ll likely be a bit jittery otherwise. You can also implement rollback netcode for even more accurate replication, but that’s optional and mainly used for fast paced games such as fighting or fps games.
2 Likes
Thanks, I’ll take a look with the “rollback netcode” and try my best to implement in to my game.
I’ll give a response soon as possible.
Hi, I did lot of research about the rollback netcode, however it seems that it is mostly used in a fighting games. My game is a 2d platformer game where you can walk around, so I don’t know how I would implement the player to rollback at it’s correct position.
Could I get some idea how would I approach in this scenario? I’ll appreciate it. and also thanks for the response!
The basic concept of rollback netcode is simply predicting input and correcting if wrong. The idea is that whatever the player is currently doing, they’re probably still doing that in the next frame. A simple example might be an arrow. When the arrow is shot, the client tells the server, the time difference is recorded (the time it took for the message to reach the server), then it adjusts and sends it to all other clients. Those clients then measure from that new time and update the arrow’s position by that amount. The arrow’s animation would need a bit of a build up so that the other clients can simply speed up that animation to catch up to where it should be. Perhaps an arrow isn’t the best example as this is more used for actual character animations and not projectiles.
The video I linked describes it much better than I can. However, I don’t think it’s necessary for every game. In your case, you can just tween the positions of other players to match where the server says they are.
Another use-case would be to predict where a player would be. I’m doing this with my most recent project where it simply takes the position of the player and adds the movement direction, multiplied by some amount. The reason for this is because whenever you’re moving, the server is always going to be behind by some amount. The AI enemies were targeting where I was, but not where I currently am on my screen. So giving the AI a predicted position allows the AI to actually target where I should be by that time. This can also be applied for updating other player positions for the client. There’s a lot of things that can be done to sort of hide the network delay and make things appear to be running completely client-side. I feel like more games should do this for a better user experience.
1 Like
I got the idea now, I haven’t implemented yet but the solution seems very promising and I’m confident with it.
Thanks for helping!
1 Like