BsGameObjectRTTI.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsRTTIType.h"
  4. #include "BsGameObject.h"
  5. #include "BsSceneObject.h"
  6. #include "BsGameObjectManager.h"
  7. namespace BansheeEngine
  8. {
  9. /** @cond RTTI */
  10. /** @addtogroup RTTI-Impl-Core
  11. * @{
  12. */
  13. /** Provides temporary storage for data used during GameObject deserialization. */
  14. struct GODeserializationData
  15. {
  16. GODeserializationData()
  17. :isDeserializationParent(false), originalId(0)
  18. { }
  19. GameObjectPtr ptr;
  20. bool isDeserializationParent;
  21. UINT64 originalId;
  22. Any moreData;
  23. };
  24. class BS_CORE_EXPORT GameObjectRTTI : public RTTIType<GameObject, IReflectable, GameObjectRTTI>
  25. {
  26. private:
  27. String& getName(GameObject* obj) { return obj->mName; }
  28. void setName(GameObject* obj, String& name) { obj->mName = name; }
  29. UINT64& getInstanceID(GameObject* obj) { return obj->mInstanceData->mInstanceId; }
  30. void setInstanceID(GameObject* obj, UINT64& instanceId)
  31. {
  32. // We record the ID for later use. Any child RTTI of GameObject must call GameObjectManager::registerObject
  33. // with this ID, so we know how to map deserialized GO handles to live objects, otherwise the handle
  34. // references will get broken.
  35. GameObject* go = static_cast<GameObject*>(obj);
  36. GODeserializationData& deserializationData = any_cast_ref<GODeserializationData>(go->mRTTIData);
  37. deserializationData.originalId = instanceId;
  38. }
  39. UINT32& getLinkId(GameObject* obj) { return obj->mLinkId; }
  40. void setLinkId(GameObject* obj, UINT32& linkId) { obj->mLinkId = linkId; }
  41. public:
  42. /**
  43. * @brief Helper method used for creating Component objects used during deserialization.
  44. */
  45. template <typename T>
  46. static std::shared_ptr<T> createGameObject()
  47. {
  48. SPtr<T> component = SceneObject::createEmptyComponent<T>();
  49. // Every GameObject must store GODeserializationData in its RTTI data field during deserialization
  50. component->mRTTIData = GODeserializationData();
  51. GODeserializationData& deserializationData = any_cast_ref<GODeserializationData>(component->mRTTIData);
  52. // Store shared pointer since the system only provides us with raw ones
  53. deserializationData.ptr = component;
  54. return component;
  55. }
  56. public:
  57. GameObjectRTTI()
  58. {
  59. addPlainField("mInstanceID", 0, &GameObjectRTTI::getInstanceID, &GameObjectRTTI::setInstanceID);
  60. addPlainField("mName", 1, &GameObjectRTTI::getName, &GameObjectRTTI::setName);
  61. addPlainField("mLinkId", 2, &GameObjectRTTI::getLinkId, &GameObjectRTTI::setLinkId);
  62. }
  63. const String& getRTTIName() override
  64. {
  65. static String name = "GameObject";
  66. return name;
  67. }
  68. UINT32 getRTTIId() override
  69. {
  70. return TID_GameObject;
  71. }
  72. std::shared_ptr<IReflectable> newRTTIObject() override
  73. {
  74. BS_EXCEPT(InternalErrorException, "Cannot instantiate an abstract class.");
  75. }
  76. };
  77. /** @} */
  78. /** @endcond */
  79. }