BsGameObject.h 4.0 KB

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