BsGameObjectManager.h 6.0 KB

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