BsGameObject.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsIReflectable.h"
  4. namespace BansheeEngine
  5. {
  6. /**
  7. * @brief Contains instance data that is held by all GameObject handles.
  8. */
  9. struct GameObjectInstanceData
  10. {
  11. GameObjectInstanceData()
  12. :mInstanceId(0), object(nullptr)
  13. { }
  14. std::shared_ptr<GameObject> object;
  15. UINT64 mInstanceId;
  16. };
  17. typedef std::shared_ptr<GameObjectInstanceData> GameObjectInstanceDataPtr;
  18. /**
  19. * @brief Type of object that can be referenced by a GameObject handle.
  20. * Each object has an unique ID and is registered with the GameObjectManager.
  21. */
  22. class BS_CORE_EXPORT GameObject : public IReflectable
  23. {
  24. public:
  25. GameObject();
  26. virtual ~GameObject();
  27. /**
  28. * @brief Returns the unique instance ID of the GameObject.
  29. */
  30. UINT64 getInstanceId() const { return mInstanceData->mInstanceId; }
  31. /**
  32. * @brief Returns an ID that identifies a link between this object and its equivalent
  33. * in the linked prefab. This will be -1 if the object has no prefab link, or if
  34. * the object is specific to the instance and has no prefab equivalent.
  35. */
  36. UINT32 getLinkId() const { return mLinkId; }
  37. /**
  38. * @brief Gets the name of the object.
  39. */
  40. const String& getName() const { return mName; }
  41. /**
  42. * @brief Sets the name of the object.
  43. */
  44. void setName(const String& name) { mName = name; }
  45. /**
  46. * @brief Marks the object as destroyed. Generally this means the object
  47. * has been queued for destruction but it hasn't occurred yet.
  48. *
  49. * @note Internal method.
  50. */
  51. void _setIsDestroyed() { mIsDestroyed = true; }
  52. /**
  53. * @brief Checks if the object has been destroyed.
  54. */
  55. bool _getIsDestroyed() const { return mIsDestroyed; }
  56. /**
  57. * @brief Changes the prefab link ID for this object. See ::getLinkId.
  58. *
  59. * @note Internal method.
  60. */
  61. void _setLinkId(UINT32 id) { mLinkId = id; }
  62. /**
  63. * @brief Replaces the instance data with another objects instance data.
  64. * This object will basically become the original owner of the provided
  65. * instance data as far as all game object handles referencing it are concerned.
  66. *
  67. * @note Internal method.
  68. * No alive objects should ever be sharing the same instance data. This can be used
  69. * for restoring dead handles.
  70. */
  71. virtual void _setInstanceData(GameObjectInstanceDataPtr& other);
  72. /**
  73. * @brief Returns instance data that identifies this GameObject and is used for referencing
  74. * by game object handles.
  75. *
  76. * @note Internal method.
  77. */
  78. virtual GameObjectInstanceDataPtr _getInstanceData() const { return mInstanceData; }
  79. protected:
  80. friend class GameObjectHandleBase;
  81. friend class GameObjectManager;
  82. friend class PrefabDiff;
  83. friend class PrefabUtility;
  84. /**
  85. * @brief Initializes the GameObject after construction.
  86. */
  87. void initialize(const std::shared_ptr<GameObject>& object, UINT64 instanceId);
  88. /**
  89. * @brief Destroys this object.
  90. *
  91. * @param [in] handle Game object handle to this object.
  92. * @param [in] immediate If true, the object will be deallocated and become unusable
  93. * right away. Otherwise the deallocation will be delayed to the end of
  94. * frame (preferred method).
  95. */
  96. virtual void destroyInternal(GameObjectHandleBase& handle, bool immediate = false) = 0;
  97. protected:
  98. String mName;
  99. UINT32 mLinkId;
  100. private:
  101. friend class Prefab;
  102. GameObjectInstanceDataPtr mInstanceData;
  103. bool mIsDestroyed;
  104. /************************************************************************/
  105. /* RTTI */
  106. /************************************************************************/
  107. public:
  108. friend class GameObjectRTTI;
  109. static RTTITypeBase* getRTTIStatic();
  110. virtual RTTITypeBase* getRTTI() const override;
  111. };
  112. }
  113. #include "BsGameObjectHandle.h"
  114. namespace BansheeEngine
  115. {
  116. // Game object handles
  117. typedef GameObjectHandle<GameObject> HGameObject;
  118. typedef GameObjectHandle<SceneObject> HSceneObject;
  119. typedef GameObjectHandle<Component> HComponent;
  120. }