Classes and GameObjects workflow

How do you guys organize your game structure?

I am new to Unity but experienced in programming. I would do the following:

  1. For each game object have a class to manage it’s data.
  2. In an RTS game for example, each time you instantiate a unit create a new class for it and to that class pass the newly instantiated game object.

So basically you have GameObject+Class pairs for all units, correct?

GameObject is the “what you see” and the Class contains the logic for that entity.

Depends of your game ofcourse, but the way you say it… sounds like RTS to me. Then I would make the pair.

But actually unity is quite good at not playing animations and not rendering objects, so in theory you could avoid making the GameObject + Class pair. Instead just having the control class as a monobehavior and always allocate the game object. It would give you free access to stuff like navmesh and collision. A single bounding box check per frame isn’t so bad and in many level/scene based games it would be just fine. Still if I was making a single player games with a lot of units(FPS, RTS, etc) or planned on running on low performance devices I would still make the “traditional” pair.

I’m currently writing an MMO, I think I’ll just let the server handle what the player knows, and then feed it all to Unity and see how it works. Correction, I’ll certainly make the pair as I’ll be handling some sort of LOD myself. The player only see full gear details on the nearest objects and stuff like that. I plan to support customized clothing, furniture, characters. Obviously the player will only see such details on the nearest objects. You’ll see the player’s clothes at a distance, but the default version, not the customized one.

So it really depends on your project. :slight_smile:

I always create a new unit from a prefab. The prefab contains the gameObjecvt and the classes, also if you use container units (armys, fleets) i make them and the basic units actual gameObjects. I have a kind of static constructor function for each unit so i can easily create a new unit without having to add the class beforehand.

Also i will take this opportunity to argument for Multi Dimensional Seperation Of Concern (MDSOF).
For example you have a unit called infantry soldier, you start by adding data values, then you add combat logic, then you add gui info functions, then you add economic values and before you know it you got a very large class that is difficult to work with, adding new things becomes very hard.

My solution for this is a sort of layer approach. I will descripe an example adding a complete infatrySoldier. First you create an object dimension folder, where you create unit objects with none feature data, example: the units name, the unit position, ths units team, the units children (for armys and fleets), static constructor funciton and static destructor function. This is the basic class script that gets added to gameObject and their prefabs. Here we make a class called infantrySoldierData where we add the previous mentioned fields.

Then you create a feature dimension folder. Here you create a sub folder for each gameplay concern, for example one for combat and one for economic. In the combat folder we create a folder called infantrySoldier, then add a class called infantrySoldier to that folder. Here we add all logic and data that the infatrySoldier needs to do combat, values like attack, hp and defence. Functions maybe like attack and or defend. Notice that in the Combat folder you can actually create super/abstract classes the the combat classes can inherit if you want to.
Then we do the same thing for economic, a class called infantrySoldierEconomic. Containing data like cost, time to build etc etc.

Now it seems like we are good to go, but we still need to implement GUI, the problem here is that GUI is cross cutting. We would like to be able to extract the cost and hp value of the infantrySoldier to the GUI. What we do here is to create a new dimension. A top folder named CrossCuttingConcernsDimension (yeah long name i know, but it is better then an inaccurate name). Where we add the scripts that handles the GUI and add an interface class called IGUI. Here we add two functions, Info() and Actions(). Then we have the infantryScriptCombat and the infantryScriptEconomic classes implement this interface. Now we are done making the scripts! So all we do now is to group these scripts togheter on a single prefab, which we create gameObjects from.

Now i got a bit long winded here, but this is a flexible architecture that is well suited to RTS. For example if you later on want to add diplomacy to each unit, then you can add a Diplomacy folder to the feature dimension folder and start implementing diplomacy without having to change any code already written and you are not working in a cramped class. Well it is an idea how you can structure your code that works well in Unity seance the gameObjects act as natural hyperModules.