LuaScriptInstance.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. /// \file
  4. #pragma once
  5. #include "../LuaScript/LuaScriptEventListener.h"
  6. #include "../Scene/Component.h"
  7. struct lua_State;
  8. namespace Urho3D
  9. {
  10. class LuaFile;
  11. class LuaFunction;
  12. class LuaScript;
  13. class LuaScriptEventInvoker;
  14. /// Lua Script object methods.
  15. enum LuaScriptObjectMethod
  16. {
  17. LSOM_START = 0,
  18. LSOM_STOP,
  19. LSOM_DELAYEDSTART,
  20. LSOM_UPDATE,
  21. LSOM_POSTUPDATE,
  22. LSOM_FIXEDUPDATE,
  23. LSOM_FIXEDPOSTUPDATE,
  24. LSOM_LOAD,
  25. LSOM_SAVE,
  26. LSOM_READNETWORKUPDATE,
  27. LSOM_WRITENETWORKUPDATE,
  28. LSOM_APPLYATTRIBUTES,
  29. LSOM_TRANSFORMCHANGED,
  30. MAX_LUA_SCRIPT_OBJECT_METHODS
  31. };
  32. /// Lua script object component.
  33. class URHO3D_API LuaScriptInstance : public Component, public LuaScriptEventListener
  34. {
  35. URHO3D_OBJECT(LuaScriptInstance, Component);
  36. public:
  37. /// Construct.
  38. explicit LuaScriptInstance(Context* context);
  39. /// Destruct.
  40. ~LuaScriptInstance() override;
  41. /// Register object factory.
  42. /// @nobind
  43. static void RegisterObject(Context* context);
  44. /// Handle attribute write access.
  45. void OnSetAttribute(const AttributeInfo& attr, const Variant& src) override;
  46. /// Handle attribute read access.
  47. void OnGetAttribute(const AttributeInfo& attr, Variant& dest) const override;
  48. /// Return attribute descriptions, or null if none defined.
  49. const Vector<AttributeInfo>* GetAttributes() const override { return &attributeInfos_; }
  50. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  51. void ApplyAttributes() override;
  52. /// Handle enabled/disabled state change.
  53. void OnSetEnabled() override;
  54. /// Add a scripted event handler by function.
  55. void AddEventHandler(const String& eventName, int functionIndex) override;
  56. /// Add a scripted event handler by function name.
  57. void AddEventHandler(const String& eventName, const String& functionName) override;
  58. /// Add a scripted event handler by function for a specific sender.
  59. void AddEventHandler(Object* sender, const String& eventName, int functionIndex) override;
  60. /// Add a scripted event handler by function name for a specific sender.
  61. void AddEventHandler(Object* sender, const String& eventName, const String& functionName) override;
  62. /// Remove a scripted event handler.
  63. void RemoveEventHandler(const String& eventName) override;
  64. /// Remove a scripted event handler for a specific sender.
  65. void RemoveEventHandler(Object* sender, const String& eventName) override;
  66. /// Remove all scripted event handlers for a specific sender.
  67. void RemoveEventHandlers(Object* sender) override;
  68. /// Remove all scripted event handlers.
  69. void RemoveAllEventHandlers() override;
  70. /// Remove all scripted event handlers, except those listed.
  71. void RemoveEventHandlersExcept(const Vector<String>& exceptionNames) override;
  72. /// Return whether has subscribed to an event.
  73. bool HasEventHandler(const String& eventName) const override;
  74. /// Return whether has subscribed to a specific sender's event.
  75. bool HasEventHandler(Object* sender, const String& eventName) const override;
  76. /// Create script object. Return true if successful.
  77. bool CreateObject(const String& scriptObjectType);
  78. /// Create script object. Return true if successful.
  79. bool CreateObject(LuaFile* scriptFile, const String& scriptObjectType);
  80. /// Set script file.
  81. void SetScriptFile(LuaFile* scriptFile);
  82. /// Set script object type.
  83. void SetScriptObjectType(const String& scriptObjectType);
  84. /// Set script file serialization attribute by calling a script function.
  85. void SetScriptDataAttr(const Vector<unsigned char>& data);
  86. /// Set script network serialization attribute by calling a script function.
  87. void SetScriptNetworkDataAttr(const Vector<unsigned char>& data);
  88. /// Return script file.
  89. LuaFile* GetScriptFile() const;
  90. /// Return script object type.
  91. const String& GetScriptObjectType() const { return scriptObjectType_; }
  92. /// Return Lua reference to script object.
  93. int GetScriptObjectRef() const { return scriptObjectRef_; }
  94. /// Get script file serialization attribute by calling a script function.
  95. Vector<unsigned char> GetScriptDataAttr() const;
  96. /// Get script network serialization attribute by calling a script function.
  97. Vector<unsigned char> GetScriptNetworkDataAttr() const;
  98. /// Return script object's funcition.
  99. LuaFunction* GetScriptObjectFunction(const String& functionName) const;
  100. /// Set script file attribute.
  101. void SetScriptFileAttr(const ResourceRef& value);
  102. /// Return script file attribute.
  103. ResourceRef GetScriptFileAttr() const;
  104. protected:
  105. /// Handle scene being assigned.
  106. void OnSceneSet(Scene* scene) override;
  107. /// Handle node transform being dirtied.
  108. void OnMarkedDirty(Node* node) override;
  109. private:
  110. /// Find script object attributes.
  111. void GetScriptAttributes();
  112. /// Find script object method refs.
  113. void FindScriptObjectMethodRefs();
  114. /// Subscribe to script method events.
  115. void SubscribeToScriptMethodEvents();
  116. /// Unsubscribe from script method events.
  117. void UnsubscribeFromScriptMethodEvents();
  118. /// Handle the logic update event.
  119. void HandleUpdate(StringHash eventType, VariantMap& eventData);
  120. /// Handle the logic post update event.
  121. void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
  122. #if defined(URHO3D_PHYSICS) || defined(URHO3D_PHYSICS2D)
  123. /// Handle the physics update event.
  124. void HandleFixedUpdate(StringHash eventType, VariantMap& eventData);
  125. /// Handle the physics post update event.
  126. void HandlePostFixedUpdate(StringHash eventType, VariantMap& eventData);
  127. #endif
  128. /// Release the script object.
  129. void ReleaseObject();
  130. /// Lua Script subsystem.
  131. LuaScript* luaScript_{};
  132. /// Lua state.
  133. lua_State* luaState_{};
  134. /// Event invoker.
  135. SharedPtr<LuaScriptEventInvoker> eventInvoker_;
  136. /// Script file.
  137. SharedPtr<LuaFile> scriptFile_;
  138. /// Script object type.
  139. String scriptObjectType_;
  140. /// Attributes, including script object variables.
  141. Vector<AttributeInfo> attributeInfos_;
  142. /// Lua reference to script object.
  143. int scriptObjectRef_{};
  144. /// Script object method.
  145. LuaFunction* scriptObjectMethods_[MAX_LUA_SCRIPT_OBJECT_METHODS]{};
  146. };
  147. }