BsGameObjectManager.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsModule.h"
  4. #include "BsGameObject.h"
  5. namespace BansheeEngine
  6. {
  7. /**
  8. * @brief Possible modes to use when deserializing games objects.
  9. */
  10. enum GameObjectHandleDeserializationMode
  11. {
  12. /** All handles will point to old ID that were restored from the deserialized file. */
  13. GODM_UseOriginalIds = 0x01,
  14. /** All handles will point to new IDs that were given to the deserialized GameObjects. */
  15. GODM_UseNewIds = 0x02,
  16. /** Handles pointing to GameObjects outside of the currently deserialized set
  17. will attempt to be restored in case those objects are still active. */
  18. GODM_RestoreExternal = 0x04,
  19. /** Handles pointing to GameObjects outside of the currently deserialized set
  20. will be broken. */
  21. GODM_BreakExternal = 0x08,
  22. /** Handles pointing to GameObjects that cannot be found will not be set to null. */
  23. GODM_KeepMissing = 0x10
  24. };
  25. /**
  26. * @brief Tracks GameObject creation and destructions. Also resolves
  27. * GameObject references from GameObject handles.
  28. *
  29. * @note Sim thread only.
  30. */
  31. class BS_CORE_EXPORT GameObjectManager : public Module<GameObjectManager>
  32. {
  33. public:
  34. GameObjectManager();
  35. ~GameObjectManager();
  36. /**
  37. * @brief Registers a new GameObject and returns the handle to the object.
  38. */
  39. GameObjectHandleBase registerObject(const std::shared_ptr<GameObject>& object);
  40. /**
  41. * @brief Unregisters a GameObject.
  42. */
  43. void unregisterObject(const GameObjectHandleBase& object);
  44. /**
  45. * @brief Attempts to find a GameObject handle based on the GameObject instance ID.
  46. * Returns empty handle if ID cannot be found.
  47. */
  48. GameObjectHandleBase getObject(UINT64 id) const;
  49. /**
  50. * @brief Attempts to find a GameObject handle based on the GameObject instance ID.
  51. * Returns true if object with the specified ID is found, false otherwise.
  52. */
  53. bool tryGetObject(UINT64 id, GameObjectHandleBase& object) const;
  54. /**
  55. * @brief Checks if the GameObject with the specified instance ID exists.
  56. */
  57. bool objectExists(UINT64 id) const;
  58. /**
  59. * @brief Changes the instance ID by which an object can be retrieved by.
  60. *
  61. * @note Caller is required to update the object itself with the new ID.
  62. */
  63. void remapId(UINT64 oldId, UINT64 newId);
  64. /**
  65. * @brief Queues the object to be destroyed at the end of a GameObject update cycle.
  66. */
  67. void queueForDestroy(const GameObjectHandleBase& object);
  68. /**
  69. * @brief Destroys any GameObjects that were queued for destruction.
  70. */
  71. void destroyQueuedObjects();
  72. /************************************************************************/
  73. /* DESERIALIZATION */
  74. /************************************************************************/
  75. // Note: GameObjects need a bit of special handling when it comes to deserialization,
  76. // which is what this part of the code is used for. It performs two main actions:
  77. // - 1. Resolves all GameObjectHandles on deserialization
  78. // - We can't just resolve them as we go because during deserialization not all objects
  79. // have necessarily been created.
  80. // - 2. Maps serialized IDs to actual in-engine ids.
  81. /**
  82. * @brief Needs to be called whenever GameObject deserialization starts. Must be followed
  83. * by endDeserialization call.
  84. */
  85. void startDeserialization();
  86. /**
  87. * @brief Needs to be called whenever GameObject deserialization ends. Must be preceded
  88. * by startDeserialization call.
  89. */
  90. void endDeserialization();
  91. /**
  92. * @brief Returns true if GameObject deserialization is currently in progress.
  93. */
  94. bool isGameObjectDeserializationActive() const { return mIsDeserializationActive; }
  95. /**
  96. * @brief Registers an id that was deserialized, and has been remapped to
  97. * an actual in-engine ID. This will be used when resolving GameObjectHandles
  98. * (since they might point to the invalid deserialized id).
  99. */
  100. void registerDeserializedId(UINT64 deserializedId, UINT64 actualId);
  101. /**
  102. * @brief Queues the specified handle and resolves it when deserialization ends.
  103. */
  104. void registerUnresolvedHandle(const GameObjectHandleBase& object);
  105. /**
  106. * @brief Registers a callback that will be triggered when GameObject serialization ends.
  107. */
  108. void registerOnDeserializationEndCallback(std::function<void()> callback);
  109. /**
  110. * @brief Changes the deserialization mode for any following GameObject handle.
  111. *
  112. * @param gameObjectDeserializationMode Mode that controls how are GameObjects handles resolved when being deserialized.
  113. */
  114. void setDeserializationMode(UINT32 gameObjectDeserializationMode);
  115. /**
  116. * @brief Attempts to update the ID of the provided handle by mapping its old ID to
  117. * the newly deserialized object and its new ID. Game object deserialization
  118. * must be active.
  119. */
  120. void resolveDeserializedHandle(GameObjectHandleBase& handle, UINT32 flags);
  121. /**
  122. * @brief Gets the currently active flags that control how are game object handles deserialized.
  123. */
  124. UINT32 getDeserializationFlags() const { return mGODeserializationMode; }
  125. private:
  126. UINT64 mNextAvailableID; // 0 is not a valid ID
  127. Map<UINT64, GameObjectHandleBase> mObjects;
  128. Map<UINT64, GameObjectHandleBase> mQueuedForDestroy;
  129. GameObject* mActiveDeserializedObject;
  130. bool mIsDeserializationActive;
  131. Map<UINT64, UINT64> mIdMapping;
  132. Vector<GameObjectHandleBase> mUnresolvedHandles;
  133. Vector<std::function<void()>> mEndCallbacks;
  134. UINT32 mGODeserializationMode;
  135. };
  136. }