I have a simple setup where I've decided to reduce my overall OOP design and opt for lots of small individual components that I can add and remove from a particular object.
The whole point of this was so components could be added and removed with out depending on other components - essentially they are decoupled.
But this is hard to setup in my case, as I have an object that depending on some of its components attached will depend on whether it is functional.
Let me give an example:
Say the object is a SecurityCamera
object for a stealth game.
It has the following components attatched:
Vision // defines what it can see
PowerInput // defines if it currently recieves power
Health // defines if it is damaged
Movement // Whether the camera can move back and forth depends on health and power being available
Now since each component is separate the problem I have is that the Movement
script needs to know about Health
and PowerInput
. It cannot move the camera unless the camera has health and power.
Currently the way I have done it is a bit lazy: The Movement
component checks the state of the Health
and PowerInput
components in every frame. To me that's not ideal, I'd prefer a less expensive check that I don't have to do in every frame, in that if Health reaches 0 or PowerInput no longer gets power, movement will then stop working.
What is the clean/performant way to link Movement
to PowerInput
and Health
and for movement to be aware of the state of those components so it decides if it can move or not?
It's very easy at the moment for it to break if I forget one of the components so its going to get messy as i develop a project with lots of components for a variety of objects where they depend on each other a lot.
I use C# for my project though this isn't particular language specific.
PowerInput
andHealth
need to know aboutMovement
to pass the data along? I'm not sure they should know aboutMovement
rather it should beMovement
knowing about the two components instead at least thats what my mind thinks is more logical unless I misunderstand what data you mean ?