BsGameObjectManager.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsModule.h"
  4. #include "BsGameObject.h"
  5. namespace BansheeEngine
  6. {
  7. /**
  8. * @brief Tracks GameObject creation and destructions. Also resolves
  9. * GameObject references from GameObject handles.
  10. *
  11. * @note Sim thread only.
  12. */
  13. class BS_CORE_EXPORT GameObjectManager : public Module<GameObjectManager>
  14. {
  15. public:
  16. GameObjectManager();
  17. ~GameObjectManager();
  18. /**
  19. * @brief Registers a new GameObject and returns the handle to the object.
  20. */
  21. GameObjectHandleBase registerObject(const std::shared_ptr<GameObject>& object);
  22. /**
  23. * @brief Unregisters a GameObject.
  24. */
  25. void unregisterObject(const GameObjectHandleBase& object);
  26. /**
  27. * @brief Attempts to find a GameObject handle based on the GameObject instance ID.
  28. * Returns empty handle if ID cannot be found.
  29. */
  30. GameObjectHandleBase getObject(UINT64 id) const;
  31. /**
  32. * @brief Attempts to find a GameObject handle based on the GameObject instance ID.
  33. * Returns true if object with the specified ID is found, false otherwise.
  34. */
  35. bool tryGetObject(UINT64 id, GameObjectHandleBase& object) const;
  36. /**
  37. * @brief Checks if the GameObject with the specified instance ID exists.
  38. */
  39. bool objectExists(UINT64 id) const;
  40. /************************************************************************/
  41. /* DESERIALIZATION */
  42. /************************************************************************/
  43. // Note: GameObjects need a bit of special handling when it comes to deserialization,
  44. // which is what this part of the code is used for. It performs two main actions:
  45. // - 1. Resolves all GameObjectHandles on deserialization
  46. // - We can't just resolve them as we go because during deserialization not all objects
  47. // have necessarily been created.
  48. // - 2. Maps serialized IDs to actual in-engine ids.
  49. /**
  50. * @brief Needs to be called whenever GameObject deserialization starts. Must be followed
  51. * by endDeserialization call.
  52. */
  53. void startDeserialization();
  54. /**
  55. * @brief Needs to be called whenever GameObject deserialization ends. Must be preceded
  56. * by startDeserialization call.
  57. */
  58. void endDeserialization();
  59. /**
  60. * @brief Returns true if GameObject deserialization is currently in progress.
  61. */
  62. bool isGameObjectDeserializationActive() const { return mIsDeserializationActive; }
  63. /**
  64. * @brief Registers an id that was deserialized, and has been remapped to
  65. * an actual in-engine ID. This will be used when resolving GameObjectHandles
  66. * (since they might point to the invalid deserialized id).
  67. */
  68. void registerDeserializedId(UINT64 deserializedId, UINT64 actualId);
  69. /**
  70. * @brief Queues the specified handle and resolves it when deserialization ends.
  71. */
  72. void registerUnresolvedHandle(const GameObjectHandleBase& object);
  73. /**
  74. * @brief Registers a callback that will be triggered when GameObject serialization ends.
  75. */
  76. void registerOnDeserializationEndCallback(std::function<void()> callback);
  77. private:
  78. UINT64 mNextAvailableID; // 0 is not a valid ID
  79. Map<UINT64, GameObjectHandleBase> mObjects;
  80. GameObject* mActiveDeserializedObject;
  81. bool mIsDeserializationActive;
  82. Map<UINT64, UINT64> mIdMapping;
  83. Vector<GameObjectHandleBase> mUnresolvedHandles;
  84. Vector<std::function<void()>> mEndCallbacks;
  85. };
  86. }