LPCGame
A Simple 2d Game Engine
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
luascript.h
1 #ifndef LUASCRIPT_H
2 #define LUASCRIPT_H
3 
4 #include <string>
5 #include <map>
6 #include <vector>
7 #include <lua.hpp>
8 #include "external/json/json.h"
9 #include "debug.h"
10 #include "luac/luacparam.h"
11 
13 
16 class LuaScript {
17 public:
18  LuaScript();
23  LuaScript(std::string script);
24  ~LuaScript();
29  void OpenScript(const std::string &script);
31  void Close();
36  lua_State* Get();
38  std::string File() const;
40  bool Open() const;
46  template<class ReturnType>
47  auto CallFunction(std::string function,
48  std::vector<LuaC::LuaParam*> args = std::vector<LuaC::LuaParam*>()) -> decltype(ReturnType::Retrieve(mL))
49  {
50  //Get the function to be called
51  lua_getglobal(mL, function.c_str());
52  //Push params onto stack
53  for (LuaC::LuaParam *p : args)
54  p->Push(mL);
55 
56  //Call the function
57  if (lua_pcall(mL, args.size(), 1, 0) != 0)
58  Debug::Log("Error calling: " + function + " in script: " + mFile + " " + lua_tostring(mL, -1));
59 
60  return ReturnType::Retrieve(mL);
61  }
62  //For calling a function with no return values
63  void CallFunction(std::string function, std::vector<LuaC::LuaParam*> args = std::vector<LuaC::LuaParam*>()){
64  //Get the function to be called
65  lua_getglobal(mL, function.c_str());
66  //Push params onto stack
67  for (LuaC::LuaParam *p : args)
68  p->Push(mL);
69 
70  //Call the function
71  if (lua_pcall(mL, args.size(), 0, 0) != 0)
72  Debug::Log("Error calling: " + function + " in script: " + mFile + " " + lua_tostring(mL, -1));
73  }
74 
75 private:
80  void AddLoader(int (*loader)(lua_State*));
81 
82 private:
84  lua_State *mL;
86  std::string mFile;
88  bool mOpen;
89 };
90 
91 #endif