BsGameObjectRTTI.h 3.1 KB

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