Lua: allow functions for some optional strings

Those places that use get_table_str_opt() to get an optional string
value can instead use a function that returns a string.
This can be used for example in quest data lua table, or some table
fields in the lua api bindings, or the dungeon definition.

For example, in quest.lua

text = "You again sense %l pleading for help.",

could be replaced with

text = function() return "You again sense %l pleading for help."; end,

which of course allows using lua to build the string.
This commit is contained in:
Pasi Kallinen
2025-03-16 20:29:28 +02:00
parent 53cbccdb86
commit 7c43654580
+10 -1
View File
@@ -1021,9 +1021,18 @@ char *
get_table_str_opt(lua_State *L, const char *name, char *defval) get_table_str_opt(lua_State *L, const char *name, char *defval)
{ {
const char *ret; const char *ret;
int ltyp;
lua_getfield(L, -1, name); lua_getfield(L, -1, name);
ret = luaL_optstring(L, -1, defval); ltyp = lua_type(L, -1);
if (ltyp == LUA_TSTRING || ltyp == LUA_TNIL) {
ret = luaL_optstring(L, -1, defval);
} else if (ltyp == LUA_TFUNCTION) {
nhl_pcall_handle(L, 0, 1, "get_table_str_opt", NHLpa_panic);
ret = luaL_optstring(L, -1, defval);
} else {
nhl_error(L, "get_table_str_opt: no string");
}
if (ret) { if (ret) {
lua_pop(L, 1); lua_pop(L, 1);
return dupstr(ret); return dupstr(ret);