Hello there,
The first thing I want to say: The book (got the 4th edition) is really great. I have it for 2 weeks now, currently reading chapter 10. I marked everything what I want to find quickly again by sticking little notes at the pages and write some short note on it.
Furhtermore I can say: The book is written easy enough for a foreigner to understand (I'm german) but not on a too low level so it would be boring. Really great work.
So now I was curious about the source code, because I had some trouble in the past especially with component based architecture.
I tried to write a Pong (I am using Java and Slick2D/LWJGL) with a component based approach. So I had some components, like position, transformation, movement, physics, render etc.
And for every component which needed a system, there was a system. Like one system for move an object around. One system for calculating physics etc.
So the components themselves did only contain data and had no real functions than getter/setter and constructors.
The systems iterated through every actor, asking if he has the components the system needs and then working on these components.
For example:
The physics system should manage the ball to reflect from walls and from the paddles.
Furhtermore the physics system should manage the paddles to stop moving when touching a wall.
So the first problem was to distinguish between the ball and the paddles.
Then the system had to differ between a collision between the ball and a paddle and the ball and a wall because they needed different calculations.
So somehow the system had to now which actor was a paddle and which was a wall.
But in my opinion this cannot be a good solution as a CBS should be modular and distinguishing between object types this way cannot be really modular.
Also I had some headache because of the following reason: I wanted to implement some powerups, for example one which made the ball reflecting from the wall and the paddles in some strange way, for example coming back really fast or reflecting in a actual wrong angle.
But this made the physics system just more messy. Because now I had one big if-statement which checked if this powerup was active, and if yes there was a totally different physics calculation which looked nearly as the one without powerup but with some other values.
So now I opened the code of the book and looked for the code of the CBS and... it helped nothing. I see that there is a different approach, so the code uses no systems to operate on the components, but the components do everything themselves.
In my opinion this changes nothing, because for example the physics component would have the same problem as my physics system: Is it attached to the ball or a paddle? If yes, how should it behave?
Is my approach wrong?
Should there be a standard physics system for movement and then two different physics systems/components, one for the paddle and one for the ball?
Also I have problems like this: If there are components which add additional abilities to an actor, then the AI should now about these components. How would you tell the AI which components its actor has and how to use them? This is surely no problem if there are some standard components like moving and shooting.
But the CBS approach is said to be very modular and the components should just be modules which add new functionality or remove them from an actor if they are removed. So for example one AI controlled actor only can walk, but another AI controlled actor has a 'fly' component - How should you tell the AI and how could the AI use this?
Just iterating through a huge list of possible components, checking which are there and then starting some AI routine depending on the world state and the attached components seems not very modular to me.
I would be happy to see some answers to this problem as it is a big problem for me. In general, it is a big problem for me to add functionality into a game (not only with CBS approach but as well with classical inheritance) without ending up in a huge mess where I have strange and big dependencies.
Greetings,
M0rgenstern
PS: Sorry if my english is not so great.
The first thing I want to say: The book (got the 4th edition) is really great. I have it for 2 weeks now, currently reading chapter 10. I marked everything what I want to find quickly again by sticking little notes at the pages and write some short note on it.
Furhtermore I can say: The book is written easy enough for a foreigner to understand (I'm german) but not on a too low level so it would be boring. Really great work.
So now I was curious about the source code, because I had some trouble in the past especially with component based architecture.
I tried to write a Pong (I am using Java and Slick2D/LWJGL) with a component based approach. So I had some components, like position, transformation, movement, physics, render etc.
And for every component which needed a system, there was a system. Like one system for move an object around. One system for calculating physics etc.
So the components themselves did only contain data and had no real functions than getter/setter and constructors.
The systems iterated through every actor, asking if he has the components the system needs and then working on these components.
For example:
The physics system should manage the ball to reflect from walls and from the paddles.
Furhtermore the physics system should manage the paddles to stop moving when touching a wall.
So the first problem was to distinguish between the ball and the paddles.
Then the system had to differ between a collision between the ball and a paddle and the ball and a wall because they needed different calculations.
So somehow the system had to now which actor was a paddle and which was a wall.
But in my opinion this cannot be a good solution as a CBS should be modular and distinguishing between object types this way cannot be really modular.
Also I had some headache because of the following reason: I wanted to implement some powerups, for example one which made the ball reflecting from the wall and the paddles in some strange way, for example coming back really fast or reflecting in a actual wrong angle.
But this made the physics system just more messy. Because now I had one big if-statement which checked if this powerup was active, and if yes there was a totally different physics calculation which looked nearly as the one without powerup but with some other values.
So now I opened the code of the book and looked for the code of the CBS and... it helped nothing. I see that there is a different approach, so the code uses no systems to operate on the components, but the components do everything themselves.
In my opinion this changes nothing, because for example the physics component would have the same problem as my physics system: Is it attached to the ball or a paddle? If yes, how should it behave?
Is my approach wrong?
Should there be a standard physics system for movement and then two different physics systems/components, one for the paddle and one for the ball?
Also I have problems like this: If there are components which add additional abilities to an actor, then the AI should now about these components. How would you tell the AI which components its actor has and how to use them? This is surely no problem if there are some standard components like moving and shooting.
But the CBS approach is said to be very modular and the components should just be modules which add new functionality or remove them from an actor if they are removed. So for example one AI controlled actor only can walk, but another AI controlled actor has a 'fly' component - How should you tell the AI and how could the AI use this?
Just iterating through a huge list of possible components, checking which are there and then starting some AI routine depending on the world state and the attached components seems not very modular to me.
I would be happy to see some answers to this problem as it is a big problem for me. In general, it is a big problem for me to add functionality into a game (not only with CBS approach but as well with classical inheritance) without ending up in a huge mess where I have strange and big dependencies.
Greetings,
M0rgenstern
PS: Sorry if my english is not so great.