BsGameObjectManager.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #pragma once
  5. #include "BsCorePrerequisites.h"
  6. #include "BsModule.h"
  7. #include "BsGameObject.h"
  8. namespace BansheeEngine
  9. {
  10. /**
  11. * @brief Tracks GameObject creation and destructions. Also resolves
  12. * GameObject references from GameObject handles.
  13. *
  14. * @note Sim thread only.
  15. */
  16. class BS_CORE_EXPORT GameObjectManager : public Module<GameObjectManager>
  17. {
  18. public:
  19. GameObjectManager();
  20. ~GameObjectManager();
  21. /**
  22. * @brief Registers a new GameObject and returns the handle to the object.
  23. */
  24. GameObjectHandleBase registerObject(const std::shared_ptr<GameObject>& object);
  25. /**
  26. * @brief Unregisters a GameObject.
  27. */
  28. void unregisterObject(const GameObjectHandleBase& object);
  29. /**
  30. * @brief Attempts to find a GameObject handle based on the GameObject instance ID.
  31. * Returns empty handle if ID cannot be found.
  32. */
  33. GameObjectHandleBase getObject(UINT64 id) const;
  34. /**
  35. * @brief Attempts to find a GameObject handle based on the GameObject instance ID.
  36. * Returns true if object with the specified ID is found, false otherwise.
  37. */
  38. bool tryGetObject(UINT64 id, GameObjectHandleBase& object) const;
  39. /**
  40. * @brief Checks if the GameObject with the specified instance ID exists.
  41. */
  42. bool objectExists(UINT64 id) const;
  43. /************************************************************************/
  44. /* DESERIALIZATION */
  45. /************************************************************************/
  46. // Note: GameObjects need a bit of special handling when it comes to deserialization,
  47. // which is what this part of the code is used for. It performs two main actions:
  48. // - 1. Resolves all GameObjectHandles on deserialization
  49. // - We can't just resolve them as we go because during deserialization not all objects
  50. // have necessarily been created.
  51. // - 2. Maps serialized IDs to actual in-engine ids.
  52. /**
  53. * @brief Needs to be called whenever GameObject deserialization starts. Must be followed
  54. * by endDeserialization call.
  55. */
  56. void startDeserialization();
  57. /**
  58. * @brief Needs to be called whenever GameObject deserialization ends. Must be preceded
  59. * by startDeserialization call.
  60. */
  61. void endDeserialization();
  62. /**
  63. * @brief Returns true if GameObject deserialization is currently in progress.
  64. */
  65. bool isGameObjectDeserializationActive() const { return mIsDeserializationActive; }
  66. /**
  67. * @brief Registers an id that was deserialized, and has been remapped to
  68. * an actual in-engine ID. This will be used when resolving GameObjectHandles
  69. * (since they might point to the invalid deserialized id).
  70. */
  71. void registerDeserializedId(UINT64 deserializedId, UINT64 actualId);
  72. /**
  73. * @brief Queues the specified handle and resolves it when deserialization ends.
  74. */
  75. void registerUnresolvedHandle(const GameObjectHandleBase& object);
  76. /**
  77. * @brief Registers a callback that will be triggered when GameObject serialization ends.
  78. */
  79. void registerOnDeserializationEndCallback(std::function<void()> callback);
  80. private:
  81. UINT64 mNextAvailableID; // 0 is not a valid ID
  82. Map<UINT64, GameObjectHandleBase> mObjects;
  83. GameObject* mActiveDeserializedObject;
  84. bool mIsDeserializationActive;
  85. Map<UINT64, UINT64> mIdMapping;
  86. Vector<GameObjectHandleBase> mUnresolvedHandles;
  87. Vector<std::function<void()>> mEndCallbacks;
  88. };
  89. }