BARE2D
LuaScriptEngine.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <functional>
4 
5 #include "LuaHeaders.hpp"
6 
7 #include "LuaScript.hpp"
8 #include "LuaScriptQueue.hpp"
9 
10 namespace BARE2D {
11 
12  class LuaScript;
13  class LuaScriptContextWrapper;
14 
15  /**
16  * @class LuaScriptEngine
17  * @file LuaScriptEngine.hpp
18  * @brief This is the class which actually processes all of the scripts in the script queue, and creates/initializes contexts for them. Takes from LuaScriptQueue and gives to LuaContextManager.
19  */
21  {
22  public:
25 
26  /**
27  * @brief Initializes the LuaScriptEngine - loads basic modules etc.
28  * @param The path to the user-created Lua modules.
29  */
30  void init(std::string luaModulesPath);
31 
32  /**
33  * @brief Loads and registers (read: runs) the module located at the given file
34  * @param filename The file to load/run.
35  */
36  void registerModule(std::string filename);
37 
38  /**
39  * @brief Adds a Lua C Function. This is a function which can be called from inside a Lua script, and executes C/C++ code.
40  * @param function The function which will be called when the Lua script calls the closure. Follows the form of a std::function<int(lua_State*)>. The lua_State pointer provides a handle to the Lua stack (really, the whole state, but the stack is the most important) which allows the C/C++ program to recieve parameters and add return values. Must return the number of values the function gives back to Lua. See https://www.lua.org/manual/5.1/manual.html#lua_CFunction
41  * @param name The name by which the Lua code can call the function.
42  */
43  void registerCFunction(lua_CFunction function, std::string name);
44 
45  /**
46  * @brief Processes all scripts from the LuaScriptQueue and updates all running scripts. That's pretty much it!
47  */
48  void update();
49 
50  /**
51  * @brief Adds a value to the Lua registry - this lets any C/C++ code use the value stored in the state (it is stored in the state)
52  * @param value A pointer to the value to be added.
53  * @param key The key to be used to access the value in the registry.
54  */
55  template<class T>
56  static void addValueToRegistry(lua_State* L, T* value, std::string key);
57 
58  template<class T>
59  static T* getValueFromRegistry(lua_State* L, std::string key);
60 
61  /**
62  * @brief Returns a pointer to the master state.
63  */
64  lua_State* getMasterState();
65 
66  private:
67  // Both the script queue and context manager are singletons, so we needn't worry about storing those.
68 
69  /**
70  * @brief Creates a LuaScriptContextWrapper using the data from script
71  * @param script A pointer to the LuaScript which we want to create a new Lua state for.
72  * @return A pointer to the LuaScriptContextWrapper which the function created.
73  */
75 
76  // The state which C-functions are loaded into, modules are loaded into, etc. All scripts start as copies of the master state, so they have all of the registry, functions, etc.
77  lua_State* m_masterState = nullptr;
78 
79  };
80 
81 }
82 
83 #include "LuaScriptEngine_impl.tcc"
84 
LuaHeaders.hpp
BARE2D::LuaScriptEngine
Definition: LuaScriptEngine.hpp:20
LuaScriptQueue.hpp
This class essentially just collects all of the scripts that need to be run from all the various end-...
BARE2D
Definition: App.cpp:13
BARE2D::LuaScriptEngine::init
void init(std::string luaModulesPath)
Initializes the LuaScriptEngine - loads basic modules etc.
Definition: LuaScriptEngine.cpp:21
BARE2D::LuaScriptEngine::m_masterState
lua_State * m_masterState
Definition: LuaScriptEngine.hpp:77
BARE2D::LuaScriptEngine::addValueToRegistry
static void addValueToRegistry(lua_State *L, T *value, std::string key)
Adds a value to the Lua registry - this lets any C/C++ code use the value stored in the state (it is ...
BARE2D::LuaScriptEngine::registerCFunction
void registerCFunction(lua_CFunction function, std::string name)
Adds a Lua C Function. This is a function which can be called from inside a Lua script,...
Definition: LuaScriptEngine.cpp:50
BARE2D::LuaScriptContextWrapper
Definition: LuaScript.hpp:31
BARE2D::LuaScriptEngine::getMasterState
lua_State * getMasterState()
Returns a pointer to the master state.
Definition: LuaScriptEngine.cpp:92
BARE2D::LuaScript
Definition: LuaScript.hpp:14
LuaScriptEngine_impl.tcc
BARE2D::LuaScriptEngine::LuaScriptEngine
LuaScriptEngine()
Definition: LuaScriptEngine.cpp:12
BARE2D::LuaScriptEngine::createLuaContext
LuaScriptContextWrapper * createLuaContext(LuaScript *script)
Creates a LuaScriptContextWrapper using the data from script.
Definition: LuaScriptEngine.cpp:82
BARE2D::LuaScriptEngine::update
void update()
Processes all scripts from the LuaScriptQueue and updates all running scripts. That's pretty much it!
Definition: LuaScriptEngine.cpp:61
BARE2D::LuaScriptEngine::~LuaScriptEngine
~LuaScriptEngine()
Definition: LuaScriptEngine.cpp:16
BARE2D::LuaScriptEngine::getValueFromRegistry
static T * getValueFromRegistry(lua_State *L, std::string key)
LuaScript.hpp
This is the basic Lua script wrapper - it is what the end-user will create and add to the queue....
BARE2D::LuaScriptEngine::registerModule
void registerModule(std::string filename)
Loads and registers (read: runs) the module located at the given file.
Definition: LuaScriptEngine.cpp:45