BARE2D
LuaScriptEngine.cpp
Go to the documentation of this file.
1 #include "LuaScriptEngine.hpp"
2 
3 #include "LuaScript.hpp"
4 #include "LuaScriptQueue.hpp"
5 #include "LuaContextManager.hpp"
6 #include "LuaFunctions.hpp"
7 
8 #include "Logger.hpp"
9 
10 namespace BARE2D {
11 
13  {
14  }
15 
17  {
18  // Lua automatically garbage collects.
19  }
20 
21  void LuaScriptEngine::init(std::string luaModulesPath)
22  {
23  // Create the master state
24  m_masterState = luaL_newstate();
25 
26  // now load all the standard Lua libraries for the master state
27  luaL_openlibs(m_masterState);
28 
29  // Set the package.path variable for Lua so that extra user-created modules can be loaded in.
30  // Put the global variable's name on the stack
31  lua_getglobal(m_masterState, "package");
32  // Push the path identifier for any and all modules
33  lua_pushstring(m_masterState, std::string(luaModulesPath + "/?.lua").c_str());
34  // Now set package**.path** instead of just package (combines items on the stack, popping the value - the path in this case)
35  lua_setfield(m_masterState, -2, "path");
36  // Clean up, popping the whole item.
37  lua_pop(m_masterState, 1);
38 
39  // register the delay() function.
41  // register the print() function
43  }
44 
45  void LuaScriptEngine::registerModule(std::string filename)
46  {
47  throwError(BAREError::UNINITIALIZED_FUNCTION, "LuaScriptEngine::registerModule");
48  }
49 
50  void LuaScriptEngine::registerCFunction(lua_CFunction function, std::string name)
51  {
52  // Needs to add a C-function to the masterState
53  // Push the function to the stack with no closure variables (this is equivalent to lua_pushcclosure(L, func, 0) as stated in the docs
54  lua_pushcfunction(m_masterState, function);
55  // Pop the function from the stack and create a global with name `name`
56  lua_setglobal(m_masterState, name.c_str());
57  }
58 
59  // addValueToRegistry is defined in the LuaScriptEngine_impl.tcc template file.
60 
62  {
63  // First, update all the contexts which are already running
65 
66  // Second, convert all new LuaScripts on the queue to LuaContextWrappers
67  // Use a for each loop because we're fancy like that
68  for(std::pair<unsigned int, LuaScript*> e : LuaScriptQueue::getInstance()->getQueue()) {
69  // Create the new context wrapper jazz for a new script to run in
70  LuaScriptContextWrapper* context = createLuaContext(e.second);
71 
72  // Activate it
73  context->start();
74 
75  // Throw it on into the context manager with the ID given before (that's the e.first part.)
76  LuaContextManager::getInstance()->addContext(e.first, context);
77  }
78  // Clear the queue - we've activated all the scripts
80  }
81 
83  {
84  // Allocate the memory for a new one
86  // Actually initialize it.
87  context->init(m_masterState, script);
88 
89  return context;
90  }
91 
93  return m_masterState;
94  }
95 
96 }
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::LuaScriptContextWrapper::start
void start()
Starts the script.
Definition: LuaScript.cpp:56
LuaContextManager.hpp
NOTE: This is a singleton class. The LuaContextManager, well, manages the Lua Contexts which the Scri...
BARE2D::LuaFunctions::delay
int delay(lua_State *L)
A custom function which calls a lua_yield. Takes one argument, an integer representing how many updat...
Definition: LuaFunctions.cpp:9
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::LuaContextManager::addContext
void addContext(unsigned int id, LuaScriptContextWrapper *context)
Adds a context wrapper to the map.
Definition: LuaContextManager.cpp:54
BARE2D::LuaScriptContextWrapper::init
void init(lua_State *parentState, LuaScript *script)
Creates and initializes all the necessary bits.
Definition: LuaScript.cpp:21
BARE2D::LuaScript
Definition: LuaScript.hpp:14
BARE2D::BAREError::UNINITIALIZED_FUNCTION
@ UNINITIALIZED_FUNCTION
BARE2D::LuaScriptQueue::clearQueue
void clearQueue()
Clears the stored script queue - for regular use by tbe Script Engine. Deallocates memory that was in...
Definition: LuaScriptQueue.cpp:56
BARE2D::LuaScriptQueue::getInstance
static LuaScriptQueue * getInstance()
Returns a pointer to the singleton object - creates the object if one doesn't already exist.
Definition: LuaScriptQueue.cpp:19
LuaScriptEngine.hpp
This is the class which actually processes all of the scripts in the script queue,...
BARE2D::LuaScriptEngine::LuaScriptEngine
LuaScriptEngine()
Definition: LuaScriptEngine.cpp:12
BARE2D::throwError
void throwError(BAREError err, std::string message)
Throws an error silently. Adds it to the pile.
Definition: BAREErrors.cpp:190
LuaFunctions.hpp
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::LuaContextManager::update
void update()
Updates all of the context wrappers, pausing, removing (if finished), or resuming them as is correct.
Definition: LuaContextManager.cpp:33
BARE2D::LuaContextManager::getInstance
static LuaContextManager * getInstance()
Definition: LuaContextManager.cpp:21
BARE2D::LuaScriptEngine::~LuaScriptEngine
~LuaScriptEngine()
Definition: LuaScriptEngine.cpp:16
BARE2D::LuaFunctions::print
int print(lua_State *L)
Uses the Logger to print to the screen and log. Takes one argument, a string to print.
Definition: LuaFunctions.cpp:16
Logger.hpp
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