-
Notifications
You must be signed in to change notification settings - Fork 0
/
StarLua.h
63 lines (58 loc) · 1.71 KB
/
StarLua.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "lua.hpp"
#include <string>
#ifndef _STARLUA_H_ //如果没有引入头文件file.h
#define _STARLUA_H_ //那就引入头文件file.h
std::pair<std::string,bool> lua_gettablestring(lua_State* L,std::string key) {
lua_pushstring(L, key.c_str());
lua_gettable(L, 1);
std::string s = "";
if (lua_type(L, -1) != LUA_TNIL) {
s = lua_tostring(L, -1);
lua_pop(L, 1);
return { s,1 };
}
lua_pop(L, 1);
return { s,0 };
}
std::pair<int, bool> lua_gettableint(lua_State* L, std::string key) {
lua_pushstring(L, key.c_str());
lua_gettable(L, 1);
int res = NULL;
if (lua_type(L, -1) != LUA_TNIL) {
res = static_cast<int>(lua_tonumber(L, -1));
lua_pop(L, 1);
return { res,1 };
}
lua_pop(L, 1);
return { res,0 };
}
std::pair<float, bool> lua_gettablefloat(lua_State* L, std::string key) {
lua_pushstring(L, key.c_str());
lua_gettable(L, 1);
float res = NULL;
if (lua_type(L, -1) != LUA_TNIL) {
res = static_cast<float>(lua_tonumber(L, -1));
lua_pop(L, 1);
return { res,1 };
}
lua_pop(L, 1);
return { res,0 };
}
std::pair<std::array<float,2>, bool> lua_gettablearray(lua_State* L, std::string key) {
lua_pushstring(L, key.c_str());
lua_gettable(L, 1);
std::array<float, 2> res = { 0,0 };
if (lua_type(L, -1) != LUA_TNIL) {
for (int i = 0; i < 2; i++) {
lua_pushnumber(L, i + 1);
lua_gettable(L, -2);
res[i] = static_cast<float>(lua_tonumber(L, - 1));
lua_pop(L, 1);
}
lua_pop(L, 1);
return { res,1 };
}
lua_pop(L, 1);
return { res,0 };
}
#endif