Quantcast LoTROInterface - View Single Post - Reading Chat in LUA
View Single Post
Unread 11-16-2015, 05:45 AM
Garan's Avatar
Garan Garan is offline
The Undying
Interface Author - Click to view interfaces
Join Date: Oct 2010
Posts: 270
Before addressing how to match a specific chat message, I would like to point out that there are two errors in the code snippet above. The first is relatively minor but would still cause the code to fail - Lua is case sensitive, the line "count = Count + 1" will not work as intended since the second "Count" starts with a capital letter. I also assume you initialize the value somewhere otherwise you will get an error about adding 1 to nil.

The second error is the more significant error and will not only affect your plugin but any other plugin in the same environment. The line that starts "ChatMonitor.Recieved = " assigns a specific value to a shared object. All Turbine objects are shared and their event handlers should never be assigned a specific function or you will clobber any other already loaded plugin's event handlers. Additionally, the Lua environment does not always exit nicely if event handlers are not removed prior to plugins unloading. To avoid this, we use a pair of functions, "AddCallback" and "RemoveCallback" to allow multiple plugins to share event handlers and to safely clean up prior to unloading. This is all discussed in greater detail in the post:

As to matching a specific message, that is fairly simple. The code snippet you posted already tests for a specific channel. The only thing left to do is add a comparison for the message:
if args.Message=="some message" then
-- do something

It is generally more desirable to match a "pattern" or sub string of the message. That gets a tiny bit more complicated as you use the string.match() function and Lua patterns (similar to but not identical to Regular Expressions):
matches={string.match(args.Message,"some pattern")}
Lua patterns are described in greater detail in section 5.4.1 of the online Lua 5.1 manual:
Reply With Quote