Rts lock step networking base example

Hello,

I have been working on a Lock-step script for an RTS that I am making. I got a basis for it working, following the simple rules of “update until end of turn, sync, repeat”. As such, because I have spent long and hard reading forums and thinking about how to set it up, I am going to post it here for other people to use/modify/reupload…whatever. If you have changes to it, feel free to email them to me and ill upload it back to my site. I also welcome suggestions and questions. I will post any non-game-specific updates I make to the architecture to keep it constant with what works and what doesnt.

This is meant to be a community tool that people can use and modify to get started on networking RTS games.

Please Note: This is an example and will not work as is. But there are comments to help you get started and I am available for questions.

Thank you all for your support thus far, and for future support.

Edit: Removed link

Ok, there are a few problems that I see with your implementation:

This defeats the purpose of a lockstep system, which allows for the possibility of almost unlimited units. Adding a network view like this will vastly reduce the amount of active units you can have. The idea behind a lockstep system is that it records what a user does, then sends those same commands to all users to be executed on the same turn.

With this in mind, you are missing the meat of a lockstep system… command sending / turn synchronization.

Also, just a note for your sanity, don’t use “frame” as the turn indicator, use turn.

You can’t just drop old commands! This will cause a desynch! You also cannot execute out of order. My solution to this was to send a packet with an intent to do something. This packet is always expected to arrive. The simulation is held until this packet does arrive from every user. The packet would hold details of what I intend to do in the next packet, if there is a next packet. This has a few bonuses, and some obvious setbacks. The bonus would be better cheat prevention. The pitfall is of course more packets.

Last edit, lol:
You are also missing something important to building a lockstep system… Variable turn time / update time. This needs to be changed based on the highest latency / computer processing ability

I don’t know what you are smoking dude, but this is not lock step.

haha, I was trying to be nice

hi, i am working on one, checkout if interested.

I’m currently working on an RTS game in Unity and I’d love to have an RTS multiplayer networking solution by the time I get to that phase of development.

I’ve only briefly glanced at how to setup networking however, so I have a lot of questions.

I read a few things about needing to use the lock-step method in order to do RTS. Is it the best method for an RTS?

I also read that using floats and timers will lead to a desync later on down the road as the game slowly spirals off-course due to float errors. Is this true?

How can I program my game to work with a networking solution that is realistic? For instance, do I need to somehow change all of my variables with “float” types to something else. Do I need to get rid of all the timers I have implemented in my code? Is there a rigid structure I need to program by in order for my rts to work with the networking?

I have so many questions, and so little idea of how networking in general works, I’m very curious. Thanks for any help, and who knows I might be able to help you with getting it to work as well later on down the road.

depending on the size of your armies you will most likely use the lock step method. p2p is more suitable for this but client server is also possible as i have managed to do it which does well up to 100ms ping with 10fps simulation. floats, randoms, timers etc. all should be carefully taken into consideration. it is said that on the same platform with same compiler settings(for c++) floats are deterministic. my tests with floats never resulted a dsync but tested only on pcs with intel/amd processors. fixed point math is a reliable option but has its drawbacks. you could inspect my code to get an understanding of how things can be setup but i am still considering to put it on asset store due to low interest.

Hi tasadar,

That is really cool that you’ve worked on this a little at least. So first of all, are you using purely C++? Or did you test this with C# or Unityscript at all?

Secondly, I’d be perfectly fine with p2p as long as it works. Any method that actually realistically works would be fine with me. I just really want to see multiplayer work for my game, as I’m sure you do for yours as well.

Thirdly, I’ve heard Unity’s physics system (math) ruins even the possibility for a multiplayer rts because it’s not deterministic. But what actually happens, for example, when a unit’s collider “collides” with another units collider. Does it really need floating point precision to calculate that a collision occurred? Couldn’t we somehow say to the game/network “as long as it’s close its okay, we don’t need that much precision”?

Another thing is that I am making heavy use of physics in terms of object colliding with other objects and triggers and whatnot, however it’s mostly all for “show” so-to-speak. It’s not really crucial to the gameplay, it’s simply for aesthetic purposes. Could I still implement a deterministic lock step networking method, and not worry about physics. The only physics I think would be crucial would be unit collisions (that don’t have to be super precise), and raycasts for unit selection purposes. Would these not work correctly in a deterministic framework either?

I don’t think I’ve heard anyone say it’s impossible to do multiplayer lock-step rts, but maybe it is? Does anyone know for sure if it’s quite simply impossible?

Fourthly, why are animations even relevant in networking lol? I thought animations/physics/etc, was all done client-side, and especially for rts games, it was okay if they were a little lagged behind, or a little off, as they don’t really affect the pure gameplay?

Anyways, just more questions I have. Thanks.

I forgot to mention. Tasadar if you develop a networking framework that works with a “traditional” rts, I would definitely be interested in it. I would want to make sure it works with my game though first before I would buy it though :slight_smile:

1- first i made a demo with unity c#, made a few tests and i will use the same structure in a c++ project.
2- unitys built in network has no p2p so i did it client/server and worked fine but it would be more ping resistant if it were p2p.
3- if physics is just for aesthetics than no problem. if a collision test effects gameplay than you cant use it, but for ex. if a collision test only results a particle effect than no prob.
4- if an animation triggers a game event like firing than it is important. other cases should have no effect.

to determine dsync cases you calculate a hash value for the game state. this should include anything that can effect the flow of the game. unit transformations, hitpoints etc. every client must generate the same hash value so that game can continue.

there is no network framework or anything like that i am working on. i belive every game needs its own implementation, ready solutions usually wont work well.
what i am offering is a sample implementation of a lock step multiplayer rts game demo which is simplified to make it easily understandable.

Interesting. Thanks for the info.

Well, I am looking far ahead right now, as I’m still a long ways from actually having to implement multiplayer in my game. If I have some time, I’m really interested in how you accomplished yours, even if they were just tests right now.

The thing I’m kind of dreading is if I have to completely scrap Unity’s built-in physics and implement another third party deterministic physics engine (if those exist), in order to implement multiplayer :frowning: I really hope this won’t be the case. The time it would take to learn how to do it, and then code it, integrate it, etc… would be enormous. If I end up having to do that, my game will most likely have to be single player. Which for my first game ever, wouldn’t be too bad, but I was really hoping multi-player would be somewhat “plug n play” with Unity.

I hate the fact that RTS’s are the most complicated game genre to make (in my opinion), but it’s the only genre I like. Go figure :slight_smile:

Sorry I’m a little late to the party. Here’s an example of a lockstep implementation in Unity:

.
I’ll be sending out the first Beta of the deterministic physics engine soon, which is 1/2 of the challenge of making a lockstep game.

If you’re still interested and alive (jk (hopefully (a lot can happen in 2 years))), please check it out. More information is linked in the video’s description.