BsGameObjectManager.h 5.9 KB

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