BsGameObjectRTTI.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. /** Helper method used for creating Component objects used during deserialization. */
  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. return nullptr;
  76. }
  77. };
  78. /** @} */
  79. /** @endcond */
  80. }