some_variable = math.sqrt(2) * 2 some_variable2 = 64 * 16 - 32
player = { pos = { X = 20, Y = 30, }, filename = "res/images/player.png", HP = 20, -- }
LuaScript script("player.lua"); std::string filename = script.get<std::string>("player.filename"); int posX = script.get<std::string>("player.pos.X");
#ifndef LUASCRIPT_H #define LUASCRIPT_H #include <string> #include <vector> #include <iostream> // Lua C, , C extern "C" { # include "lua.h" # include "lauxlib.h" # include "lualib.h" } class LuaScript { public: LuaScript(const std::string& filename); ~LuaScript(); void printError(const std::string& variableName, const std::string& reason); template<typename T> T get(const std::string& variableName) { // } bool lua_gettostack(const std::string& variableName) { // . - true // } // 0 template<typename T> T lua_get(const std::string& variableName) { return 0; } // , - // template<typename T> T lua_getdefault(const std::string& variableName) { return 0; } private: lua_State* L; int level; // . lug_gettostack }; #endif
LuaScript::LuaScript(const std::string& filename) { L = luaL_newstate(); if (luaL_loadfile(L, filename.c_str()) || lua_pcall(L, 0, 0, 0)) { std::cout<<"Error: script not loaded ("<<filename<<")"<<std::endl; L = 0; } }
. LuaScript::~LuaScript() { if(L) lua_close(L); }
void LuaScript::printError(const std::string& variableName, const std::string& reason) { std::cout<<"Error: can't get ["<<variableName<<"]. "<<reason<<std::endl; }
template<> inline std::string LuaScript::lua_getdefault<std::string>() { return "null"; }
bool lua_gettostack(const std::string& variableName) { level = 0; std::string var = ""; for(unsigned int i = 0; i < variableName.size(); i++) { if(variableName.at(i) == '.') { if(level == 0) { lua_getglobal(L, var.c_str()); } else { lua_getfield(L, -1, var.c_str()); } if(lua_isnil(L, -1)) { printError(variableName, var + " is not defined"); return false; } else { var = ""; level++; } } else { var += variableName.at(i); } } if(level == 0) { lua_getglobal(L, var.c_str()); } else { lua_getfield(L, -1, var.c_str()); } if(lua_isnil(L, -1)) { printError(variableName, var + " is not defined"); return false; } // , true return true; }
template <typename T> T get(const std::string& variableName) { if(!L) { printError(variableName, "Script is not loaded"); return lua_getdefault<T>(); } T result; if(lua_gettostack(variableName)) { // , result = lua_get<T>(variableName); } else { result = lua_getdefault<T>(); } lua_pop(L, level + 1); // return result; } }
template <> inline bool LuaScript::lua_get<bool>(const std::string& variableName) { return (bool)lua_toboolean(L, -1); } template <> inline float LuaScript::lua_get<float>(const std::string& variableName) { if(!lua_isnumber(L, -1)) { printError(variableName, "Not a number"); } return (float)lua_tonumber(L, -1); } template <> inline int LuaScript::lua_get<int>(const std::string& variableName) { if(!lua_isnumber(L, -1)) { printError(variableName, "Not a number"); } return (int)lua_tonumber(L, -1); } template <> inline std::string LuaScript::lua_get<std::string>(const std::string& variableName) { std::string s = "null"; if(lua_isstring(L, -1)) { s = std::string(lua_tostring(L, -1)); } else { printError(variableName, "Not a string"); } return s; }
Source: https://habr.com/ru/post/197300/
All Articles