I know this question is similar to this thread: Component Dependency - Industries - News & General Discussion - Unity Discussions
But I didn’t want to hijack it. Plus this is my first post and I’m sort of winging it.
I’ve watched BoredMormon’s video on component design (10/10 art by the way). I understood it on a high level but I’m stuck on the details at around the 3:00 mark. I see that the human can have the following components attached: combat boots, a tin hat, and a gun. As I understand it, these are just represented by GameObjects which are attached to a GameObject to make a soldier. Is all that makes something a soldier in this game simply the components for boots, a hat, and a gun, and possibly a soldier sprite/model? Do the GameObjects representing the equipment hold scripts defining each property of their respective equipment? (eg is the gun represented by a GameObject with a script attached with fields like damage, fireRate, etc and methods like shoot()?) Also, what is the best way to implement input? If I press “spacebar” to shoot, is this information detected in the Update() method of the Soldier, and passed to the gun? Or maybe detected in the gun’s Update()? Or maybe a separate script like PlayerInput?
Now a second example just so I don’t leave out anything…I guess I’ll just start off with a theoretical flying dogfight brawl game. There is a variety of aircraft. They can hold one or many troops (fighter jet versus chinook). Both the aircraft and the troops have weapons. The player is the “leader”, one of the troops. Then to keep it simple let’s just have the player meet a computer player in a room and fight until either player (leader) dies. Which means both aircraft have health, and all the troops have health.
I hope I’m still making sense here…Now the aircraft. I’m thinking I should not make all the kinds of planes inherit from an “Aircraft” class, but I’m not sure of the component-based solution. Instead, should I make different scripts for each kind of plane? That would mean just adjusting some variables like “speed” and “health” for each different kind of plane, slapping that on a GameObject, making a prefab, and calling it <aircraft_name>, right?
Then to store troops; would this be done best with a List of GameObjects that stores the troops? Or maybe a SoldierHandler script that attaches to the aircraft, which can keep track of the troops and perform actions on them (eg deploying when the craft lands)?
Movement seems easier. Maybe a Flying script and a Running script, which I would attach to aircraft and troops respectively. I have the same question about input as above though.
For the weapons, what immediately comes to mind is a “Weapon” class and all different kinds of weapons as subclasses. However, the components alternative…not sure, same deal as “Aircraft.”
And what keeps track of game state? It seems like the point of components is to eliminate a sprawling web of classes, but I can’t think of any way for there not to a single giant class that keeps track of game state. Score, leader health, UI inputs…ya.
I’m sorry if this was long. I’m still pretty new to Unity and game dev in general, and I don’t have a degree in code structure. I also default to inheritance ALL THE TIME which is probably an issue. Please let me know if I should clarify anything.