BsScriptGameObjectManager.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #pragma once
  2. #include "BsScriptEnginePrerequisites.h"
  3. #include "BsModule.h"
  4. #include <mono/jit/jit.h>
  5. namespace BansheeEngine
  6. {
  7. /**
  8. * @brief Manages all active GameObject interop objects. GameObjects can be created from native
  9. * code and used in managed code therefore we need to keep a dictionary or all the native
  10. * objects we have mapped to managed objects.
  11. */
  12. class BS_SCR_BE_EXPORT ScriptGameObjectManager : public Module<ScriptGameObjectManager>
  13. {
  14. /**
  15. * @brief Contains information about a single interop object containing a game object.
  16. */
  17. struct ScriptGameObjectEntry
  18. {
  19. ScriptGameObjectEntry();
  20. ScriptGameObjectEntry(ScriptGameObjectBase* instance, bool isComponent);
  21. ScriptGameObjectBase* instance;
  22. bool isComponent;
  23. };
  24. public:
  25. ScriptGameObjectManager();
  26. ~ScriptGameObjectManager();
  27. /**
  28. * @brief Attempts to find the interop object for the specified SceneObject. If one cannot be found
  29. * new one is created and returned.
  30. */
  31. ScriptSceneObject* getOrCreateScriptSceneObject(const HSceneObject& sceneObject);
  32. /**
  33. * @brief Creates a new interop object for the specified SceneObject. Throws an exception if one
  34. * already exists.
  35. */
  36. ScriptSceneObject* createScriptSceneObject(const HSceneObject& sceneObject);
  37. /**
  38. * @brief Connects an existing managed SceneObject instance with the native SceneObject by creating
  39. * the interop object. Throws an exception if the interop object already exists.
  40. */
  41. ScriptSceneObject* createScriptSceneObject(MonoObject* existingInstance, const HSceneObject& sceneObject);
  42. /**
  43. * @brief Connects an existing managed Component instance with the native Component by creating
  44. * the interop object. Throws an exception if the interop object already exists.
  45. */
  46. ScriptComponent* createScriptComponent(MonoObject* existingInstance, const GameObjectHandle<ManagedComponent>& component);
  47. /**
  48. * @brief Attempts to find the interop object for the specified managed component.
  49. * If one cannot be found null is returned.
  50. */
  51. ScriptComponent* getScriptComponent(const GameObjectHandle<ManagedComponent>& component) const;
  52. /**
  53. * @brief Attempts to find the interop object for a managed component with the specified instance ID.
  54. * If one cannot be found null is returned.
  55. */
  56. ScriptComponent* getScriptComponent(UINT64 instanceId) const;
  57. /**
  58. * @brief Attempts to find the interop object for the specified SceneObject. If one cannot be found
  59. * null is returned.
  60. */
  61. ScriptSceneObject* getScriptSceneObject(const HSceneObject& sceneObject) const;
  62. /**
  63. * @brief Attempts to find the interop object for a GameObject with the specified instance ID.
  64. * If one cannot be found null is returned.
  65. */
  66. ScriptGameObjectBase* getScriptGameObject(UINT64 instanceId) const;
  67. /**
  68. * @brief Destroys and unregisters the specified GameObject interop object.
  69. */
  70. void destroyScriptGameObject(ScriptGameObjectBase* gameObject);
  71. private:
  72. /**
  73. * @brief Triggers OnReset methods on all registered managed components.
  74. *
  75. * @note Usually this happens after an assembly reload.
  76. */
  77. void sendComponentResetEvents();
  78. UnorderedMap<UINT64, ScriptGameObjectEntry> mScriptGameObjects;
  79. HEvent mOnAssemblyReloadDoneConn;
  80. };
  81. }