01 | function ToucanBeak(source, find, replace, wholeword) |
02 | if wholeword then |
03 | find = '%f[%a]' ..find.. '%f[%A]' |
04 | end |
05 | return (source:gsub(find,replace)) |
06 | end |
07 |
08 | function WaveBeak(message,player) |
09 | local newmessage 1 = string.lower(message) |
10 | local newmessage 2 = ToucanBeak(newmessage 1 , "teemo" , "warbler" , false ) |
11 | local newmessage 3 = ToucanBeak(newmessage 2 , "teemoing" , "warbler maining" , false ) |
12 | end |
Basically ToucanBeak just subs the target word out for something out.
WaveBeak sets everything to lowercase, and then uses ToucanBeak to get the new message.
I intend to do a lot more with this - is there any way I can make it more efficient/use way less local variables?
Use a table. When you find yourself numbering variables, it means you just want a list.
You could use a list:
01 | local simple = { |
02 | { from = "teemo" , to = "warbler" } , |
03 | } |
04 | local whole = { |
05 | { from = "foo" , to = "bar" } , |
06 | } |
07 | for _, rule in pairs (simple) do |
08 | message = ToucanBeak(message, rule.from, rule.to, false ) |
09 | end |
10 | for _, rule in pairs (whole) do |
11 | message = ToucanBeak(message, rule.from, rule.to, true ) |
12 | end |
Also, by the nature of "find/replace" you could just use a dictionary:
01 | local simple = { |
02 | teemo = "warbler" , |
03 | } |
04 | local whole = { |
05 | foo = "bar" , |
06 | } |
07 |
08 | for from, to in pairs (simple) do |
09 | message = ToucanBeak(message, rule.from, rule.to, false ) |
10 | end |
11 | for from, to in pairs (whole) do |
12 | message = ToucanBeak(message, rule.from, rule.to, true ) |
13 | end |