BARE2D
BARE2D::LuaScriptEngine Class Reference

#include <LuaScriptEngine.hpp>

+ Collaboration diagram for BARE2D::LuaScriptEngine:

Public Member Functions

 LuaScriptEngine ()
 
 ~LuaScriptEngine ()
 
void init (std::string luaModulesPath)
 Initializes the LuaScriptEngine - loads basic modules etc. More...
 
void registerModule (std::string filename)
 Loads and registers (read: runs) the module located at the given file. More...
 
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, and executes C/C++ code. More...
 
void update ()
 Processes all scripts from the LuaScriptQueue and updates all running scripts. That's pretty much it! More...
 
lua_State * getMasterState ()
 Returns a pointer to the master state. More...
 

Static Public Member Functions

template<class T >
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 stored in the state) More...
 
template<class T >
static T * getValueFromRegistry (lua_State *L, std::string key)
 

Private Member Functions

LuaScriptContextWrappercreateLuaContext (LuaScript *script)
 Creates a LuaScriptContextWrapper using the data from script. More...
 

Private Attributes

lua_State * m_masterState = nullptr
 

Detailed Description

Definition at line 20 of file LuaScriptEngine.hpp.

Constructor & Destructor Documentation

◆ LuaScriptEngine()

BARE2D::LuaScriptEngine::LuaScriptEngine ( )

Definition at line 12 of file LuaScriptEngine.cpp.

◆ ~LuaScriptEngine()

BARE2D::LuaScriptEngine::~LuaScriptEngine ( )

Definition at line 16 of file LuaScriptEngine.cpp.

Member Function Documentation

◆ addValueToRegistry()

template<class T >
static void BARE2D::LuaScriptEngine::addValueToRegistry ( lua_State *  L,
T *  value,
std::string  key 
)
static

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)

Parameters
valueA pointer to the value to be added.
keyThe key to be used to access the value in the registry.

◆ createLuaContext()

LuaScriptContextWrapper * BARE2D::LuaScriptEngine::createLuaContext ( LuaScript script)
private

Creates a LuaScriptContextWrapper using the data from script.

Parameters
scriptA pointer to the LuaScript which we want to create a new Lua state for.
Returns
A pointer to the LuaScriptContextWrapper which the function created.

Definition at line 82 of file LuaScriptEngine.cpp.

References BARE2D::LuaScriptContextWrapper::init(), and m_masterState.

Referenced by update().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getMasterState()

lua_State * BARE2D::LuaScriptEngine::getMasterState ( )

Returns a pointer to the master state.

Definition at line 92 of file LuaScriptEngine.cpp.

References m_masterState.

◆ getValueFromRegistry()

template<class T >
static T* BARE2D::LuaScriptEngine::getValueFromRegistry ( lua_State *  L,
std::string  key 
)
static

◆ init()

void BARE2D::LuaScriptEngine::init ( std::string  luaModulesPath)

Initializes the LuaScriptEngine - loads basic modules etc.

Parameters
Thepath to the user-created Lua modules.

Definition at line 21 of file LuaScriptEngine.cpp.

References BARE2D::LuaFunctions::delay(), m_masterState, BARE2D::LuaFunctions::print(), and registerCFunction().

+ Here is the call graph for this function:

◆ registerCFunction()

void BARE2D::LuaScriptEngine::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, and executes C/C++ code.

Parameters
functionThe 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
nameThe name by which the Lua code can call the function.

Definition at line 50 of file LuaScriptEngine.cpp.

References m_masterState.

Referenced by init().

+ Here is the caller graph for this function:

◆ registerModule()

void BARE2D::LuaScriptEngine::registerModule ( std::string  filename)

Loads and registers (read: runs) the module located at the given file.

Parameters
filenameThe file to load/run.

Definition at line 45 of file LuaScriptEngine.cpp.

References BARE2D::throwError(), and BARE2D::UNINITIALIZED_FUNCTION.

+ Here is the call graph for this function:

◆ update()

void BARE2D::LuaScriptEngine::update ( )

Processes all scripts from the LuaScriptQueue and updates all running scripts. That's pretty much it!

Definition at line 61 of file LuaScriptEngine.cpp.

References BARE2D::LuaContextManager::addContext(), BARE2D::LuaScriptQueue::clearQueue(), createLuaContext(), BARE2D::LuaContextManager::getInstance(), BARE2D::LuaScriptQueue::getInstance(), BARE2D::LuaScriptContextWrapper::start(), and BARE2D::LuaContextManager::update().

+ Here is the call graph for this function:

Field Documentation

◆ m_masterState

lua_State* BARE2D::LuaScriptEngine::m_masterState = nullptr
private

Definition at line 77 of file LuaScriptEngine.hpp.

Referenced by createLuaContext(), getMasterState(), init(), and registerCFunction().


The documentation for this class was generated from the following files: