Page 1 of 1

Allowing your AddOn to communicate with itself  [Accepted Answer]

Posted: Tue Jul 18, 2017 1:22 pm
by Darth_Predator
Originally posted by Hydra

Sounds goofy, right? Why do I want my AddOn talking to itself...?

Well, it may be handy for version checking, or whatever other reasons you can come up with. Really this is something I wanted to do ages ago and only just got around to actually writing, so here we go. To make an AddOn communicate with itself it uses a hidden addon channel. We can send messages through this channel with the function SendAddonMessage, and recieve messages by checking what's coming through the "CHAT_MSG_ADDON" event.

a sample:

Code: Select all

-- Communicate to other players through our AddOn (By Hydra)
local msg = "We've made communication with %s!" 
local SendRecieve = function(self, event, prefix, message, channel, sender)
 if event == "CHAT_MSG_ADDON" then
 	if (prefix ~= "HydraExample") then return end
 		print(format(message, sender))
 		self:UnregisterEvent("CHAT_MSG_ADDON") -- We got the message, don't need to look anymore
 		SendAddonMessage("HydraExample", msg, "PARTY") -- Can't stop the signal
local CheckMessages = CreateFrame("Frame")
CheckMessages:SetScript("OnEvent", SendRecieve)
The function "SendRecieve" is first checking if the event that has fired is "CHAT_MSG_ADDON", if so then check the prefix of the message (We'll get to the prefix in just a second). If the prefix of the sent message is not "HydraExample", then end the function right there, otherwise continue with the function and print the message that was broadcast.

In my example you'll notice I use

Code: Select all

print(format(message, sender))
If you don't know what format does, don't worry about it, it's not important to this guide.

So getting back to the function, if the event that has fired is NOT "CHAT_MSG_ADDON", then it will perform the function SendAddonMessage;

Code: Select all

SendAddonMessage("HydraExample", msg, "PARTY")
What does this do?
"HydraExample" -- This is my messages prefix, it's an identifier for my message being sent. This can be a maximum of 15 characters long
msg -- This is the message I want to broadcast, you can see I've declared "msg" locally at the top of my example as a string.
"PARTY" -- This is the AddOn channel I want broadcast to, Valid types are "PARTY", "RAID", "GUILD", "BATTLEGROUND", "WHISPER".

So upon entering the game world, joining a party group, a raid group, or when the members of that group change, you will send out a hidden AddOn message. And every single Addon message that you recieve will be scanned, and if the prefix matches your own it will print it out to your chat frame.

Again, why could this possibly be useful? Well... If your AddOn is popular (And I mean, it would have to be popular), it can be used as a method to tell people that they are using an older version of the AddOn in question, or maybe let them know some other piece of information. The downside is that it requires that you run into another player who is using the AddOn.

Maybe someone can make some use of this, or will find it interesting.

Cookies to anyone who actually understands the "Can't stop the signal" quote.