【问题标题】:Is STD::Set what I should use to organize objects into a hierarchy?是 STD::Set 我应该使用什么来将对象组织成层次结构吗?
【发布时间】:2012-04-08 01:19:08
【问题描述】:

我想将我所有的游戏对象组织成一个层次结构。我认为一棵树是最好的主意。我考虑过使用 STD::Set 来处理这个问题。这有任何意义吗?基本上,一个游戏对象可以容纳可变数量的其他游戏对象。如果我确实以这种方式处理它,那么处理访问树中对象的最佳方法是什么?通过 ID 访问它们是否足够快?我想我也可以通过指针访问它们,但是如果你遇到有很多对象的情况,传递它们听起来有点危险和乏味。

如果有任何影响,我也会显示这些数据。

前:

-Hierarchy
    -GameObject
        -GameObject
        -Gameobject
    -GameObject
        -GameObject
            -GameObject
   -GameObject

感谢任何输入的家伙。谢谢。

【问题讨论】:

  • 是什么让你觉得std::set适合抱树?
  • 如果你不擅长指针,即使是 1 也是危险的。如果你对他们很好,1 和 100 没有区别。这句话我也可以通过指针访问它们,但是如果你遇到有很多对象的情况,传递它们听起来有点危险和乏味没有意义!

标签: c++ boost data-structures hierarchy


【解决方案1】:

我假设你想做这样的事情:

class GameObject {
    some_container<GameObject> nested_objects_;
    ....
}

如果您真的不需要O(log(n)) 在您的对象集合中搜索和删除,我建议您改用std::vector,这样您就可以避免内存开销和(很可能)一些CPU 开销。所以如果你只需要为每个对象存储几个嵌套对象,那么一定要选择std::vector

但如果您需要快速查找/删除对象或保持唯一性,那么std::set 可能是一个不错的选择。为了使用std::set,你需要提供一个比较函数,它就像一个&lt;操作符(严格来说,你的比较函数必须是strict weak ordering。)你可以为你定义operator &lt;GameObject类. std::set的效率取决于这个函数,所以这里要小心。

请注意,您可以通过提供GameObject 本身来遍历整个集合或find 单个GameObjects(严格来说,您可以提供其等效项-查看比较功能)。因此,如果您需要通过 id 获取对象,最好使用std::map,它更适合这种类型的工作。 std::set reference。 std::map reference

我还建议研究 boost::unordered_set 和 boost::unordered_map(或 std::unordered_set 和 std::unordered_map,如果你可以使用 C++11)。

【讨论】:

    【解决方案2】:

    每个“游戏对象”不能将游戏对象数组作为实例变量是否有原因?也许您的 main 应该创建一个 GameObjects 数组,并且这些游戏对象中的每一个都可以将正在进行的游戏对象保存在一个数组实例变量中。至少我是这么想的。

    【讨论】:

      【解决方案3】:

      我考虑过使用 STD::Set 来处理这个问题。这有意义吗?

      没有。 std::set 用于存储唯一对象的集合,其中对象使用用户提供的operator&lt; 或lessthan 比较器进行排序。没有理由将 std::set 用于对象列表。

      通过 ID 访问它们是否足够快?

      这就是问题所在。 std::set 中没有 IDs。有物体。因此,如果您使用 std::set,您将无法通过 ID 访问对象,而无需遍历整个集合(除非对象按 ID 排序,但这是另一回事)。要将某些内容映射到其他内容,请使用 std::map。 std::set 用于收藏。

      如果我确实以这种方式处理它,那么处理访问树中对象的最佳方法是什么?

      最好的方法可能是使用列表或双端队列 - 取决于您的游戏处理对象的方式。

      没有“最好”的方式来表示树木。

      1. 您可以将层次结构的根对象存储在列表中,并将子对象存储在父对象中。
      2. 您可以将所有对象存储在列表中(子对象和父对象),子对象存储对父对象的引用,然后确保“update()”函数考虑到这一点。
      3. 您可以存储两个列表 - 一个用于根对象,另一个用于所有对象。 (例如,您在第一个列表上调用更新,并使用第二个列表进行碰撞检测查询等)

      所有这些方法都有意义,并且对于某些场景很有用。

      但是传递那些周围听起来有些危险

      是的,当发射导弹的人被炸成碎片并且导弹指向所有者进行友军射击检查时,这肯定会导致游戏崩溃。但是,由于您的问题被标记为“提升”,您可以使用boost::weak_ptr 来处理这个问题。对象也可以作为shared_ptr 的列表存储在 std::list、std::deque 甚至 std::vector 中 - 取决于您的游戏将如何使用它们。

      【讨论】:

      • 作为替代方案,您可以让实体向任何听众发布一条消息,说他们正在被摧毁 - 在这种情况下,如果发射导弹的人被杀死,那么导弹会通过以下方式发出警报消息系统。
      • @StuartGolodetz:是的,有几种方法可以做到这一点。但是,我认为最好使用自动处理情况且不需要您编写任何内容的解决方案。除非现有库提供消息机制(并且“即将被销毁”通知是自动完成的),否则我认为最好使用类似weak_ptr的东西。此类通知的示例是QObject::destroyed() 信号,但它取决于应用程序 - 使所有内容 QObject 对于许多应用程序来说肯定是过度杀伤力。
      【解决方案4】:

      对于它的价值,我发现通过“路径”访问它们,有点像磁盘上的目录树,效果很好。这取自我正在编写的一些 C# 代码(请注意,我在下面的课程中遗漏了一些内容),但这个想法将直接转化为 C++:

      sealed class Entity : IEntity
      {
          private readonly IDictionary<string,IEntity> m_children = new Dictionary<string,IEntity>();
      
          private readonly IDictionary<string,dynamic> m_properties;
      
          public string Name { get { return m_properties["Name"]; } }
      
          public IEntity Parent { get; set; }
      
          /// <summary>
          /// Adds a child to this entity.
          /// </summary>
          /// <param name="child">The child to add.</param>
          public void AddChild(IEntity child)
          {
              m_children.Add(child.Name, child);
              child.Parent = this;
          }
      
          /// <summary>
          /// Gets the absolute path of this entity in its tree.
          /// </summary>
          /// <returns>The entity's absolute path.</returns>
          public string GetAbsolutePath()
          {
              IEntity cur = this;
              var path = new LinkedList<string>();
              while(cur.Parent != null)
              {
                  path.AddFirst(cur.Name);
                  cur = cur.Parent;
              }
              path.AddFirst(".");
              return string.Join("/", path);
          }
      
          /// <summary>
          /// Gets another entity in this entity's tree by its absolute path (i.e. its path relative to the root entity).
          /// </summary>
          /// <param name="path">The absolute path to the other entity.</param>
          /// <returns>The other entity, if found, or null otherwise.</returns>
          public IEntity GetEntityByAbsolutePath(string path)
          {
              return GetRootEntity().GetEntityByRelativePath(path);
          }
      
          /// <summary>
          /// Gets another entity in this entity's tree by its absolute path (i.e. its path relative to the root entity).
          /// </summary>
          /// <param name="path">The absolute path to the other entity, as a list of path components.</param>
          /// <returns>The other entity, if found, or null otherwise.</returns>
          public IEntity GetEntityByAbsolutePath(LinkedList<string> path)
          {
              return GetRootEntity().GetEntityByRelativePath(path);
          }
      
          /// <summary>
          /// Gets another entity in this entity's tree by its path relative to this entity.
          /// </summary>
          /// <param name="path">The relative path to the other entity.</param>
          /// <returns>The other entity, if found, or null otherwise.</returns>
          public IEntity GetEntityByRelativePath(string path)
          {
              return GetEntityByRelativePath(new LinkedList<string>(path.Split('/')));
          }
      
          /// <summary>
          /// Gets another entity in this entity's tree by its path relative to this entity.
          /// </summary>
          /// <param name="path">The relative path to the other entity, as a list of path components.</param>
          /// <returns>The other entity, if found, or null otherwise.</returns>
          public IEntity GetEntityByRelativePath(LinkedList<string> path)
          {
              IEntity cur = this;
      
              while(cur != null && path.Count != 0)
              {
                  switch(path.First())
                  {
                      case ".":
                          break;
                      case "..":
                          cur = cur.Parent;
                          break;
                      default:
                          cur = cur.GetChild(path.First());
                          break;
                  }
      
                  path.RemoveFirst();
              }
      
              return cur;
          }
      
          /// <summary>
          /// Gets the root entity of this entity's tree.
          /// </summary>
          /// <returns>The root entity of this entity's tree.</returns>
          private IEntity GetRootEntity()
          {
              IEntity cur = this;
              while(cur.Parent != null)
              {
                  cur = cur.Parent;
              }
              return cur;
          }
      }
      

      给定实体树中的任何实体,然后您可以通过它们的绝对或相对路径访问其他实体,例如

      cityA.GetEntityByRelativePath("../city:B/building:1");
      

      【讨论】:

      • 按照 3D 射击游戏中的逻辑,您必须为每个敌人甚至每个射弹提供唯一的名称。对我来说听起来像是“YAGNI”的案例。
      • @SigTerm:这是一款城市建设游戏,似乎在上下文中运行得相当好。我同意对于具有大量实体的 FPS 可能会有点傻 :) 顺便说一句,当您可以使用 GUID 时,提供唯一名称并不是很困难。
      • @SigTerm:“似乎工作正常”只要你的游戏不包括哥斯拉攻击和类似的城市毁灭灾难,它就会正常工作。如果城市的某些部分经常被破坏和再生,或者城市经常发生变化,(IMO)你很可能不得不处理可能的名称冲突,这可能会很混乱。另一方面,您将获得所有东西的街道地址,这可能对您的游戏有用。
      猜你喜欢
      • 2017-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-16
      • 1970-01-01
      • 2010-09-27
      • 1970-01-01
      相关资源
      最近更新 更多