merged methods for extracting fields directly from scripts or tables

within
This commit is contained in:
devZoGok
2023-01-20 20:23:08 +02:00
parent 08ef768e2a
commit b9a47c687c
3 changed files with 29 additions and 55 deletions
+5 -6
View File
@@ -20,14 +20,13 @@ namespace gameBase {
LuaManager *luaManager = LuaManager::getSingleton();
luaManager->buildScript(vector<string>{optionsFile});
string table = "mappings";
for(int i = firstMapping; i < firstMapping + numMappings; i++){
Index idIndex = Index(i + 1);
int bind = luaManager->getIntFromTable(table, vector<Index>{idIndex, Index("bind")});
Mapping::BindType type = (Mapping::BindType)luaManager->getIntFromTable(table, vector<Index>{idIndex, Index("bindType")});
bool action = luaManager->getBoolFromTable(table, vector<Index>{idIndex, Index("action")});
int trigger = luaManager->getIntFromTable(table, vector<Index>{idIndex, Index("trigger")});
Index tableInd = Index("mappings"), idIndex = Index(i + 1);
int bind = luaManager->getInt(vector<Index>{tableInd, idIndex, Index("bind")});
Mapping::BindType type = (Mapping::BindType)luaManager->getInt(vector<Index>{tableInd, idIndex, Index("bindType")});
bool action = luaManager->getBool(vector<Index>{tableInd, idIndex, Index("action")});
int trigger = luaManager->getInt(vector<Index>{tableInd, idIndex, Index("trigger")});
Mapping *m = new Mapping;
m->bind = bind;
+19 -41
View File
@@ -32,37 +32,13 @@ namespace gameBase{
cout << errMsg;
}
int LuaManager::getInt(string varName){
lua_getglobal(state, varName.c_str());
int isNum;
int var = (int)lua_tointegerx(state, -1, &isNum);
lua_pop(state, 1);
return var;
}
void LuaManager::prepareTableForRetrieval(vector<Index> &indices){
lua_getglobal(state, indices[0].index.c_str());
float LuaManager::getFloat(string varName){
lua_getglobal(state, varName.c_str());
int isNum;
float var = (int)lua_tonumberx(state, -1, &isNum);
lua_pop(state, 1);
return var;
}
for(int i = 1; i < indices.size(); i++){
if(!lua_istable(state, -1))
cout << indices[i].index << " is not in the " << indices[0].index << " table\n";
string LuaManager::getString(string varName){
lua_getglobal(state, varName.c_str());
string result = lua_tostring(state, -1);
lua_pop(state, 1);
return result;
}
void LuaManager::prepareTableForRetrieval(string table, vector<Index> &indices){
lua_getglobal(state, table.c_str());
if(!lua_istable(state, -1))
cout << table << " is not a table\n";
for(int i = 0; i < indices.size(); i++){
if(indices[i].string)
lua_pushstring(state, indices[i].index.c_str());
else
@@ -72,40 +48,42 @@ namespace gameBase{
}
}
int LuaManager::getIntFromTable(string table, vector<Index> indices){
prepareTableForRetrieval(table, indices);
int LuaManager::getInt(vector<Index> indices){
prepareTableForRetrieval(indices);
int isNum;
int result = lua_tointegerx(state, -1, &isNum);
lua_pop(state, 1 + indices.size());
lua_pop(state, indices.size());
return result;
}
float LuaManager::getFloatFromTable(string table, vector<Index> indices){
prepareTableForRetrieval(table, indices);
float LuaManager::getFloat(vector<Index> indices){
prepareTableForRetrieval(indices);
int isNum;
float result = lua_tonumberx(state, -1, &isNum);
lua_pop(state, 1 + indices.size());
lua_pop(state, indices.size());
return result;
}
bool LuaManager::getBoolFromTable(string table, vector<Index> indices){
prepareTableForRetrieval(table, indices);
bool LuaManager::getBool(vector<Index> indices){
prepareTableForRetrieval(indices);
bool result = lua_toboolean(state, -1);
lua_pop(state, 1 + indices.size());
lua_pop(state, indices.size());
return result;
}
string LuaManager::getStringFromTable(string table, vector<Index> indices){
prepareTableForRetrieval(table, indices);
string LuaManager::getString(vector<Index> indices){
prepareTableForRetrieval(indices);
string result = lua_tostring(state, -1);
lua_pop(state, 1 + indices.size());
lua_pop(state, indices.size());
return result;
}
+5 -8
View File
@@ -35,16 +35,13 @@ namespace gameBase{
inline lua_State* getState(){return state;}
void buildScript(std::vector<std::string>);
void executeCode(std::string, std::string = "Error executing code\n");
int getInt(std::string);
float getFloat(std::string);
int getIntFromTable(std::string, std::vector<Index>);
bool getBoolFromTable(std::string, std::vector<Index>);
float getFloatFromTable(std::string, std::vector<Index>);
std::string getStringFromTable(std::string, std::vector<Index>);
std::string getString(std::string);
int getInt(std::vector<Index>);
bool getBool(std::vector<Index>);
float getFloat(std::vector<Index>);
std::string getString(std::vector<Index>);
private:
LuaManager();
void prepareTableForRetrieval(std::string, std::vector<Index>&);
void prepareTableForRetrieval(std::vector<Index>&);
lua_State *state = nullptr;
};