Brunot
Loading...
Searching...
No Matches
Script.h
Go to the documentation of this file.
1
10// ____ __ __ __
11// /\__ _\/\ \ /\ \/\ \
12// \/_/\ \/\ \ \___ __ \ \ \_\ \ ___ __ __ ____ __
13// \ \ \ \ \ _ `\ /'__`\ \ \ _ \ / __`\/\ \/\ \ /',__\ /'__`\
14// \ \ \ \ \ \ \ \/\ __/ \ \ \ \ \/\ \L\ \ \ \_\ \/\__, `\/\ __/
15// \ \_\ \ \_\ \_\ \____\ \ \_\ \_\ \____/\ \____/\/\____/\ \____\
16// \/_/ \/_/\/_/\/____/ \/_/\/_/\/___/ \/___/ \/___/ \/____/
17
18#pragma once
19#include <filesystem>
20
21#define SOL_NO_CHECK_NUMBER_PRECISION 1
22#define SOL_ALL_SAFETIES_ON 1
23#include <sol/sol.hpp>
24
26
27
28class ScriptHolder;
29struct Message;
30
31struct Script final : GameObject
32{
34
35 explicit Script(const std::filesystem::path& path);
36
39 Script();
40
41
42 Script(const Script& other);
43 Script(Script&& other) noexcept;
44 auto operator=(const Script& other) -> Script&;
45 auto operator=(Script&& other) noexcept -> Script&;
46
47 explicit operator bool() const
48 {
49 return !isNull;
50 }
51
52 auto showMenu() -> void override;
53
54 auto clone() const -> std::unique_ptr<GameObject> override;
55 auto endWindow() -> void override;
56
57private:
58 auto onUpdate(float dt) -> void override;
59 auto onRender() -> void override;
60
61public:
62 auto onEnterEngine() -> void override;
63 auto onSceneStart(const std::string& sceneName) -> void override;
64 auto onSceneExit(const std::string& sceneName) -> void override;
65
66 auto load(const Stream& stream) -> void override;
67 auto save(Stream& stream) const -> void override;
68
74 [[nodiscard("Parenting may fail")]] auto parentTo(GameObject* newParent) -> bool override;
75
83 template <typename... Args>
84 auto callIfFunction(const std::string& functionName,
85 Args&&... args) -> sol::optional<sol::protected_function_result>
86 {
87 if (sol::optional<sol::protected_function> func = luaObject[functionName])
88 {
89 // result is a sol::protected_function_result
90 auto result = func.value()(luaObject, std::forward<Args>(args)...);
91 if (result.valid())
92 {
93 hlg::Trace("Call to function {} succeeded", functionName);
94 }
95 else
96 {
97 sol::error err = result;
98 std::string what = err.what();
99 hlg::Error("Function {} has Error: {}", functionName, what);
100 }
101 return result;
102 }
103 return {};
104 }
105
114 template <typename... Args>
115 auto callFunction(const std::string& functionName, Args&&... args) -> sol::protected_function_result
116 {
117 if (sol::optional<sol::protected_function_result> result = Script::callIfFunction(functionName, std::forward<Args>(args)...))
118 {
119 return std::move(result.value());
120 }
121 return {};
122
123 }
124
130 __forceinline auto getVariable(
131 const std::string& name) -> sol::table_proxy<
132 sol::basic_table_core<false, sol::basic_reference<>>&, std::tuple<
133 const char(&)[5], const std::string&>>
134 {
135 using namespace sol;
136 // no function, get from data
137 return luaObject["data"][name];
138 }
139
140
146 __forceinline auto operator[](
147 const std::string& name) -> sol::table_proxy<
148 sol::basic_table_core<false, sol::basic_reference<>>&, std::tuple<
149 const char(&)[5], const std::string&>>
150 {
151 return getVariable(name);
152 };
153
154
159 auto loadDataFromJson(const nlohmann::json& j) -> void;
160
165 auto getDataAsJson() const -> nlohmann::json;
166
167
171 auto reloadScript() -> void;
172
174 std::filesystem::path scriptPath;
175
177 std::string scriptName;
178
181
182 // generally const members like this aren't amazing, but we're going to use it for NULL scripts.
184 const bool isNull = false;
185
186private:
191 auto updateOtherComponentBindings() -> void;
192
197 auto receiveComponentAdded(const Message*) -> Message;
198
203 auto setParentInLua(GameObject* newParent) -> void;
204
211 static auto tableToJson(const sol::table& table) -> nlohmann::json;
212
218 static auto jsonToTable(const nlohmann::json& j, sol::table table) -> void;
219
224 static auto tableShowMenu(sol::table table) -> void;
225 void deepCopyTable(sol::table table, const sol::table& otherTable);
226};
227
228
229namespace nlohmann
230{
235template <>
236struct adl_serializer<Script>
237{
238 static auto from_json(const json& j) -> Script
239 {
240 auto script = Script(j.at("scriptPath").get<std::string>());
241 script.setGameObjectJson(j.at("GameObject"));
242 script.loadDataFromJson(j);
243 return script;
244 }
245
246 static auto to_json(json& j, const Script& s) -> void
247 {
248 j = {
249 {"GameObject", s.getGameObjectJson()},
250 {"scriptPath", s.scriptPath.string()},
251 {"data", s.getDataAsJson()}
252 };
253 }
254};
255}
256
static FMOD_RESULT result
Definition AudioObject.cpp:27
sys::Json Stream
Definition AudioObject.h:20
the base class for the engine, most things inherit from this.
nlohmann::json json
Definition Json.cpp:19
the base class for the engine, most things inherit from this.
Definition GameObject.h:83
GameObject(std::string typeName, gobj::Type parentType, gobj::Type type)
constructor for gameobject.
Definition GameObject.cpp:24
Definition ScriptHolder.h:30
auto Error(spdlog::format_string_t< Args... > message, Args &&... args) -> void
Definition Logging.h:117
auto Trace(spdlog::format_string_t< Args... > message, Args &&... args) -> void
Definition Logging.h:53
Definition Script.h:230
Definition GameObject.h:27
Definition Message.h:35
Definition Script.h:32
void deepCopyTable(sol::table table, const sol::table &otherTable)
Definition Script.cpp:126
std::filesystem::path scriptPath
the path of the script
Definition Script.h:174
__forceinline auto getVariable(const std::string &name) -> sol::table_proxy< sol::basic_table_core< false, sol::basic_reference<> > &, std::tuple< const char(&)[5], const std::string & > >
Gets a reference to public variable from the script.
Definition Script.h:130
Script()
Constructs a NULL script, which represents a script that does not exist Once constructed as a NULL sc...
Definition Script.cpp:33
__forceinline auto operator[](const std::string &name) -> sol::table_proxy< sol::basic_table_core< false, sol::basic_reference<> > &, std::tuple< const char(&)[5], const std::string & > >
Gets a reference to public variable from the script.
Definition Script.h:146
Script(const std::filesystem::path &path)
Definition Script.cpp:24
friend ScriptHolder
Definition Script.h:33
auto operator=(const Script &other) -> Script &
Definition Script.cpp:194
auto save(Stream &stream) const -> void override
Implementations will load the state of a GameObject to a th::Json object.
Definition Script.cpp:265
auto loadDataFromJson(const nlohmann::json &j) -> void
helper to de-serialize a json into a script
Definition Script.cpp:463
auto callIfFunction(const std::string &functionName, Args &&... args) -> sol::optional< sol::protected_function_result >
checks if a member function of the script exists, and then calls it if it does, otherwise returns an ...
Definition Script.h:84
auto getDataAsJson() const -> nlohmann::json
helper to serialize a script into a json
Definition Script.cpp:402
static auto tableShowMenu(sol::table table) -> void
for all the values in a lua table, create editor fields for them in imgui
Definition Script.cpp:39
auto onSceneExit(const std::string &sceneName) -> void override
Hook that is called right before a scene is unloaded To receive the hook, simply override the functio...
Definition Script.cpp:255
auto setParentInLua(GameObject *newParent) -> void
helper function to set a pointer to entity parent of the script in lua
Definition Script.cpp:270
auto clone() const -> std::unique_ptr< GameObject > override
makes a copy a GameObject even if it's held polymorphically
Definition Script.cpp:226
auto onRender() -> void override
called every frame after update has been called for every object.
Definition Script.cpp:240
auto parentTo(GameObject *newParent) -> bool override
override of parentTo because a ScriptHolder parent is the Entity above it, not the ScriptHolder compo...
Definition Script.cpp:275
auto showMenu() -> void override
Called before update each frame, for calling ImGui editor code relevant to the gameObject.
Definition Script.cpp:215
std::string scriptName
the name of the script
Definition Script.h:177
auto receiveComponentAdded(const Message *) -> Message
message recieving function that is currently unused, but might be used later
Definition Script.cpp:315
static auto jsonToTable(const nlohmann::json &j, sol::table table) -> void
helper function that de-serializes a json into a lua table
Definition Script.cpp:363
auto onUpdate(float dt) -> void override
called once every frame.
Definition Script.cpp:235
auto onSceneStart(const std::string &sceneName) -> void override
Hook that is called before the first frame a scene is run, but after the entire scene is loaded into ...
Definition Script.cpp:250
auto updateOtherComponentBindings() -> void
Helper function that may be used later should link other components on the entity to the script objec...
Definition Script.cpp:305
const bool isNull
whether the script is null (not constructed with a script path)
Definition Script.h:184
static auto tableToJson(const sol::table &table) -> nlohmann::json
helper function that serializes a lua table to a json this works on: numbers (convert to double),...
Definition Script.cpp:320
auto load(const Stream &stream) -> void override
Implementations will load the state of a GameObject from a th::Json Object.
Definition Script.cpp:260
auto reloadScript() -> void
loads a script from the file it is constructed from
Definition Script.cpp:409
sol::table luaObject
the data for the script in lua
Definition Script.h:180
auto callFunction(const std::string &functionName, Args &&... args) -> sol::protected_function_result
calls a lua function that is a member of the Script
Definition Script.h:115
auto onEnterEngine() -> void override
hook that is called when a GameObject enters the Engine tree.
Definition Script.cpp:245
auto endWindow() -> void override
currently unused, but intended to but was assumed to be necessary for a proper ImGui editor
Definition Script.cpp:231
static auto from_json(const json &j) -> Script
Definition Script.h:238
static auto to_json(json &j, const Script &s) -> void
Definition Script.h:246