galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
Script.cpp
Go to the documentation of this file.
1
7
8#include "galaxy/core/ServiceLocator.hpp"
10#include "galaxy/utils/Globals.hpp"
11
12#include "Script.hpp"
13
14namespace galaxy
15{
16 namespace lua
17 {
19 : m_loaded {false}
20 {
21 }
22
23 Script::Script(const std::string& file)
24 : m_loaded {false}
25 {
26 GALAXY_UNUSED(load(file));
27 }
28
30 {
31 }
32
33 bool Script::load(const std::string& file)
34 {
35 auto& fs = core::ServiceLocator<fs::VirtualFileSystem>::ref();
36
37 const auto script = fs.read(file);
38 if (!script.empty())
39 {
40 m_script = core::ServiceLocator<sol::state>::ref().load(script);
41
42 if (m_script.status() != sol::load_status::ok)
43 {
44 GALAXY_LOG(GALAXY_ERROR, "Failed to load script '{0}' because '{1}'.", file, magic_enum::enum_name(m_script.status()));
45 }
46 else
47 {
48 m_loaded = true;
49 }
50 }
51 else
52 {
53 GALAXY_LOG(GALAXY_ERROR, "Failed to read script '{0}'.", file);
54 }
55
56 return m_loaded;
57 }
58
60 {
61 if (m_loaded)
62 {
63 const auto result = m_script();
64 return result.valid();
65 }
66
67 return false;
68 }
69
70 sol::protected_function_result Script::run_and_return()
71 {
72 if (m_loaded)
73 {
74 return m_script();
75 }
76
77 return {};
78 }
79 } // namespace lua
80} // namespace galaxy
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:29
#define GALAXY_ERROR
Definition Log.hpp:25
#define GALAXY_UNUSED(var)
Prevents compiler warnings when applied to unused parameters.
Definition Settings.hpp:285
bool load(const std::string &file)
Load a script.
Definition Script.cpp:33
sol::protected_function_result run_and_return()
Run a lua script and get a return value.
Definition Script.cpp:70
Script()
Constructor.
Definition Script.cpp:18
sol::load_result m_script
Script loaded into sol3 memory.
Definition Script.hpp:68
bool m_loaded
Flag to make sure script is loaded.
Definition Script.hpp:73
bool run()
Run a lua script.
Definition Script.cpp:59
~Script()
Destructor.
Definition Script.cpp:29
Timer.hpp galaxy.
Definition Async.hpp:17