基于组件的设计
假设您制作了一个游戏引擎,并且有多个 GameObjects
,并且每个 GameObject
都有一个可以添加或删除的组件列表。
假设有一个具有顶点、法线等的 MeshComponent
。如果多个 GameObjects
具有相同的 MeshComponent
,将会产生大量内存浪费。当然有很多方法可以实现这一点,但我想要一些好的建议如何解决这个问题?组件如何共享不会被修改的数据?
Lets say you make a game engine, and you have several GameObjects
and every GameObject
have a list of components that you can add or remove.
Lets say there is a MeshComponent
who has vertices, normals etc. If several GameObjects
have the same MeshComponent
, there will be a lot of memory waste. Of course there are many ways to implements this but I want some good advice how to solve this? How do components share data that is not going to be modified?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
C++ 没有静态类。
如果多个
GameObjects
具有相同的MeshComponent
,则不会有内存浪费。当然,因为它是相同的
MeshComponent
...如果您有许多概念上应该相同的
MeshComponent
副本,您将会浪费内存。如果许多
GameObjects
需要引用相同的MeshComponent
,那么它们都应该持有一个指向相同MeshComponent
的(智能)指针。C++ does not have static classes.
If several
GameObjects
have the sameMeshComponent
, there will be NO memory waste.Because, of course, it is the same
MeshComponent
...You will waste memory if you have many copies of the
MeshComponent
which conceptually should be identical.If many
GameObjects
need to refer to the sameMeshComponent
then they should each hold a (smart) pointer to the sameMeshComponent
.