Component Design (noob)

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.

1 Like

Bump…also nobody has to answer all the questions, I’m just trying to get a grasp on components

That is the gist of it, but it’s a bit more nuanced. Components can alternately be called behaviors (that’s why it’s called MonoBehaviour). Because of component x, it has behavior y. The tricky part is that entities are also defined by the values assigned to them. It’s feasible that a soldier and a medic share the exact same components, and only differ in terms of the values given to them. The medic’s “gun” might just have a very short range that deals negative damage.

They don’t have to, but certainly can. This is more about whatever fits your style/workflow.

Now is not the time to get a good definitive answer on this, with the new input system looming over the horizon (it’s likely to be at least a year out from being finished though). While you’re getting your bearings, just try to keep it to as few places as possible.

1 Like

You COULD do it this way, if you wanted to. But don’t spawn too many components just for the sake of making more components.

[quote=“Cutty_Flam, post:1, topic: 630102, username:Cutty_Flam”]
Also, what is the best way to implement input?
[/quote][quote=“Cutty_Flam, post:1, topic: 630102, username:Cutty_Flam”]
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?
[/quote]
Input.ButtonDown. Player is a component attached to Soldier. AiController is a component attached to an enemy Soldier.

[quote=“Cutty_Flam, post:1, topic: 630102, username:Cutty_Flam”]
[/quote][quote=“Cutty_Flam, post:1, topic: 630102, username:Cutty_Flam”]
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?
[/quote]
You can make one script to all planes.
Then you can make one base script for all weapons.
In weapon script you’ll specify prefab for a projectile.
When aircraft shoots, it finds weapons attached to it, and calls weapon’s method “fire” which sapwns projectile.

[quote=“Cutty_Flam, post:1, topic: 630102, username:Cutty_Flam”]
[/quote][quote=“Cutty_Flam, post:1, topic: 630102, username:Cutty_Flam”]
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)?
[/quote]
You can get away with not tracking anything and not having any lists.

[quote=“Cutty_Flam, post:1, topic: 630102, username:Cutty_Flam”]
[/quote][quote=“Cutty_Flam, post:1, topic: 630102, username:Cutty_Flam”]
However, the components alternative…not sure, same deal as “Aircraft.”
[/quote]
One class for all weapons.
Prefabs specify the type of projectile, type of effect on detonation, sound effect, and damage type is a configuration vairable.
No subclasses.

[quote=“Cutty_Flam, post:1, topic: 630102, username:Cutty_Flam”]
[/quote][quote=“Cutty_Flam, post:1, topic: 630102, username:Cutty_Flam”]
And what keeps track of game state?
[/quote]
You don’t really need that. You’re pretty much designing a “robot swarm”, not a system. Think of every object acting independently in the game world. Pick simplest solution you can think of. Instead of subclasses, try to turn “behavior differences” into member variables. Different sound means differnet AudioClip. Different shooting effect means different particle system Prefab. This kind of thing.

You can make Player track its score, and make Player availble through singleton-like interface. Worst case scenario, you can make an invisible gameobject that tracks SOME information that ABSOLUTELY needs to be tracked.

1 Like

@Afropenguinn & @Cutty_Flam Thanks a bunch for asking these questions. Questions that I could not have asked myself, but I have benefited from.

@neginfinity @Kiwasi @RockoDyne @angrypenguin @GarBenjamin @aer0ace @RichardKain Thanks a ton for the responses you guys have provided to these questions. I feel like I’ve been to school for the last two days - my brain has been smoking - slowly dissecting the information all ya’ll have provided. It’s not as easy for me to understand as it is for you to write it, but I feel like I’m getting an education and starting to understand what all this mystical coder foreign language is all about. It started to make sense about 2 months ago (part-time dev) (hands on helps) and the picture is becoming more clear.
I feel like I owe you something. How about your name in the credits - when I eventually release something?

1 Like