Why do we mix server and client codes?

Hi,

I have a general question. I do not understand one thing, Why do we mix codes of clients and servers in many c# scripts, and why there are so many code duplications? Wouldn’t it be better and cleaner, if we store all server code in one place and client code in another?

PS. I am at the lesson ‘Selecting Multiple Units’.

1 Like

Hi there,
As in most programs, there are multiple ways to achieve the end goal and to organize the scripts. In this case the scripts are grouped by functionality rather than where they are executed. So if a script is doing a similar task on both client and server that is in one script.

Which specific code duplications are bothering you? A lot of times in a course like this I can see places where refactoring could be useful if expanding the project. However, at the current stage, for learning multiplayer, it’s not always the most efficient use of time for the student.
Curious about your thoughts on both of these points.

Hi, thanks for answer.

OK, duplications might not be the problem, because there have to be code that is executed at client and server. I just imagined, that the mirror would handle it. E.g. 1) client says we move to XYZ - 2) server see it moves and 3) command player to move. I just thought server would just broadcast this info to all players in some way.

I also thought about some kind of AI system we fight against (like in starcraft) or shared stats system. It could grow large, and in my imagination that would also be a driver for “where we execute code” script organization.

Finally I imagined that we have like separate builds for server and client side, which would be also an argument for the separation.

Cheers

You make a lot of interesting points.

Mirror does handled movement syncing through the network transform component and you can implement similar functionality with other variables using syncvars.

For an AI component, I can imagine there are two ways to look at it. One way would be that it is mostly separate from the player controls, in which case it could be handled entirely server side, and that code would be almost all server implementation. The other way to do AI I think is just to have some minimum behaviour that interfaces with the player controls. In this case it would also run on the server side, but would have to interface more with the client code.

In terms of separate builds, I’m not sure at what scale it becomes more efficient to do this. Mirror is doing all the hard work in running all the server stuff, server side and client stuff, client side so we don’t have to think about it. Having that leftover code that isn’t running locally isn’t a big deal with todays hardware. I’m not sure how this is managed for other games. Certainly there could be backend server stuff that isn’t shipped to the end user, but in terms of gameplay I’m not sure how significant it is.

It makes sense to me that if you were building a game where there was significant separation between the parts it would make more sense to organize your code that way.

Thanks! I think I understand much more. I would love to see more implementations/courses on multiplayer game, e.g. like the ones that is used in mobile games (see: e.g. Tactile wars; maybe there are better examples)

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms