2
\$\begingroup\$

Lately I've been looking into networking and sockets and made small test programs to get the basics down. So far I've made a TCP turn-based game from where I learnt a lot and helped me getting some fundamentals down.

Recently I also got a basic game working where multiple clients can connect and see each other move, but I feel like the architecture is not the best though. This made me want to expand the game a little and build a more solid foundation but I've been struggling to find the answers on a few questions I have.

The approach I've had for 2D action games(which is my goal to make) is to have a dedicated server, a separate project written purely in java(I use LibGDX for clients) and in the server project I have a Client class which has a x, y, and an id. Then when a client connects to the server it creates a new Client object, gives it an ID based on the amount of clients connected and puts it in an ArrayList called players.

Then for movement I have the Game client send a message when it moves, structured like id,x,y;. The servers Client object receives the message and tells the server to broadcast() it to all the other clients which in turn receives a position message. The Game clients get a message, structured as above, and the Game client updates his own playerList.

This feels really clumsy and not well structured at all and this leads to my next question, what data should the server have?

Let's say we have a 2-player game where the players cooperate to kill mobs and receive items and exp. How would the server keep track of the mob entities? Would i create an identical Entity class from the Game Client and put in the Server project with everything but the render / graphical stuff? And collision with projectiles and stuff, do I collision check in the Game Client and then validate in the server?

All this feels very overwhelming and I would love if someone could give me some advice or point me to any forum posts they've read. I've searched around the forum but haven't managed to find anything that really answers this structuring part.

Thanks in advance, feel free to ask me anything!

\$\endgroup\$
1
\$\begingroup\$

First of all, you're using Java and LibGDX, so you have access to UDP. UDP is better in a lot of aspects than TCP if you make anything but a strategy or puzzle game (as a rule of thumb, UDP is better if missing a couple packets doesn't matter that much, but speed is crucial).

It's perfectly normal to have a single project with two entry points for a server and a client. This way you don't have to write everything twice. The only difference is the main game loop (the server doesn't render, just update) and the update method (the client should receive the updates to the entities and send inputs, the server should send updates to the entities and receive inputs)

\$\endgroup\$
  • \$\begingroup\$ I was trying to learn UDP for this reason but it was really difficult and I had a hard time grasping how the server would send data to multiple clients and such without sockets etc. I also heard that TCP was pretty much as good as UDP if i disable some algorithm so I went with TCP since it feels simpler. But if you have any advice on UDP feel free to share! Now to the structure, so I could basically copy the classes needed and just delete the graphical stuff and then in the update just keep sending entity information and add into the clients entity lists and map list? Thanks! \$\endgroup\$ – Dennis Björk Oct 29 '17 at 22:54
  • \$\begingroup\$ @DennisBjörk It's hard to wrap your head around it first, but you'll understand it eventually. The reason TCP is so slow compared to UDP is that it makes sure everything arrives without holes and in order. As to your second point, yes, but you could just use the exact same classes without rendering them. Your main classes should handle that. \$\endgroup\$ – Bálint Oct 29 '17 at 23:00
  • \$\begingroup\$ I guess I'll read up on more UDP then! So let's add a variable and call it "netmode". Netmode can be 0,1, or 2. 0 = Single player/No server or any message sent. 1 = Client and 2 = Server/host. Then in my update and render methods etc I could put if statements like if(netmode == 2) {//do server stuff} could that be a way to go about it? \$\endgroup\$ – Dennis Björk Oct 29 '17 at 23:20
  • \$\begingroup\$ @DennisBjörk You could just have 2 or 3 separate main classes and make them use different functions if they need to \$\endgroup\$ – Bálint Oct 29 '17 at 23:23
  • \$\begingroup\$ That's even better..damn you make me feel stupid but thanks! :') \$\endgroup\$ – Dennis Björk Oct 29 '17 at 23:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.