Exception handling in Lua setting get
authorPerttu Ahola <celeron55@gmail.com>
Sat, 26 Nov 2011 01:40:16 +0000 (03:40 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Tue, 29 Nov 2011 17:13:53 +0000 (19:13 +0200)
data/mods/default/init.lua
src/scriptapi.cpp

index c6f039385afb07ed5f9e616839bcba8d72a74222..8f8188df551b0432eb2f4468ca1985c94a1155fd 100644 (file)
@@ -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
index f1f0658f4232ebcdd644f4301d38c15caaa0e363..af6e1779f91a2cb44a439bce21579104314a35ce 100644 (file)
@@ -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;
 }