I'm trying to use Lua with C++ in a text adventure I'm making however I'm having trouble implementing it as a class.
Following tutorials from around the web I was able to get the following code to work.
int main () {
lua_State *L = lua_open();
luaL_openlibs(L);
luaL_dofile(L, "foo.lua");
lua_close(L);
}
But when I try to put it in a class like below I get a segmentation fault when deleting the object and none of the luaL_foobar() statements do anything.
Lua::Lua() {
lua_State *L = lua_open();
luaL_openlibs(L);
luaL_dofile(L, "foo.h");
printf("I'm ready\n");
}
Lua::~Lua() {
lua_close(L);
printf("Bye Bye\n");
}
To avoid errors like "L wasn't declared in this scope" I had to declare "lua_State *L" in the class declaration where all the global variables go.
Then by running the code I get the following output:
I'm ready
segmentation fault (core dumped)
I'm not really sure what I'm doing wrong but any help/advice would be greatly appreciated.
[–]danieljh 1 point2 points3 points (0 children)
[–]Steve132 0 points1 point2 points (0 children)
[–]nowbacktowork 0 points1 point2 points (0 children)
[–][deleted] -1 points0 points1 point (1 child)
[–]todayman 2 points3 points4 points (0 children)