[NEW] Multiple lua files

This commit is contained in:
2022-09-19 17:28:43 +02:00
parent 664710d59a
commit aad3ba639a
4 changed files with 41 additions and 20 deletions

46
lua.cpp
View File

@@ -585,16 +585,7 @@ bool lua_is_playing() {
return is_playing;
}
void lua_init() {
L = luaL_newstate();
if (luaL_loadfile(L, "game.lua")) {
debug("error loading game");
debug(lua_tostring(L, -1));
lua_pop(L,1);
return;
}
void push_lua_funcs() {
lua_pushcfunction(L,cpp_newsurf); lua_setglobal(L, "newsurf");
lua_pushcfunction(L,cpp_loadsurf); lua_setglobal(L, "loadsurf");
lua_pushcfunction(L,cpp_freesurf); lua_setglobal(L, "freesurf");
@@ -785,14 +776,37 @@ void lua_init() {
lua_pushinteger(L, 0); lua_setglobal(L, "FILE_READ");
lua_pushinteger(L, 1); lua_setglobal(L, "FILE_WRITE");
}
if (lua_pcall(L,0, LUA_MULTRET, 0)) {
debug("runtime error");
debug(lua_tostring(L, -1));
lua_pop(L,1);
return;
}
void lua_init(char* filenames) {
L = luaL_newstate();
push_lua_funcs();
char *pointer = filenames;
do {
// Get next filename...
char *file_start=pointer;
while (*pointer!=',' && *pointer!=0) pointer++;
if (*pointer!=0) *(pointer++)=0;
// Load and execute file
if (luaL_loadfile(L, file_start)) { // "game.lua"
debug("error loading game");
debug(lua_tostring(L, -1));
lua_pop(L,1);
return;
}
if (lua_pcall(L,0, LUA_MULTRET, 0)) {
debug("runtime error");
debug(lua_tostring(L, -1));
lua_pop(L,1);
return;
}
if (*pointer!=0) *(pointer-1)=',';
} while (*pointer!=0);
// Check if _init and _update exist
lua_getglobal(L, "_init");
if (lua_isfunction(L,-1)) init_exists = true;
lua_pop(L,1);