Component Oriented Programming in C++ ( COP ) ?

Started by
23 comments, last by Kylotan 15 years, 9 months ago
Hello, A friend of mine mentioned a type of programming called COP or Component Oriented Programming. In C++, I typically use OOP can never heard of COP. My friend described COP as something along the lines of this His quote: "Well, sorta. COP uses these but the main idea is something like this (just an example, it won't necessarily look like this):"

CEntity *pPlayer = CEntityManager->AddEntity("Player"); // just an entity with name "Player"

 

pPlayer->AddComponent(CComponentFactory->CreatComponent("Position")); // player now has position (x, y, z)
pPlayer->AddComponent(CComponentFactory->CreatComponent("3DModel")); // player now has a mesh that can be shown on screen
pPlayer->AddComponent(CComponentFactory->CreatComponent("Body")); // player now has body for collision
pPlayer->AddComponent(CComponentFactory->CreatComponent("Run")); // player now can run (has speed and acceleration, etc...)
pPlayer->AddComponent(CComponentFactory->CreatComponent("Jump")); // player now can jump
pPlayer->AddComponent(CComponentFactory->CreatComponent("Attack")); // player now can attack
pPlayer->AddComponent(CComponentFactory->CreatComponent("Storage")); // player now can carry items
pPlayer->AddComponent(CComponentFactory->CreatComponent("Lock-On")); // player now can lock on to targets

pPlayer->AddComponent(CComponentFactory->CreatComponent("A.I.")); // player now can think (or for enemy)

pPlayer->AddComponent(CComponentFactory->CreatComponent("Input")); // player now can be controlled through keyboard and mouse

 

pPlayer->GetComponent("Position")->SetPosition(float x, float y, float z);
pPlayer->GetComponent("3DModel")->SetMesh(CMeshManager->LoadMesh("mesh.3ds"));

pPlayer->GetComponent("A.I.")->SetBehavior("FollowTarget", "NPC00125");

 
After this quote: "And so on and so forth :P As you can see with this system you can create any player, enemy, items, NPCs, and whatever and they can have any attributes and functions depending on the components they have (a tree that can talk, a flying sword that attacks, etc...). The game designers can go crazy with their ideas without having to worry about having a class for each type of entity they want to make (no more CPlayer, CEnemy, CItem, etc...)" So my question is this: how do you go about making something like this? Any kick-start answers or links to tutorials would be very helpful. Thank you for your time! ~Maverick
Holy crap, you can read!
Advertisement
I think this would be pretty easily implementable in C++ (and it's been done in Bay 12 Games's Dwarf Fortress). Create an extensible container for each object which contains some form of mnemonic token. At some point during run-time, your program reads through the tokens and performs whichever actions you want on it. The details, especially when and how you want the program to check for tokens, depends on what you need your software to do.
Thanks! I think I have an idea on how to go about this. Still any other input is greatly appreciated!
Holy crap, you can read!
Check out my project for an idea on how this can be achieved. My approach is container based wherein entities are containers of components. In my system, entities assimilate or expose aspects of their underlying components, where aspects include attributes, events, and operations.

I haven't moved code into the repository yet, so the wiki will have to do. Sorry.

As mentioned on the linked page, I used Boost.Any and Boost.Function to help achieve my design. You may want to look into those.

Others here have started threads with their (more expert) thoughts and designs. I'm sure someone can provide links.

Hope this helps!

EDIT:

I also used Boost.Signals. You can see the correlation between these libraries and the three types of aspects I mentioned.
That sounds like some fancy words for something which is basically just dynamic objects. It's a good approach.

Steps to implementing dynamic objects:

1) Every "object" has a hashtable. These are what your friend calls "entities".

2) Every element in the hashtable is an association from a string (its name) to some variant structure that contains the relevant data. These elements are what your friend calls "components"

To implement variant types, you can use boost::any or you can roll out your own system.
He's making terms up. All that he's doing is object composition, which is a very, very common idea in OOP.
It would be nice if you could just do what is done in the example, but tell me, how are you doing this:
pPlayer->GetComponent("Position")->SetPosition(float x, float y, float z);pPlayer->GetComponent("3DModel")->SetMesh(CMeshManager->LoadMesh("mesh.3ds"));pPlayer->GetComponent("A.I.")->SetBehavior("FollowTarget", "NPC00125");

without a typecast?

GetComponent() would have to return a type that has SetPosition(), SetMesh() , SetBehavior(), etc. as member functions. What's the use of those components if you need all the functionality in that type (returned by GetComponent())?

I've found this document. In the sample code of the document an "Object"(in Java) is returned, which also needs to be casted.



[Edited by - delta user on July 3, 2008 5:22:52 PM]
Someone who uses a, euhm..., delta!?
Quote:Original post by Zahlman
He's making terms up. All that he's doing is object composition, which is a very, very common idea in OOP.


Forgive me if I'm wrong, but I think there is a slight difference. Object composition is defined at compile time and Component oriented allows you to add components to entities at run time (Hence the "AddComponent" in the original code snippet. I think it's meant to lead to greater flexibility.)

Quote:Original post by delta user
without a typecast?

As far as I see you are right. If there is only one set function per component you could just have the function accept a boost::any.
Cowboy Programming - Evolve Your Hierarchy.

Steven Yau
[Blog] [Portfolio]

Yes, I know I'm going to need type-casting. I came across that half-way through writing it. ;p

I've come upon a stump though, so I'm going to check out the rest of the articles/links you all posted for me. Thank you all, again!
Holy crap, you can read!

This topic is closed to new replies.

Advertisement