From 57a2bd056c6c0e61cd26490a2ca653f5720d60ea Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Sat, 26 Nov 2011 03:40:16 +0200 Subject: [PATCH] Exception handling in Lua setting get --- data/mods/default/init.lua | 3 ++- src/scriptapi.cpp | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/data/mods/default/init.lua b/data/mods/default/init.lua index c6f03938..8f8188df 100644 --- a/data/mods/default/init.lua +++ b/data/mods/default/init.lua @@ -1316,7 +1316,8 @@ minetest.register_on_respawnplayer(function(player) end) -- Example setting get -print("max_users = " .. dump(minetest.setting_get("max_users"))) +print("setting max_users = " .. dump(minetest.setting_get("max_users"))) +print("setting asdf = " .. dump(minetest.setting_get("asdf"))) -- -- Done, print some random stuff diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index f1f0658f..af6e1779 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -861,8 +861,12 @@ static int l_register_on_respawnplayer(lua_State *L) static int l_setting_get(lua_State *L) { const char *name = luaL_checkstring(L, 1); - std::string value = g_settings->get(name); - lua_pushstring(L, value.c_str()); + try{ + std::string value = g_settings->get(name); + lua_pushstring(L, value.c_str()); + } catch(SettingNotFoundException &e){ + lua_pushnil(L); + } return 1; } @@ -870,8 +874,12 @@ static int l_setting_get(lua_State *L) static int l_setting_getbool(lua_State *L) { const char *name = luaL_checkstring(L, 1); - bool value = g_settings->getBool(name); - lua_pushboolean(L, value); + try{ + bool value = g_settings->getBool(name); + lua_pushboolean(L, value); + } catch(SettingNotFoundException &e){ + lua_pushnil(L); + } return 1; } -- 2.30.2