Network is both fun and frustrating. This week I continued working with the networking code, the problem however is that it is going slow. You need go about coding in a bit different way than you do when you code otherwise. Because when you code for singleplayer you don’t have to take into consideration what happens to the object when other people see it. In a network however you need to send the location of your player to the others, but at the same time enable the code to be able to get the movement input from the other players as well.
That I can tell you is a pain to get right. I’m not completely there when it comes to wrapping my head around how to fix this, but I’m slowly getting there. Right now we have a way to update the players position on the other clients, the next thing we need to do is to synchronize the creeps which we call soldiers with the other clients, that means we need to implement a way that only one player is setting the positions of the creeps and updates whether or not they are alive. This is however the problem, the code needs to be reachable by all clients, so they need to know that a creep have spawned, where it spawned, what team it belongs to, how much hp it has and what its current action is.
Sounds simple when it is put it like that, however getting all that into a script, that we already have made is not that easy. Firstly I wasn’t the one that wrote the original code, so I need to first look into the code, understand what happens, then rewrite the whole thing so that the script that spawns creep only gets called once, so that the game objects that represents the creeps is spawned locally but are controlled from the host. This needs to be done for every single object on the playing field. Right now that is what happens with the players, so that the players can see each other.
At least I got that to work at last, we had the weirdest bug that made 1 player see the other and see all movements, but when another player connected and selected a different team, they couldn’t see anyone else than the players belonging to their team. The other team could still see everyone. That said, I solved the bug at last, simply just one integer that was different, once I put the integer to the same for both teams, it worked fine.
In anycase, here’s what the client and “server” looks like at the moment, graphics are just placeholders right now, so it doesn’t look good, but at least we have something that is somewhat playable.
I say “server” because right now the server actually shows what is on the screen, reason for it being blue is that while connecting the clients the camera scrolled into something that doesn’t contain anything and it gets blue. The little disconnect window is the server window.
In any case, the work goes forward, although slowly, but we’re getting there. Next week I’ll get some help with the networking, which I need, that said, we also need to set up a plan on how we want the game to work. As I see it, we should maybe make connection and server boxes in a different scene, so that we need to load a new scene then wait a little bit before the game starts so that all clients have a chance to load the game before it begins.