anyone using Entity Component System for game development?

Hi,
I used to develop apps without engine and now that I am working on games, I decided to use cocos2d-x. Going over through APIs, I found CCComponent class and CCComponentContainer class were added in somewhere after v2.1.

I worked on flash games with PushButton Engine which used Entity Component System and I would like to use it for iOS development as well. I tried to find some documents on using these class but wasn’t able to find.

Does any of you use Entity Component system to develop app games?
Do you guys think component class in Cocos2D solid enough to power proper game development?

I am using Cocos2D-x 2.2.2 on xcode.

Thank you very much

Gene

Hi there

Im not maybe an expert in entiti component system but what we have in cocos2dx is useless. CCComponent onEnter and onExit doesn’t work like onEnter in CCNode. I created own component system example

ComponentNode< CCSprite >* pSprite = ComponentNode< CCSprite >::create();
pSprite->initWithFile(...);

pSprite->getComponentManager().addComponent(ClickComponent(...));

so with this simple code i have pSprite that i can click if i want animation i simply make

pSprite->getComponentManager().addComponent(BumpComponent(...));

My component system works form me i mean i can register callbacks for onEnter/onExit and everything i want also i don’t have with this any performance overhead.

For me cocos2dx component are simply bad written but this is my opinion.

Hi Dawid,

Thanks for the feeds. I better look into current CCComponent and start building my own.

Dawid Drozd wrote:

Im not maybe an expert in entiti component system but what we have in cocos2dx is useless. CCComponent onEnter and onExit doesn’t work like onEnter in CCNode. I created own component system example

I have some questions, if i may.

Are you implying onEnter and onExit creates overheads? If possible will I be able to view some of your Components?

Thanks!

Hi, I’m Ben Garney, the author of PBE.
I have occasionally thought about porting PBE to Cocos2DX. Would anyone be interested in using it if I did?

With overhead i mean that if some of my component don’t register for onEnter it doesn’t recive onEnter in cocos2dx it is done by virtual methods i use callbacks.

So example you have 100 components and you simply don;t need update,onEnter and other kind of methods. Bu in cocos2dx they will be executed because of CCComponent class.

I do such thing

void ClickComponent::initComponent ( ComponentManager& manager )
{
	ComponentListener& listener = manager.getComponentListener();
	listener.addOnTouchBeganCallback ( Callback2< bool ( CCTouch* ) > ( this,&ClickComponent::ccTouchBegan ), INT_MIN );
	listener.addOnTouchEndedCallback ( Callback2< void ( CCTouch* ) > ( this,
									   &ClickComponent::ccTouchEnded ) );
}

I use Callbacks that Jakub present’s here:http://www.google.com/url?q=http%3A%2F%2Fwww.cocos2d-x.org%2Fforums%2F6%2Ftopics%2F41358%3Fr%3D43554%23message-43554&sa=D&sntz=1&usg=AFQjCNGFRhGBq_zj4_14CNk8-PaQEbKmqw
I also write my own Notification center because i need something faster than comparing strings

So as you see my ClickComponent don’t listen for onEnter and other stuff. Ok this is microoptimization :slight_smile:

PS. i updated my 1 post i don’t add space in templates.

PS2. Also if you wan’t i can publish my component system on Github. Your opinion is welcome

Hello, Dawid
It would be great if you can publish component system on github. It will definitely help me get on right track!

Thank you!

Gene

Ben Garney wrote:

Hi, I’m Ben Garney, the author of PBE.
I have occasionally thought about porting PBE to Cocos2DX. Would anyone be interested in using it if I did?

Wow, Ben Garney, great to see you! *_*b
I will love really love to see PBE ported to Cocos2d-x. Really been fan of PBE, learned a lot and help a lot from making many components to testing/trying out new crazy behaviors.

If you do I will definitely use it for app development! Waiting first in line.

Thanks!

Gene.

Gene Han wrote:

Hello, Dawid
It would be great if you can publish component system on github. It will definitely help me get on right track!

Thank you!

Gene

Tomorrow i will give here link.

Awesome! Thank you so much in advance~!

Dawid Drozd wrote:

Gene Han wrote:

Hello, Dawid
It would be great if you can publish component system on github. It will definitely help me get on right track!

Thank you!

Gene

Tomorrow i will give here link.

@Ben Garney What is PBE?
@Dawid Drozd please do release the code + some short explanation / example
thanks!

here some stuff i was reading about this :

Sorry for the delay but i need to refactor some things and separate the code :slight_smile:
https://github.com/gelldur/cocos2dx-components all in readme

@Dawid Drozd
Looks Great! thanks.

By the way did you take the concepts from the Artemis framework ( the component part) ?
how its better then implementing for example c++ version of Artemis ?

Wow, Thank you Dawid Dorzd. I will check it out today and follow up!
thanks~

No i write 100% my self i don’t look at any other component system (i did what i need also i want good performance). Today i will add some tests and compares

Below i paste comparison Notifier with CCNotificationCenter (i use it a lot so this is why i rewrite it)

On PC (DEBUG) (AMD Phenom 2 x6)

avg1: 0µ,	avg2: 0µ,	min: 0µ,	max: 14µ,	total: 0.29s,	nr calls: 1000000 Notifier_1kk_notify_to_1_listener
avg1: 0µ,	avg2: 0µ,	min: 0µ,	max: 83µ,	total: 0.30s,	nr calls: 1000000 Notifier_1kk_notify_to_1_listener
avg1: 0µ,	avg2: 0µ,	min: 0µ,	max: 45µ,	total: 0.30s,	nr calls: 1000000 Notifier_1kk_notify_to_1_listener
avg1: 0µ,	avg2: 0µ,	min: 0µ,	max: 18µ,	total: 0.28s,	nr calls: 1000000 Notifier_1kk_notify_to_1_listener
avg1: 0µ,	avg2: 0µ,	min: 0µ,	max: 47µ,	total: 0.53s,	nr calls: 1000000 CCNotificationCenter_1kk_notify_to_1_listener
avg1: 0µ,	avg2: 0µ,	min: 0µ,	max: 69µ,	total: 0.54s,	nr calls: 1000000 CCNotificationCenter_1kk_notify_to_1_listener
avg1: 0µ,	avg2: 0µ,	min: 0µ,	max: 87µ,	total: 0.57s,	nr calls: 1000000 CCNotificationCenter_1kk_notify_to_1_listener
avg1: 0µ,	avg2: 0µ,	min: 0µ,	max: 41µ,	total: 0.52s,	nr calls: 1000000 CCNotificationCenter_1kk_notify_to_1_listener



avg1: 42µ,	avg2: 38µ,	min: 36µ,	max: 296µ,	total: 3.89s,	nr calls: 100000 Notifier_100k_notify_to_1k_listeners
avg1: 45µ,	avg2: 38µ,	min: 36µ,	max: 256µ,	total: 3.81s,	nr calls: 100000 Notifier_100k_notify_to_1k_listeners
avg1: 36µ,	avg2: 37µ,	min: 36µ,	max: 279µ,	total: 3.72s,	nr calls: 100000 Notifier_100k_notify_to_1k_listeners
avg1: 77µ,	avg2: 78µ,	min: 75µ,	max: 733µ,	total: 7.83s,	nr calls: 100000 CCNotificationCenter_100k_notify_to_1k_listeners
avg1: 79µ,	avg2: 79µ,	min: 74µ,	max: 124µ,	total: 7.91s,	nr calls: 100000 CCNotificationCenter_100k_notify_to_1k_listeners
avg1: 91µ,	avg2: 83µ,	min: 75µ,	max: 129µ,	total: 8.37s,	nr calls: 100000 CCNotificationCenter_100k_notify_to_1k_listeners



avg1: 9µ,	avg2: 9µ,	min: 9µ,	max: 277µ,	total: 0.98s,	nr calls: 100000 Notifier_100k_notify_to_1k_listener_multiple_notifications
avg1: 10µ,	avg2: 10µ,	min: 9µ,	max: 251µ,	total: 1.00s,	nr calls: 100000 Notifier_100k_notify_to_1k_listener_multiple_notifications
avg1: 54µ,	avg2: 54µ,	min: 50µ,	max: 101µ,	total: 5.46s,	nr calls: 100000 CCNotificationCenter_100k_notify_to_1k_listener_multiple_notifications
avg1: 55µ,	avg2: 54µ,	min: 50µ,	max: 133µ,	total: 5.47s,	nr calls: 100000 CCNotificationCenter_100k_notify_to_1k_listener_multiple_notifications

On PC (RELEASE) (AMD Phenom 2 x6)

avg1: 0µ,	avg2: 0µ,	min: 0µ,	max: 9µ,	total: 0.08s,	nr calls: 1000000 Notifier_1kk_notify_to_1_listener
avg1: 0µ,	avg2: 0µ,	min: 0µ,	max: 20µ,	total: 0.08s,	nr calls: 1000000 Notifier_1kk_notify_to_1_listener
avg1: 0µ,	avg2: 0µ,	min: 0µ,	max: 51µ,	total: 0.40s,	nr calls: 1000000 CCNotificationCenter_1kk_notify_to_1_listener
avg1: 0µ,	avg2: 0µ,	min: 0µ,	max: 49µ,	total: 0.41s,	nr calls: 1000000 CCNotificationCenter_1kk_notify_to_1_listener


avg1: 9µ,	avg2: 9µ,	min: 9µ,	max: 52µ,	total: 0.98s,	nr calls: 100000 Notifier_100k_notify_to_1k_listeners
avg1: 9µ,	avg2: 9µ,	min: 9µ,	max: 52µ,	total: 0.98s,	nr calls: 100000 Notifier_100k_notify_to_1k_listeners
avg1: 31µ,	avg2: 31µ,	min: 29µ,	max: 77µ,	total: 3.19s,	nr calls: 100000 CCNotificationCenter_100k_notify_to_1k_listeners
avg1: 38µ,	avg2: 37µ,	min: 29µ,	max: 92µ,	total: 3.78s,	nr calls: 100000 CCNotificationCenter_100k_notify_to_1k_listeners


avg1: 2µ,	avg2: 2µ,	min: 2µ,	max: 42µ,	total: 0.26s,	nr calls: 100000 Notifier_100k_notify_to_1k_listener_multiple_notifications
avg1: 2µ,	avg2: 2µ,	min: 2µ,	max: 42µ,	total: 0.26s,	nr calls: 100000 Notifier_100k_notify_to_1k_listener_multiple_notifications
avg1: 26µ,	avg2: 27µ,	min: 24µ,	max: 59µ,	total: 2.73s,	nr calls: 100000 CCNotificationCenter_100k_notify_to_1k_listener_multiple_notifications
avg1: 28µ,	avg2: 29µ,	min: 22µ,	max: 52µ,	total: 2.96s,	nr calls: 100000 CCNotificationCenter_100k_notify_to_1k_listener_multiple_notifications

@Ben
I don’t know what is PBE, but I googled it and I found this: https://github.com/PushButtonLabs/PushButtonEngine/

And I think we met at GDC 2013. We went out for lunch with Scott Lembcke (from Chipmunk).

But yes, we definitely need an entity component system in cocos2d-x.

Hi, Gene Han
I written a ECS demo based on cocos2d-x 3.0 alpha, you can find the source code at https://github.com/ElvisQin/EntitySystem
There is a presentation I made a speech at a Tech conference, you may find it with this link: http://elvisco.de/wp-content/uploads/2013/11/Entity-System.pdf

I fellowed the origin ECS idea, means every System has just one instance in the game. Every frame the system will find the Entities which own a certain Component, this make System keep dependent and will be automatic attach to the Entity. but this cause performance issue.

So basically we should make Entity own it’s System instance,like the component in Unity3D. this avoid the find operate in every system at every frame. But we should still keep the automatic attaching. I will keep work on it.

May it will helped. :slight_smile:

@Dawid Drozd
Hey, I took a look at your GitHub. I am new to Cocos2d.

I am wondering why in your Example Code section you name your variable “pSprite” ? Is it because in order to create a ComponentNode you must add a default type, which in that example is CCSprite? Or maybe more likely that you’re never going to have a game object that doesn’t include a CCSprite.

Or is it because Cocos2d doesn’t have the right inheritance setup to do something like create a base Component class, let’s call it CCComponent.
CCSprite inherits from CCComponent, along with TouchComponent, ClickComponent, etc. Then the code looks like:

ComponentNode<CCComponent>* pGameObject = ComponentNode<CCComponent>::create();

pGameObject->getComponentManager().addComponent ( CCSprite::createWithFile ( R::Sprite::GUI_BUTTON_GREEN_9 ) );
pGameObject->getComponentManager().addComponent ( TouchComponent::create() );
pGameObject->getComponentManager().addComponent ( StandardPressActionComponent::create() );
pGameObject->getComponentManager().addComponent ( ClickComponent::create ( Utils::makeCallback ( this, &AchievementRow::onSkipClickedCallback ) ) );

Hey Ricardo! I remember that lunch well. Chipmunk guys are cool and it was neat hearing your feedback.

Would it help if I started a new thread? If there’s interest/demand I’d love to collaborate on this, if there are existing great solutions then I will defer to them. :slight_smile:

@Elvis Qin
Thank you for sharing ECS demo :slight_smile: unfortunately I dont read Chinese, Im korean, although I will your code and check it out!

@Ben Garney
Yes, please start a new thread on this topic. I try to contribute more but being new to cocos2d is slowing me down.