BsGameObjectManager.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. /* DESERIALIZATION */
  64. /************************************************************************/
  65. // Note: GameObjects need a bit of special handling when it comes to deserialization,
  66. // which is what this part of the code is used for. It performs two main actions:
  67. // - 1. Resolves all GameObjectHandles on deserialization
  68. // - We can't just resolve them as we go because during deserialization not all objects
  69. // have necessarily been created.
  70. // - 2. Maps serialized IDs to actual in-engine ids.
  71. /**
  72. * @brief Needs to be called whenever GameObject deserialization starts. Must be followed
  73. * by endDeserialization call.
  74. */
  75. void startDeserialization();
  76. /**
  77. * @brief Needs to be called whenever GameObject deserialization ends. Must be preceded
  78. * by startDeserialization call.
  79. */
  80. void endDeserialization();
  81. /**
  82. * @brief Returns true if GameObject deserialization is currently in progress.
  83. */
  84. bool isGameObjectDeserializationActive() const { return mIsDeserializationActive; }
  85. /**
  86. * @brief Registers an id that was deserialized, and has been remapped to
  87. * an actual in-engine ID. This will be used when resolving GameObjectHandles
  88. * (since they might point to the invalid deserialized id).
  89. */
  90. void registerDeserializedId(UINT64 deserializedId, UINT64 actualId);
  91. /**
  92. * @brief Queues the specified handle and resolves it when deserialization ends.
  93. */
  94. void registerUnresolvedHandle(const GameObjectHandleBase& object);
  95. /**
  96. * @brief Registers a callback that will be triggered when GameObject serialization ends.
  97. */
  98. void registerOnDeserializationEndCallback(std::function<void()> callback);
  99. /**
  100. * @brief Changes the deserialization mode for any following GameObject handle.
  101. *
  102. * @param gameObjectDeserializationMode Mode that controls how are GameObjects handles resolved when being deserialized.
  103. */
  104. void setDeserializationMode(UINT32 gameObjectDeserializationMode);
  105. private:
  106. UINT64 mNextAvailableID; // 0 is not a valid ID
  107. Map<UINT64, GameObjectHandleBase> mObjects;
  108. GameObject* mActiveDeserializedObject;
  109. bool mIsDeserializationActive;
  110. Map<UINT64, UINT64> mIdMapping;
  111. Vector<GameObjectHandleBase> mUnresolvedHandles;
  112. Vector<std::function<void()>> mEndCallbacks;
  113. UINT32 mGODeserializationMode;
  114. };
  115. }