游戏开发中的基于组件的架构
我一直在考虑尝试基于组件的游戏开发架构。我已经阅读了一些有关它的博客文章和文章,但我还有一些事情尚未解决。当我说基于组件时,我的意思是您可以将组件添加到 ComponentManager,该组件管理器会更新其列表中的所有组件。我想尝试这样做以避免获得继承它们不需要的变量和函数的类。我真的很喜欢这样的想法:拥有一个非常简单的实体类,其中有许多组件可以并行工作而不会变得臃肿。此外,在运行时很容易删除和添加功能,这使得它非常酷。
这就是我的目标。
// this is what the setup could be like
entity.componentManager.add(new RigidBody(3.0, 12.0));
entity.componentManager.add(new CrazyMagneticForce(3.0));
entity.componentManager.add(new DrunkAffection(42.0, 3.0));
// the game loop updates the component manager which updates all components
entity.componentManager.update(deltaTime);
通信:某些组件需要与其他组件通信
我不能依赖这些组件来自我维持。有时他们需要与其他组件进行通信。我该如何解决这个问题?在 Unity 3D 中,您可以使用 GetComponent()
访问组件。
我正在考虑这样做,但是如果你有两个相同类型的组件会发生什么?也许你会得到一个 Vector。
var someComponent : RigidBody = _componentManager.getComponent(RigidBody);
优先级:某些组件需要先于其他组件更新
某些组件需要先于其他组件更新才能获取当前游戏循环的正确数据。我正在考虑为每个组件添加优先级,但我不确定这是否足够。 Unity 使用 LateUpdate 和 Update,但也许有一种方法可以更好地控制执行顺序。
嗯,就是这样。如果您有任何想法,请随时发表评论。或者,如果您有任何关于此的好文章或博客,我会很乐意看一下。谢谢。
http://cowboyprogramming.com/2007/01/05/evolve-your-heirachy/
编辑: 我的问题实际上是如何解决组件之间的优先级和通信问题。
I have been thinking of trying out component based architecture for game development. I have read some blog posts and articles about it but I have a few things I have not sorted out yet. When I say component based I mean that you can add components to a ComponentManager that updates all components in its list. I want to try this to avoid getting classes that inherits variables and functions that they don´t need. I really like the idea of having an really simple entity class with a lot of components working side by side without getting bloated. Also, it is easy to remove and add functionality at runtime wich makes it really cool.
This is what I am aiming for.
// this is what the setup could be like
entity.componentManager.add(new RigidBody(3.0, 12.0));
entity.componentManager.add(new CrazyMagneticForce(3.0));
entity.componentManager.add(new DrunkAffection(42.0, 3.0));
// the game loop updates the component manager which updates all components
entity.componentManager.update(deltaTime);
Communication: Some of the components need to communicate with other components
I can´t rely on the components to be self-sustaining. Sometime they will need to communicate with the other components. How do I solve this? In Unity 3D, you can access the components using GetComponent()
.
I was thinking of doing it like this, but what happens if you have two components of the same type? Maybe you get a Vector back.
var someComponent : RigidBody = _componentManager.getComponent(RigidBody);
Priority: Some components need to update before others
Some components need to update before others to get the correct data for the current game loop. I am thinking of adding a PRIORITY to each component, but I am not sure that this is enough. Unity uses LateUpdate and Update but maybe there is a way to get an even better control of the order of execution.
Well, thats it. If you have any thoughts don´t hesitate to leave a comment. Or, if you have any good articles or blogs about this I will gladly take a look at them. Thanks.
Component based game engine design
http://cowboyprogramming.com/2007/01/05/evolve-your-heirachy/
EDIT:
My question is really how to solve the issue with priorities and communication between the components.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我选择了 RDMBS 组件实体系统 http://entity-systems .wikidot.com/rdbms-with-code-in-systems#objc
到目前为止,它运行得非常好。每个组件只保存数据,没有方法。然后我有处理组件并完成实际工作的子系统。有时一个子系统需要与另一个子系统通信,为此我使用服务定位器模式 http://gameprogrammingpatterns.com/ service-locator.html
至于优先级。每个系统都在我的主游戏循环中进行处理,因此这只是先处理哪个系统的问题。因此,对于我的程序,我先处理控制,然后处理物理,然后处理相机,最后处理渲染。
I settled for an RDMBS component entity system http://entity-systems.wikidot.com/rdbms-with-code-in-systems#objc
It is so far working really well. Each component only holds data and has no methods. Then I have subsystems that process the components and do the actual work. Sometimes a subsystem needs to talk to another and for that I use a service locator pattern http://gameprogrammingpatterns.com/service-locator.html
As for priorities. Each system is processed in my main game loop and so it is just a matter of which gets processed first. So for mine I process control, then physics, then camera and finally render.