forked from Sokomine/yl_speak_up
reduce number of gsub calls for text variable substitutions
Use lua feature that allows to specify table with substitutions to string.gsub()
This commit is contained in:
parent
706f6d7436
commit
b3c8889b95
@ -3112,11 +3112,12 @@ end
|
|||||||
-- replace some variables in the text the NPC speaks and which the player can use to reply
|
-- replace some variables in the text the NPC speaks and which the player can use to reply
|
||||||
-- pname: the name of the player that is talking to the NPC
|
-- pname: the name of the player that is talking to the NPC
|
||||||
yl_speak_up.replace_vars_in_text = function(text, dialog, pname)
|
yl_speak_up.replace_vars_in_text = function(text, dialog, pname)
|
||||||
-- Note: the $ char is a special one. It needs to be escaped with %$ in lua.
|
local subs = {
|
||||||
text = string.gsub(text, "%$MY_NAME%$", dialog.n_npc)
|
MY_NAME = dialog.n_npc,
|
||||||
text = string.gsub(text, "%$NPC_NAME%$", dialog.n_npc)
|
NPC_NAME = dialog.n_npc,
|
||||||
text = string.gsub(text, "%$OWNER_NAME%$", dialog.npc_owner)
|
OWNER_NAME = dialog.npc_owner,
|
||||||
text = string.gsub(text, "%$PLAYER_NAME%$", pname)
|
PLAYER_NAME = pname,
|
||||||
|
}
|
||||||
|
|
||||||
local day_time_name = "day"
|
local day_time_name = "day"
|
||||||
local day_time = minetest.get_timeofday()
|
local day_time = minetest.get_timeofday()
|
||||||
@ -3127,8 +3128,15 @@ yl_speak_up.replace_vars_in_text = function(text, dialog, pname)
|
|||||||
else
|
else
|
||||||
day_time_name = "evening"
|
day_time_name = "evening"
|
||||||
end
|
end
|
||||||
text = string.gsub(text, "%$GOOD_DAY%$", "Good "..day_time_name)
|
subs.GOOD_DAY = "Good "..day_time_name
|
||||||
text = string.gsub(text, "%$good_DAY%$", "good "..day_time_name)
|
subs.good_DAY = "good "..day_time_name
|
||||||
|
|
||||||
|
-- Note: the $ char is a special one. It needs to be escaped with %$ in lua.
|
||||||
|
-- Note: when substitution argument is a table, we look up
|
||||||
|
-- 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, "%$([%a_]+)%$", subs)
|
||||||
|
|
||||||
return text
|
return text
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user