有一个实例化的派生类将指向其自身的指针放入数组中吗?
好吧,这是一个真正混乱的问题 - 我什至不完全知道要搜索什么。
我在这里问了一个与游戏实体处理系统相关的问题: 使用特定变量值启动派生类
到目前为止,这对我来说效果很好,但有一点是这样的。我希望生物不只是互相碰撞,而是互动。
如何通过坐标查找派生类的特定实例?
例如,找到位于 22,22 的 baseObject:Enemy() 并从其中获取“nType”的值
想到的是将某种指向数组中实例的指针,并在所述实体移动时移动它...但是如何使派生类将指向其自身的指针添加到数组?然后我如何从该实例的变量中提取一些内容?
呼。希望这是有道理的。
Okay so here's a real mess of a question - I don't even entirely know what to search for.
I asked a question here, related to a game's entity handling system: Initiating a derived class with specific variable values
So far, that's working out great for me, but for one thing. I want to have mobs not just collide with eachother, but interact.
How can I look up a specific instance of the derived class, by coordinates?
For example, find the baseObject:Enemy() located at 22,22 and get the value of "nType" from within it
What comes to mind is putting some kind of pointer to an instance in an array, and moving it when said entity moves... but how do I make a derived class add a pointer to itself to an array? and how do I then pull something from that instance's variables?
Whew. Hope this makes sense.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,我认为解决这个问题的方法是使用委托/事件传递。
假设你有一个暴民,正如之前有人所说的,你可以有一个“暴徒观察者”对象。这个想法是,当 GameEntity 是生物的一部分时,该实体订阅“生物观察者”。如果该实体离开生物,它会让“生物观察者”知道(取消订阅)。
因此,当您需要知道谁在组成暴民时,您可以向暴民观察者询问“暴民列表”,如果您需要按实体位置搜索,您可以自己通过组成暴民的实体列表进行操作,并且找到“感兴趣的位置”中的那个。
如果你的怪物很大,你可以向你的怪物观察器添加某种空间哈希功能,这样你就可以轻松过滤并询问“位于 gameGrid[10][13] 中的怪物”。
如果您使用事件传递,这非常酷,因为如果您想在生物之间转发消息,可以使用向生物观察者发送事件,然后将上述事件转发到订阅的实体。
如果您使用委托,它的工作方式类似。
观察者模式
委托模式
Well, I think a solution to this problem is to use delegates/event passing.
Let's say you have a mob, as someone said earlier you could have a "mob watcher" object. The idea is that when a GameEntity is part of a mob, that entity subscribes to the "mob watcher". If that entity leaves the mob, it will let the "mob watcher" know about it (unsubscribe).
So, when you need to know, who's composing a mob, you could just ask the mob watcher for the "mob list", if you need to search by Entity position you could then work yourself through the list of entities composing the mob, and find the one in the "position of interest".
If your mobs are gigantic you could add some kind of spatial hash feature to your mob watcher, so you could easily filter and ask for "guys in the mob which are located in gameGrid[10][13]".
If you use event passing, it's quite cool because if when you want to forward messages between the mob, sending an event to the mob watcher could be used to then forward the aforementioned event to the subscribed entities.
If you use delegates, it works in a similar way.
Observer Pattern
Delegate Pattern
好吧,您提出的具体问题很容易回答:伪变量 this 在所有成员函数中都可用;它指向当前对象。
“通过坐标查找”部分比较棘手;您可以考虑使用八叉树结构来组织您的对象(Google 是您的朋友。)
Well, the specific question you're asking is easy to answer: the pseudo-variable this is available in all member functions; it points to the current object.
The "looking up by coordinates" part is trickier; you might consider using an octree structure to organize your objects (Google is your friend there.)
您将需要创建一个新对象,其工作是跟踪“生物”的位置。根据你的问题,这个新对象将需要至少包含一个 std::vector ,它包含一个指向世界上每个“暴徒”的指针,以及一个成员函数,例如:
不幸的是,你在问题中要求的可能是不是你想要的。您可能希望获得某个点附近的所有“小怪”:
...但是返回 std::vector 可能非常慢,因此您可能需要一个数组,但必须仔细调整数组的大小,现在我们'到目前为止,这远远超出了您问题中提供的信息量。
You will need to make a new object whose job is to keep track of where the "mobs" are. According to your question, this new object will need to contain at least a std::vector which contains a pointer to each "mob" in the world, and also a member function like:
Unfortunately, what you asked for in your question is probably not what you want. You probably want to get all of the "mobs" near a point:
...but returning a std::vector can be very slow, so you probably want an array instead, but the array must be sized carefully, and now we're far far beyond the amount of information provided in your question so far.