BARE2D
LuaScript.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 
5 #include <lua5.3/lua.hpp>
6 
7 namespace BARE2D {
8 
9  /**
10  * @class LuaScript
11  * @file LuaScript.hpp
12  * @brief This is the basic Lua script wrapper - it is what the end-user will create and add to the queue. It basically just holds the text that the script will run.
13  */
14  class LuaScript {
15  public:
16  LuaScript();
17  ~LuaScript();
18 
19  // The script's text - this is the code.
20  std::string m_script;
21  std::string m_path;
22 
23  bool inited = false;
24  };
25 
26  /**
27  * @class LuaScriptContextWrapper
28  * @file LuaScript.hpp
29  * @brief This is not to be used by the end user under any circumstances. This is the class which represents a Lua context that has been initialized with a script's Lua to run. It holds various data, such as a handle to the parent context.
30  */
32  public:
35 
36  /**
37  * @brief Creates and initializes all the necessary bits.
38  * @param id The ID of the script, which was assigned from the LuaScriptQueue
39  * @param parentState The masterstate, generally, although I suppose it could be others.
40  */
41  void init(lua_State* parentState, LuaScript* script);
42 
43  /**
44  * @brief Cleans up
45  */
46  void destroy();
47 
48  /**
49  * @brief Starts the script
50  */
51  void start();
52 
53  /**
54  * @brief Updates the script - decreases delay counter, checks if it is finished, etc.
55  */
56  void update();
57 
58  /**
59  * @brief Returns m_completed.
60  */
61  bool isCompleted();
62 
63  protected:
64  /**
65  * @brief Loads and compiles the given Lua script. Also, sets m_scriptReference to the index of the compiled script in the registry.
66  * @param luaStr The script to compile.
67  */
68  void loadLua(std::string& luaStr);
69 
70  /**
71  * @brief Creates the m_state variable as a thread, or subroutine of the parent
72  */
73  void createThread();
74 
75  // The state (or context, if you will) that this class represents.
76  lua_State* m_state = nullptr;
77  // The parent state (or context) that caused this class to be created.
78  lua_State* m_parent = nullptr;
79 
80  // Is the script paused? if so, how long is on the timer?
81  bool m_yielded = false;
82  unsigned int m_remainingDelay = 0;
83 
84  // If the script is finished.
85  bool m_completed = false;
86 
87  // This is a reference to the loaded script's place in the registry.
88  int m_scriptReference = LUA_REFNIL;
89  // This is a reference to the thread in the master state's registry
90  int m_threadReference = LUA_REFNIL;
91  };
92 
93 } // namespace BARE2D
BARE2D::LuaScript::m_script
std::string m_script
Definition: LuaScript.hpp:20
BARE2D
Definition: App.cpp:13
BARE2D::LuaScriptContextWrapper::m_parent
lua_State * m_parent
Definition: LuaScript.hpp:78
BARE2D::LuaScript::LuaScript
LuaScript()
Definition: LuaScript.cpp:9
BARE2D::LuaScriptContextWrapper::m_scriptReference
int m_scriptReference
Definition: LuaScript.hpp:88
BARE2D::LuaScript::~LuaScript
~LuaScript()
Definition: LuaScript.cpp:12
BARE2D::LuaScriptContextWrapper::~LuaScriptContextWrapper
~LuaScriptContextWrapper()
Definition: LuaScript.cpp:18
BARE2D::LuaScriptContextWrapper::loadLua
void loadLua(std::string &luaStr)
Loads and compiles the given Lua script. Also, sets m_scriptReference to the index of the compiled sc...
Definition: LuaScript.cpp:120
BARE2D::LuaScriptContextWrapper::start
void start()
Starts the script.
Definition: LuaScript.cpp:56
BARE2D::LuaScriptContextWrapper::m_yielded
bool m_yielded
Definition: LuaScript.hpp:81
BARE2D::LuaScriptContextWrapper
Definition: LuaScript.hpp:31
BARE2D::LuaScriptContextWrapper::init
void init(lua_State *parentState, LuaScript *script)
Creates and initializes all the necessary bits.
Definition: LuaScript.cpp:21
BARE2D::LuaScriptContextWrapper::update
void update()
Updates the script - decreases delay counter, checks if it is finished, etc.
Definition: LuaScript.cpp:80
BARE2D::LuaScript
Definition: LuaScript.hpp:14
BARE2D::LuaScriptContextWrapper::m_threadReference
int m_threadReference
Definition: LuaScript.hpp:90
BARE2D::LuaScriptContextWrapper::isCompleted
bool isCompleted()
Returns m_completed.
Definition: LuaScript.cpp:141
BARE2D::LuaScriptContextWrapper::m_completed
bool m_completed
Definition: LuaScript.hpp:85
BARE2D::LuaScriptContextWrapper::LuaScriptContextWrapper
LuaScriptContextWrapper()
Definition: LuaScript.cpp:15
BARE2D::LuaScript::inited
bool inited
Definition: LuaScript.hpp:23
BARE2D::LuaScript::m_path
std::string m_path
Definition: LuaScript.hpp:21
BARE2D::LuaScriptContextWrapper::createThread
void createThread()
Creates the m_state variable as a thread, or subroutine of the parent.
Definition: LuaScript.cpp:32
BARE2D::LuaScriptContextWrapper::m_remainingDelay
unsigned int m_remainingDelay
Definition: LuaScript.hpp:82
BARE2D::LuaScriptContextWrapper::m_state
lua_State * m_state
Definition: LuaScript.hpp:76
BARE2D::LuaScriptContextWrapper::destroy
void destroy()
Cleans up.
Definition: LuaScript.cpp:45