BsGameObjectManager.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. };
  23. /**
  24. * @brief Tracks GameObject creation and destructions. Also resolves
  25. * GameObject references from GameObject handles.
  26. *
  27. * @note Sim thread only.
  28. */
  29. class BS_CORE_EXPORT GameObjectManager : public Module<GameObjectManager>
  30. {
  31. public:
  32. GameObjectManager();
  33. ~GameObjectManager();
  34. /**
  35. * @brief Registers a new GameObject and returns the handle to the object.
  36. */
  37. GameObjectHandleBase registerObject(const std::shared_ptr<GameObject>& object);
  38. /**
  39. * @brief Unregisters a GameObject.
  40. */
  41. void unregisterObject(const GameObjectHandleBase& object);
  42. /**
  43. * @brief Attempts to find a GameObject handle based on the GameObject instance ID.
  44. * Returns empty handle if ID cannot be found.
  45. */
  46. GameObjectHandleBase getObject(UINT64 id) const;
  47. /**
  48. * @brief Attempts to find a GameObject handle based on the GameObject instance ID.
  49. * Returns true if object with the specified ID is found, false otherwise.
  50. */
  51. bool tryGetObject(UINT64 id, GameObjectHandleBase& object) const;
  52. /**
  53. * @brief Checks if the GameObject with the specified instance ID exists.
  54. */
  55. bool objectExists(UINT64 id) const;
  56. /**
  57. * @brief Changes the instance ID by which an object can be retrieved by.
  58. *
  59. * @note Caller is required to update the object itself with the new ID.
  60. */
  61. void remapId(UINT64 oldId, UINT64 newId);
  62. /**
  63. * @brief Queues the object to be destroyed at the end of a GameObject update cycle.
  64. */
  65. void queueForDestroy(const GameObjectHandleBase& object);
  66. /**
  67. * @brief Destroys any GameObjects that were queued for destruction.
  68. */
  69. void destroyQueuedObjects();
  70. /************************************************************************/
  71. /* DESERIALIZATION */
  72. /************************************************************************/
  73. // Note: GameObjects need a bit of special handling when it comes to deserialization,
  74. // which is what this part of the code is used for. It performs two main actions:
  75. // - 1. Resolves all GameObjectHandles on deserialization
  76. // - We can't just resolve them as we go because during deserialization not all objects
  77. // have necessarily been created.
  78. // - 2. Maps serialized IDs to actual in-engine ids.
  79. /**
  80. * @brief Needs to be called whenever GameObject deserialization starts. Must be followed
  81. * by endDeserialization call.
  82. */
  83. void startDeserialization();
  84. /**
  85. * @brief Needs to be called whenever GameObject deserialization ends. Must be preceded
  86. * by startDeserialization call.
  87. */
  88. void endDeserialization();
  89. /**
  90. * @brief Returns true if GameObject deserialization is currently in progress.
  91. */
  92. bool isGameObjectDeserializationActive() const { return mIsDeserializationActive; }
  93. /**
  94. * @brief Registers an id that was deserialized, and has been remapped to
  95. * an actual in-engine ID. This will be used when resolving GameObjectHandles
  96. * (since they might point to the invalid deserialized id).
  97. */
  98. void registerDeserializedId(UINT64 deserializedId, UINT64 actualId);
  99. /**
  100. * @brief Queues the specified handle and resolves it when deserialization ends.
  101. */
  102. void registerUnresolvedHandle(const GameObjectHandleBase& object);
  103. /**
  104. * @brief Registers a callback that will be triggered when GameObject serialization ends.
  105. */
  106. void registerOnDeserializationEndCallback(std::function<void()> callback);
  107. /**
  108. * @brief Changes the deserialization mode for any following GameObject handle.
  109. *
  110. * @param gameObjectDeserializationMode Mode that controls how are GameObjects handles resolved when being deserialized.
  111. */
  112. void setDeserializationMode(UINT32 gameObjectDeserializationMode);
  113. private:
  114. UINT64 mNextAvailableID; // 0 is not a valid ID
  115. Map<UINT64, GameObjectHandleBase> mObjects;
  116. Map<UINT64, GameObjectHandleBase> mQueuedForDestroy;
  117. GameObject* mActiveDeserializedObject;
  118. bool mIsDeserializationActive;
  119. Map<UINT64, UINT64> mIdMapping;
  120. Vector<GameObjectHandleBase> mUnresolvedHandles;
  121. Vector<std::function<void()>> mEndCallbacks;
  122. UINT32 mGODeserializationMode;
  123. };
  124. }