21#define SOL_NO_CHECK_NUMBER_PRECISION 1
22#define SOL_ALL_SAFETIES_ON 1
35 explicit Script(
const std::filesystem::path& path);
47 explicit operator bool()
const
58 auto
onUpdate(
float dt) ->
void override;
63 auto
onSceneStart(const std::
string& sceneName) ->
void override;
64 auto
onSceneExit(const std::
string& sceneName) ->
void override;
66 auto
load(const
Stream& stream) ->
void override;
67 auto
save(
Stream& stream) const ->
void override;
74 [[nodiscard(
"Parenting may fail")]] auto
parentTo(
GameObject* newParent) ->
bool override;
83 template <typename... Args>
85 Args&&... args) ->
sol::optional<
sol::protected_function_result>
87 if (sol::optional<sol::protected_function> func =
luaObject[functionName])
93 hlg::Trace(
"Call to function {} succeeded", functionName);
98 std::string what = err.what();
99 hlg::Error(
"Function {} has Error: {}", functionName, what);
114 template <
typename... Args>
115 auto callFunction(
const std::string& functionName, Args&&... args) -> sol::protected_function_result
119 return std::move(
result.value());
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&>>
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&>>
240 auto script =
Script(j.at(
"scriptPath").get<std::string>());
241 script.setGameObjectJson(j.at(
"GameObject"));
242 script.loadDataFromJson(j);
249 {
"GameObject", s.getGameObjectJson()},
250 {
"scriptPath", s.scriptPath.string()},
251 {
"data", s.getDataAsJson()}
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 GameObject.h:27
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