Background: I was led onto CBSE by someone on these forums. I am having a hard time grasping how to integrate this type of system not only into my games but into anything I do programming wise. I decided to purchase Component-Based Software Engineering, Putting the pieces together,(haven’t read every page yet but most of it) but I fear it has been to general for me to fully grasp what I should be doing.
Some of the biggest things I’m having issues with is:
1 - How to properly make components communicate with each other. Lets say you have a ship, and you want to have lets say a cannon, but the cannon balls use GPS to target. To my understanding you would have a ship component, a cannon component, a cannon ball component, and probably something along the lines of a GPS component. These components may need to communicate between each other to know whats going on. But if everything is so separated from each other and you want your system to keep running even if you delete cannon balls how exactly would that work.
2 - In other programming projects such as a software that runs reports, has a user management, and has email capabilities. So lets take the user management portion first. Would that just all be 1 component, or do you break it down, im having a hard time visualizing how you would break something like that down so maybe you don’t. Then I come to the issue of having the other components lets say the reporting component, how exactly does it communicate back and forth to the users component to know if the user has valid Clarence.
I hope I make enough sense for someone to understand where I’m lost. If you can help me understand or have any links to somewhere that helps explain it all I would VERY much appreciate it. Ive been trying to search all over the net for examples but having a hard time finding something that makes the light bulb turn on.
Just on another note maybe I don’t fully understand how separated components should be, and maybe the differences between CBSE and OOP patterns. From the examples I see online they seem awfully similar.
You’re thinking too much from a classical object oriented point of view. Ship, cannon and cannon ball are not components. They are objects. Objects are composed of components. For example, your cannon is an object that could be composed from a component that rotates the turret, a component that moves the barrel up and down, a component that takes care of shooting, and a component that aims the turret at a target by controlling the rotation and barrel components. (this is just an example, not necessarily a good break down).
The advantage of this approach is that when a point comes that you want a player to manually control a turret, you can simply remove the aiming component and replace it by a component that controls the rotation and barrel components based on user input instead. Or, if you want another turret that is a flame-thrower instead of a cannon, you could simply replace the shooting script by one that throws fire. In these examples the rotation and barrel components are re-used for completely different objects.
In Unity, the composed object is a GameObject and each script on it is a component.
That makes much more sense, yeah I clearly was way off base on my thinking… So its probably very rare that a component needs to know about another component. And if it does normally the parent object takes care of the linking im assuming?
I often think of components as things that provide functionality to an object. In Unity’s case the objects I am speaking of are GameObjects.
So for example I have a component called Buoyancy which I attach to any object that I want to float (Rather than having a base class called BuoyantObject and deriving from that.)
I have another component called Propulsion which references propeller GameObjects which are attached to the boat. The Propulsion component is attached to the boat, but it interacts with other GameObjects too (the propellers). It spins the propellers why pushing on the Rigidbody (another component) of the boat.
tomvds already mentioned that you wouldn’t have components for each object… But I just wanted to point out something about communication between components. If there indeed were cannonballs in real life with GPS tracking capabilities then each cannonball would have to have a component inside it that communicated with orbiting sattelites. So yes in a sense you would need a way for these cannonballs to communicate with another object. However in the game-world you obviously don’t need an external sattelite to know where you are… for that you use the Transform component.
“[The ship would have to communicate with the cannon]”… much like the ship’s captain would have to communicate with the gunner on a ship in real life. So the Captain would need to know the names of all of his gunners to say “Tom, shoot your cannon!”. Or maybe he might say “One of you, shoot your cannon!”. To do this in programming you just need an array of Canon components as a variable on your Ship component.