allow to show value of variables and properties in dialog text and option text

This commit is contained in:
Sokomine 2024-12-21 22:53:09 +01:00
parent 1b45fc7792
commit 68e37e24b8
2 changed files with 36 additions and 1 deletions

View File

@ -24,6 +24,30 @@ yl_speak_up.replace_vars_in_text = function(text, dialog, pname)
PLAYER_NAME = pname,
}
-- only try to replace variables if there are variables inside the text
if(string.find(text, "$VAR ")) then
local varlist = yl_speak_up.get_quest_variables(dialog.npc_owner, true)
for i,v in ipairs(varlist) do
local v_name = string.sub(v, 3)
-- only allow to replace unproblematic variable names
if(not(string.find(v_name, "[^%w^%s^_^%-^%.]"))) then
-- remove leading $ from $ var_owner_name var_name
subs["VAR "..v_name] = yl_speak_up.get_quest_variable_value(dialog.npc_owner, v) or "- not set -"
end
end
end
-- only replace properties if any properties are used inside the text
if(string.find(text, "$PROP ")) then
local properties = yl_speak_up.get_npc_properties(pname)
for k,v in pairs(properties) do
-- only allow to replace unproblematic property names
if(not(string.find(k, "[^%w^%s^_^%-^%.]"))) then
subs["PROP "..k] = v
end
end
end
local day_time_name = "day"
local day_time = minetest.get_timeofday()
if(day_time < 0.5) then
@ -41,7 +65,9 @@ yl_speak_up.replace_vars_in_text = function(text, dialog, pname)
-- substitutions in it using substring captured by "()" in
-- pattern. "[%a_]+" means one or more letter or underscore.
-- If lookup returns nil, then no substitution is made.
text = string.gsub(text or "", "%$([%a_]+)%$", subs)
-- Note: Names of variables may contain alphanumeric signs, spaces, "_", "-" and ".".
-- Variables with other names cannot be replaced.
text = string.gsub(text or "", "%$([%w%s_%-%.]+)%$", subs)
return text
end

View File

@ -254,6 +254,15 @@ The replacements will not be applied in edit mode.
Servers can define [additional custom replacements](#add-simple-variables).
It is also possible to insert the *value* of variables into the text. Only variables the owner of the NPC has read access to can be replaced. Example: The variable "Example Variable Nr. 3" (without blanks would be a better name, i.e. "example\_variable\_nr\_3"), created by "exampleplayer", could be inserted by inserting the following text into dialog texts and options:
$VAR exampleplayer Example Variable Nr. 3$
It will be replaced by the *value* that variable has for the player that is talking to the NPC.
Properties can be replaced in a similar way. The value of the property "job" of the NPC for example could thus be shown:
$PROP job$
Variables and properties can only be replaced if their *name* contains only alphanumeric signs (a-z, A-Z, 0-9), spaces, "\_", "-" and/or ".".
### 1.8 Alternate Text
<a name="alternate_text"></a>