As @Packora mentioned in the thread, cocos2d-x is very hierarchical, it makes cocos2d-x hard to extend. So we want to add Entity Component System(ECS) or similar mechanism in cocos2d-x to make it extendable.
I am doing researching on ECS and trying to add it in cocos2d-x. I will discuss and post progress in this thread. You are appreciated if you have any suggestion.
There is already some discussion in this thread. So please read this thread if you want to join the discussion.
GKEntity is a container of components. It has a function named
updateWithDeltaTime:, this function will invoke each component’s
updateWithDeltaTime:. It provides functions to add/remove/find a component.
GKComponent will holds a reference to its entity, can use it to find other components. It only has a function named
updateWithDeltaTime:. This function will be invoked by
GKComponentSystemto do something in every period duration.
- GKComponentSystem is to invoke registered components without looping GKEntitys. It can add a component by a component instance or by an entity instance.
- Should use
GKCompoent's update function.
- It also provide some help classes, such as State Machines, Pathfinding, and Rule System. Detail information please refer to this thread.
- It can be used with any rendering system, like SpriteKit or SceneKit or any other, but see below.
- There are no callbacks / messages between different components, or at least not in the current version. But perhaps it uses SpriteKit events??
- Entity: GameObject is the entity. It can find a component, but there is not way to remove a component.
- Component: Almost all components are added to an entity through editor. Script is a special component, can use it to communicate with other components.
- There is no obvious systems. It seems systems are implemented internally, and can take effect as needed. For example, when a RigidBody is added into a
GameObject, then it can do physical simulation automatically.
- There is not
GameObject. But there are many call back functions in script component(inherits from MonoBehaviour, such as:
- All component controlling codes will be in a registered script, because only the script can receive messages.
- It is not easy to add a new component that is not provided by Unity.
- All script will be looped many times no matter they need to be updated or not, because these functions will be invoked every frame or every fixed period:
- Update: will be invoked every frame
- FixedUpdate: will be invoked in fixed frame rate
- LateUpdate: will be invoked after
I am not familiar with Unity, please correct me if there is something wrong.
- Entity: entity is just an id. It doesn’t contain components. EntityManager record the relationship of entity and components. It has come helper functions of:
- create a component and associate with the component to entity
- find a component
- remove a component
- Component: component doesn’t contain anything. Developer should subclass it and it is better to just include data. Most time developers deal with ComponentHandler.
- EntityManager: It records the relationship of entity and component. And it provides functions to find components.
- System: just a base class. subclass should implement update function.
- SystemManager: will loop all added systems.
- EventSystem: can use it to subscribe new event type and emit events.
- It is a framework of ECS, developers should implement all needed systems. For example, should implement rendering system, physics system, audio system.
- Entity: Actor is the entity. It is a container of Components. It is a complex class, it includes many variables and many functions. InputComponent and RootComponent is two variables that can be accessed directly. InputComponent is intialized internally; RootComponent should be assigned outside, and it holds the transform information of the Actor. It has a
Tick()function, which will be invoked every frame.
Component: component can be created by Editor or by code. If a component is created by code, then we can continue to edit it in editor. As the documents shows, we can create a
StaticMeshComponentcomponent in c++, and assign its
Static Meshin editor. And a component can be attached to another component no matter they are belongs to the same Actor or not. Their transform is relatives to the component it attachs to. Actor’s transform is the same as its
- System: there is not obvious system that developers should notice. When a coomponent is added into an Actor, the component takes effects automatically. For example, when a
StaticMeshComponentis added into an Actor, then the Actor will be visable.
- Message: As the document shows, developers should keep reference of a component if it wants to use it in future. But Actor provides some functions to find a component, such as AActor::GetComponentsByTag and AActor::GetComponentsByClass.
The component system is not as pure as Unity. Some concept is treated as component, such as physics, but others are not, such as network. I think it is a duration that it towards to more pure component system. But it is more valuable for cocos2d-x because we are on the road too.