Skip to content

Commit 1cf3f8a

Browse files
authored
Reworked the scripting system to be fully agnostic (#355)
1 parent dbe0b2d commit 1cf3f8a

38 files changed

Lines changed: 1344 additions & 831 deletions

Sources/Overload/OvCore/include/OvCore/ECS/Components/Behaviour.h

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
#pragma once
88

9-
#include <sol.hpp>
10-
11-
#include "OvCore/ECS/Components/CPhysicalObject.h"
9+
#include <OvCore/ECS/Components/CPhysicalObject.h>
10+
#include <OvTools/Utils/OptRef.h>
11+
#include <OvCore/Scripting/ScriptEngine.h>
1212

1313
namespace OvCore::ECS { class Actor; }
1414

@@ -38,31 +38,20 @@ namespace OvCore::ECS::Components
3838
virtual std::string GetName() override;
3939

4040
/**
41-
* Register the behaviour to lua
42-
* Returns true on success
43-
* @param p_luaState
44-
* @param p_rootFolder
45-
*/
46-
bool RegisterToLuaContext(sol::state& p_luaState, const std::string& p_scriptFolder);
47-
48-
/**
49-
* Register the behaviour to lua
50-
* Returns true on success
41+
* Sets the script associated with this behaviour
42+
* @param p_script
5143
*/
52-
void UnregisterFromLuaContext();
44+
void SetScript(std::unique_ptr<Scripting::Script>&& p_script);
5345

5446
/**
55-
* Call a lua function for this behaviour
56-
* @param p_functionName
57-
* @param p_args
47+
* Returns the script context of this behaviour
5848
*/
59-
template<typename... Args>
60-
void LuaCall(const std::string& p_functionName, Args&&... p_args);
49+
OvTools::Utils::OptRef<Scripting::Script> GetScript();
6150

6251
/**
63-
* Return the lua table attached to this behaviour
52+
* Removes the script associated with this behaviour
6453
*/
65-
sol::table& GetTable();
54+
void RemoveScript();
6655

6756
/**
6857
* Called when the scene start right before OnStart
@@ -166,14 +155,9 @@ namespace OvCore::ECS::Components
166155
virtual void OnInspector(OvUI::Internal::WidgetContainer & p_root) override;
167156

168157
public:
169-
static OvTools::Eventing::Event<Behaviour*> CreatedEvent;
170-
static OvTools::Eventing::Event<Behaviour*> DestroyedEvent;
171-
172158
const std::string name;
173159

174160
private:
175-
sol::table m_object = sol::nil;
161+
std::unique_ptr<Scripting::Script> m_script;
176162
};
177163
}
178-
179-
#include "OvCore/ECS/Components/Behaviour.inl"

Sources/Overload/OvCore/include/OvCore/ECS/Components/Behaviour.inl

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @project: Overload
3+
* @author: Overload Tech.
4+
* @licence: MIT
5+
*/
6+
7+
#pragma once
8+
9+
namespace OvCore::Scripting
10+
{
11+
/**
12+
* Available scripting languages
13+
*/
14+
enum class EScriptingLanguage
15+
{
16+
LUA,
17+
NONE
18+
};
19+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @project: Overload
3+
* @author: Overload Tech.
4+
* @licence: MIT
5+
*/
6+
7+
#pragma once
8+
9+
#include <OvCore/Scripting/Common/EScriptingLanguage.h>
10+
11+
namespace OvCore::Scripting
12+
{
13+
/**
14+
* Interface for any scripting backend to implement.
15+
* This class defines the necessary methods that a scripting engine should provide
16+
* to interact with the engine's components and handle various lifecycle events.
17+
*/
18+
template<EScriptingLanguage Language, class Context>
19+
class TScript
20+
{
21+
public:
22+
/**
23+
* Constructor of the generic script
24+
*/
25+
TScript();
26+
27+
/**
28+
* Destructor of the generic script (virtual to allow polymorphism)
29+
*/
30+
virtual ~TScript();
31+
32+
/**
33+
* Checks if the script context is valid.
34+
* @return True if the context is valid, false otherwise.
35+
*/
36+
bool IsValid() const;
37+
38+
/**
39+
* Return the context of the script
40+
*/
41+
inline const Context& GetContext() const { return m_context; }
42+
43+
protected:
44+
Context m_context;
45+
};
46+
}
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/**
2+
* @project: Overload
3+
* @author: Overload Tech.
4+
* @licence: MIT
5+
*/
6+
7+
#pragma once
8+
9+
#include <string>
10+
#include <vector>
11+
12+
#include <OvCore/Scripting/Common/EScriptingLanguage.h>
13+
14+
namespace OvCore::ECS::Components
15+
{
16+
class Behaviour;
17+
class CPhysicalObject;
18+
}
19+
20+
namespace OvCore::Scripting
21+
{
22+
/**
23+
* Interface for any scripting backend to implement.
24+
* This class defines the necessary methods that a scripting engine should provide
25+
* to interact with the engine's components and handle various lifecycle events.
26+
*/
27+
template<EScriptingLanguage Language, class Context>
28+
class TScriptEngine
29+
{
30+
public:
31+
/**
32+
* Constructor of the generic script engine
33+
*/
34+
TScriptEngine();
35+
36+
/**
37+
* Destructor of the generic script engine (virtual to allow polymorphism)
38+
*/
39+
virtual ~TScriptEngine();
40+
41+
/**
42+
* Defines the root location of the script folder
43+
* @param p_rootFolder
44+
*/
45+
void SetScriptRootFolder(const std::string& p_rootFolder);
46+
47+
/**
48+
* Returns a list of valid extensions for scripts.
49+
*/
50+
std::vector<std::string> GetValidExtensions();
51+
52+
/**
53+
* Returns the content for a default script
54+
* @param p_name Name of the class or object represented by this script
55+
*/
56+
std::string GetDefaultScriptContent(const std::string& p_name);
57+
58+
/**
59+
* Returns the extension for a default script
60+
*/
61+
std::string GetDefaultExtension();
62+
63+
/**
64+
* Adds a behaviour to the scripting engine.
65+
* @param p_toAdd The behaviour to add.
66+
*/
67+
void AddBehaviour(OvCore::ECS::Components::Behaviour& p_toAdd);
68+
69+
/**
70+
* Removes a behaviour from the scripting engine.
71+
* @param p_toRemove The behaviour to remove.
72+
*/
73+
void RemoveBehaviour(OvCore::ECS::Components::Behaviour& p_toRemove);
74+
75+
/**
76+
* Reloads the scripting engine.
77+
*/
78+
void Reload();
79+
80+
/**
81+
* Checks if the scripting engine is in a good state.
82+
* @return True if the engine is okay, false otherwise.
83+
*/
84+
bool IsOk() const;
85+
86+
/**
87+
* Called when a behaviour is awakened.
88+
* @param p_target The target behaviour.
89+
*/
90+
void OnAwake(OvCore::ECS::Components::Behaviour& p_target);
91+
92+
/**
93+
* Called when a behaviour starts.
94+
* @param p_target The target behaviour.
95+
*/
96+
void OnStart(OvCore::ECS::Components::Behaviour& p_target);
97+
98+
/**
99+
* Called when a behaviour is enabled.
100+
* @param p_target The target behaviour.
101+
*/
102+
void OnEnable(OvCore::ECS::Components::Behaviour& p_target);
103+
104+
/**
105+
* Called when a behaviour is disabled.
106+
* @param p_target The target behaviour.
107+
*/
108+
void OnDisable(OvCore::ECS::Components::Behaviour& p_target);
109+
110+
/**
111+
* Called when a behaviour is destroyed.
112+
* @param p_target The target behaviour.
113+
*/
114+
void OnDestroy(OvCore::ECS::Components::Behaviour& p_target);
115+
116+
/**
117+
* Called every frame to update a behaviour.
118+
* @param p_target The target behaviour.
119+
* @param p_deltaTime The time elapsed since the last frame.
120+
*/
121+
void OnUpdate(OvCore::ECS::Components::Behaviour& p_target, float p_deltaTime);
122+
123+
/**
124+
* Called every physics frame to update a behaviour.
125+
* @param p_target The target behaviour.
126+
* @param p_deltaTime The time elapsed since the last physics frame.
127+
*/
128+
void OnFixedUpdate(OvCore::ECS::Components::Behaviour& p_target, float p_deltaTime);
129+
130+
/**
131+
* Called every frame after OnUpdate to perform late updates on a behaviour.
132+
* @param p_target The target behaviour.
133+
* @param p_deltaTime The time elapsed since the last frame.
134+
*/
135+
void OnLateUpdate(OvCore::ECS::Components::Behaviour& p_target, float p_deltaTime);
136+
137+
/**
138+
* Called when a behaviour's owner enters a collision with another physical object.
139+
* @param p_target The target behaviour.
140+
* @param p_otherObject The other physical object involved in the collision.
141+
*/
142+
void OnCollisionEnter(OvCore::ECS::Components::Behaviour& p_target, OvCore::ECS::Components::CPhysicalObject& p_otherObject);
143+
144+
/**
145+
* Called when a behaviour's owner stays in collision with another physical object.
146+
* @param p_target The target behaviour.
147+
* @param p_otherObject The other physical object involved in the collision.
148+
*/
149+
void OnCollisionStay(OvCore::ECS::Components::Behaviour& p_target, OvCore::ECS::Components::CPhysicalObject& p_otherObject);
150+
151+
/**
152+
* Called when a behaviour's owner exits a collision with another physical object.
153+
* @param p_target The target behaviour.
154+
* @param p_otherObject The other physical object involved in the collision.
155+
*/
156+
void OnCollisionExit(OvCore::ECS::Components::Behaviour& p_target, OvCore::ECS::Components::CPhysicalObject& p_otherObject);
157+
158+
/**
159+
* Called when a behaviour's owner enters a trigger with another physical object.
160+
* @param p_target The target behaviour.
161+
* @param p_otherObject The other physical object involved in the trigger.
162+
*/
163+
void OnTriggerEnter(OvCore::ECS::Components::Behaviour& p_target, OvCore::ECS::Components::CPhysicalObject& p_otherObject);
164+
165+
/**
166+
* Called when a behaviour's owner stays in a trigger with another physical object.
167+
* @param p_target The target behaviour.
168+
* @param p_otherObject The other physical object involved in the trigger.
169+
*/
170+
void OnTriggerStay(OvCore::ECS::Components::Behaviour& p_target, OvCore::ECS::Components::CPhysicalObject& p_otherObject);
171+
172+
/**
173+
* Called when a behaviour's owner exits a trigger with another physical object.
174+
* @param p_target The target behaviour.
175+
* @param p_otherObject The other physical object involved in the trigger.
176+
*/
177+
void OnTriggerExit(OvCore::ECS::Components::Behaviour& p_target, OvCore::ECS::Components::CPhysicalObject& p_otherObject);
178+
179+
protected:
180+
Context m_context;
181+
};
182+
}

0 commit comments

Comments
 (0)