need advice on best conventions of coding

I’ve only made small games and helpful programs for personal use. I want to make a large multiplayer game now but i don’t know any of the conventions involved with keeping large games manageable and organised. The game will start small and be fully functional before i start adding additional characters and additional content. It should be very manageable for my current skill level but i have several questions before i end up with a horribly cluttered and poorly designed framework for my game. DotA is a good example for you to imagine when considering these questions.

1: My game has RTS style controls, you click on yourself then right click to move yourself. There will be an option to take control of enemy units and left click then right click to control them as well. (Control is given if your ID matches a variable on the thing you’re clicking.) Should all my units use the same script that includes movement and attacking? I also need to include some spells (additional GetKey functions) for the main character, should it be inside the movement/attack script with some “if==main” lines attached to the spells? Should the spell script inherit from the movement/attack script? Should the spell script be entirely separate? I’m leaning towards it being entirely separate or inheriting since i’m thinking nonmagical units will never be able to gain spells and thus there’s no reason to include the functionality in any way. What would be the most conventional way of handling this when all units on the map move/attack in the same fashion but not all units can do additional actions?

2: What is the best way to handle multiple colliders on the same gameobject? My enemies need a collider for collisions and another collider for triggering their aggro radius. Should their aggro radius be a child with the OnCollision scripts attached to it? Also my players can buy auras that would probably use a collision trigger to know who is inside the radius. Would it be more reasonable to add the component and the script to the player or add a child object with a script.

3: Is it standard practice to slap multiple scripts onto the same gameobject? All my units right now have Health and UnitCapabilities scripts attached directly to the gameobject, in addition to my colliders and rigidbody and navMeshAgent. It just seems like it’s a lot… or that it COULD be a lot but i’ve condensed some things like movement/attack into the same script. How many scripts on the same gameobject is considered reasonable?

4: Most importantly, how exactly do servers work? I’m using photon PUN right now (the free version) and i’ve come to the conclusion that a server is basically just an additional player who doesn’t really do anything and is considered the masterclient. The masterclient is capable of doing some extra options like instantiating scene objects that persist when players disconnect, which is important for a game that is supposed to be played from start to finish without people joining or leaving. Am i right in thinking that the server is playing the game along side you waiting for you to finish then creating another lobby for new players to join a new game? So if i build this entire game with the logic that the master client controls extra things that make the game operate, people can do hackable custom lobbies (if they’re the host) but also i could get servers and set them as the only clients who could create a public lobby. Then the server would be the master client and be in control of the game. Is this right?

Question 1

No… if they move in different ways, use a different script. If they move in the same way, use the same script.

Should the script for ‘spells’ be in the ‘movement’ script? Well, since it’s a movement script… and not a spell script… I’d say no. It should be in its own spell script.

Also… if your script is saying something like “if(self == main)”… you’re probably designing it poorly.

Again… why? It’s a spell script, not a movement script.

Yes, probably.

Unity is designed with what is called the ‘component design pattern’… which is similar to the ‘composite design pattern’. The idea is that to add functionality to an object, you add a component that implements that functionality.

So to add ‘magic abilities’ to a unit, you add a ‘magic script’ to that unit.

Question 2

So the script that reacts to the collider usually has to be on the same object as the collider (rigidbodies add some complexity to this where the object they’re attached to will also receive the messages).

I usually use a ‘entity’ structure for everything… I describe that over here:

I usually go with ‘child’.

Question 3

Yeah… I have anywheres from 1 to 20 scripts per gameobject.

Reasonable is what you consider it to be.

Personally I like to break up my scripts into multiple sub-gameobjects. But that’s just me.

Like from the linked thread, that image, I have multiple child gameobjects… some for animation data, some for the rig of the entity, others for various events that occur.

Question 4

That’s a very broad question…

Here’s the thing… different servers are designed different ways.

For example a classic server design is a peer hosted server, where one of the players is considered the server. This design is considered flaw though since that player gets a home advantage.

Other designs where the server is hosted separate from the players can be very different. Some might just end up being a player in the game, that doesn’t play… this is where the server and clients all share the same general structure to the game.

But another hosted server solution will have the server be a slimmed down version of the game, or completely unique from the game.

Servers are a very complicated subject… and can take forever to describe in depth.

Depends on how you structure it… it CAN act like this if you want.

Yeah…