forked from Sokomine/yl_speak_up
Merge pull request 'reduce number of gsub calls for text variable substitutions' (#4) from whosit/yl_speak_up:trade_list into trade_list
Reviewed-on: Sokomine/yl_speak_up#4
This commit is contained in:
commit
4ab2ee03f1
@ -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