├── .gitignore ├── LuaScript.cpp ├── LuaScript.h ├── Player.lua ├── README.md └── main.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | *.d 2 | *.o 3 | Makefile -------------------------------------------------------------------------------- /LuaScript.cpp: -------------------------------------------------------------------------------- 1 | #include "LuaScript.h" 2 | 3 | LuaScript::LuaScript(const std::string& filename) { 4 | L = luaL_newstate(); 5 | if (luaL_loadfile(L, filename.c_str()) || lua_pcall(L, 0, 0, 0)) { 6 | std::cout<<"Error: failed to load ("< LuaScript::getIntVector(const std::string& name) { 22 | std::vector v; 23 | lua_gettostack(name.c_str()); 24 | if(lua_isnil(L, -1)) { // array is not found 25 | return std::vector(); 26 | } 27 | lua_pushnil(L); 28 | while(lua_next(L, -2)) { 29 | v.push_back((int)lua_tonumber(L, -1)); 30 | lua_pop(L, 1); 31 | } 32 | clean(); 33 | return v; 34 | } 35 | 36 | std::vector LuaScript::getTableKeys(const std::string& name) { 37 | std::string code = 38 | "function getKeys(name) " 39 | "s = \"\"" 40 | "for k, v in pairs(_G[name]) do " 41 | " s = s..k..\",\" " 42 | " end " 43 | "return s " 44 | "end"; // function for getting table keys 45 | luaL_loadstring(L, 46 | code.c_str()); // execute code 47 | lua_pcall(L,0,0,0); 48 | lua_getglobal(L, "getKeys"); // get function 49 | lua_pushstring(L, name.c_str()); 50 | lua_pcall(L, 1 , 1, 0); // execute function 51 | std::string test = lua_tostring(L, -1); 52 | std::vector strings; 53 | std::string temp = ""; 54 | std::cout<<"TEMP:"< 5 | #include 6 | #include 7 | 8 | extern "C" { 9 | # include "lua.h" 10 | # include "lauxlib.h" 11 | # include "lualib.h" 12 | } 13 | 14 | class LuaScript { 15 | public: 16 | LuaScript(const std::string& filename); 17 | ~LuaScript(); 18 | void printError(const std::string& variableName, const std::string& reason); 19 | std::vector getIntVector(const std::string& name); 20 | std::vector getTableKeys(const std::string& name); 21 | 22 | inline void clean() { 23 | int n = lua_gettop(L); 24 | lua_pop(L, n); 25 | } 26 | 27 | template 28 | T get(const std::string& variableName) { 29 | if(!L) { 30 | printError(variableName, "Script is not loaded"); 31 | return lua_getdefault(); 32 | } 33 | 34 | T result; 35 | if(lua_gettostack(variableName)) { // variable succesfully on top of stack 36 | result = lua_get(variableName); 37 | } else { 38 | result = lua_getdefault(); 39 | } 40 | 41 | 42 | clean(); 43 | return result; 44 | } 45 | 46 | bool lua_gettostack(const std::string& variableName) { 47 | level = 0; 48 | std::string var = ""; 49 | for(unsigned int i = 0; i < variableName.size(); i++) { 50 | if(variableName.at(i) == '.') { 51 | if(level == 0) { 52 | lua_getglobal(L, var.c_str()); 53 | } else { 54 | lua_getfield(L, -1, var.c_str()); 55 | } 56 | 57 | if(lua_isnil(L, -1)) { 58 | printError(variableName, var + " is not defined"); 59 | return false; 60 | } else { 61 | var = ""; 62 | level++; 63 | } 64 | } else { 65 | var += variableName.at(i); 66 | } 67 | } 68 | if(level == 0) { 69 | lua_getglobal(L, var.c_str()); 70 | } else { 71 | lua_getfield(L, -1, var.c_str()); 72 | } 73 | if(lua_isnil(L, -1)) { 74 | printError(variableName, var + " is not defined"); 75 | return false; 76 | } 77 | 78 | return true; 79 | } 80 | 81 | // Generic get 82 | template 83 | T lua_get(const std::string& variableName) { 84 | return 0; 85 | } 86 | 87 | template 88 | T lua_getdefault() { 89 | return 0; 90 | } 91 | 92 | private: 93 | lua_State* L; 94 | std::string filename; 95 | int level; 96 | }; 97 | 98 | // Specializations 99 | 100 | template <> 101 | inline bool LuaScript::lua_get(const std::string& variableName) { 102 | return (bool)lua_toboolean(L, -1); 103 | } 104 | 105 | template <> 106 | inline float LuaScript::lua_get(const std::string& variableName) { 107 | if(!lua_isnumber(L, -1)) { 108 | printError(variableName, "Not a number"); 109 | } 110 | return (float)lua_tonumber(L, -1); 111 | } 112 | 113 | template <> 114 | inline int LuaScript::lua_get(const std::string& variableName) { 115 | if(!lua_isnumber(L, -1)) { 116 | printError(variableName, "Not a number"); 117 | } 118 | return (int)lua_tonumber(L, -1); 119 | } 120 | 121 | template <> 122 | inline std::string LuaScript::lua_get(const std::string& variableName) { 123 | std::string s = "null"; 124 | if(lua_isstring(L, -1)) { 125 | s = std::string(lua_tostring(L, -1)); 126 | } else { 127 | printError(variableName, "Not a string"); 128 | } 129 | return s; 130 | } 131 | 132 | template<> 133 | inline std::string LuaScript::lua_getdefault() { 134 | return "null"; 135 | } 136 | 137 | #endif 138 | -------------------------------------------------------------------------------- /Player.lua: -------------------------------------------------------------------------------- 1 | player = { 2 | position = { 3 | x = 32.5, y = 20.0 4 | }, 5 | filename = "sprite.png", 6 | HP = 300 7 | } 8 | 9 | array = {1, 1, 2, 3, 5, 10, 20} -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | unnamed_lua_binder 2 | ================== 3 | 4 | Simple lua binding with C++ I was using for my game. 5 | 6 | This is very old and not that good. Use [sol2](https://github.com/ThePhD/sol2) instead! It's perfect. 7 | 8 | **Tutorial** 9 | 10 | **Warning: very outdated** 11 | 12 | **Part1**: http://eliasdaler.wordpress.com/2013/10/11/lua_cpp_binder/ 13 | 14 | **Part2**: http://eliasdaler.wordpress.com/2013/10/20/lua_and_cpp_pt2/ 15 | 16 | **Russian version of tutorial** 17 | 18 | **Part1**: http://habrahabr.ru/post/197300/ 19 | 20 | **Part2**: In progress 21 | 22 | This code uses zlib license. 23 | 24 | Copyright (C) 1995-2013 Elias Daler 25 | 26 | This software is provided 'as-is', without any express or implied 27 | warranty. In no event will the authors be held liable for any damages 28 | arising from the use of this software. 29 | 30 | Permission is granted to anyone to use this software for any purpose, 31 | including commercial applications, and to alter it and redistribute it 32 | freely, subject to the following restrictions: 33 | 34 | 1. The origin of this software must not be misrepresented; you must not 35 | claim that you wrote the original software. If you use this software 36 | in a product, an acknowledgment in the product documentation would be 37 | appreciated but is not required. 38 | 2. Altered source versions must be plainly marked as such, and must not be 39 | misrepresented as being the original software. 40 | 3. This notice may not be removed or altered from any source distribution. 41 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "LuaScript.h" 5 | 6 | int main() { 7 | LuaScript script("Player.lua"); 8 | float posX = script.get("player.position.x"); 9 | float posY = script.get("player.position.y"); 10 | std::string filename = script.get("player.filename"); 11 | int hp = script.get("player.HP"); 12 | 13 | std::cout<<"Position X = "< v = script.getIntVector("array"); 19 | std::cout<<"Contents of array:"; 20 | for(std::vector::iterator it = v.begin(); 21 | it != v.end(); 22 | it++) { 23 | std::cout<<*it<<","; 24 | } 25 | std::cout< keys = script.getTableKeys("player"); 29 | std::cout<<"Keys of [player] table:"; 30 | for(std::vector::iterator it = keys.begin(); 31 | it != keys.end(); 32 | it++) { 33 | std::cout<<*it<<","; 34 | } 35 | std::cout<