• This is a read only backup of the old Emudevs forum. If you want to have anything removed, please message me on Discord: KittyKaev

Eluna and Client Addons!

Foereaper

Founder
Earlier today we pushed a new hook and a new method to the repo. Secifically the hook OnAddonMessage, and the method player:SendAddonMessage. These two now allows us to create extremely powerful and easy binding for two way communication between the client (addons) and the server (scripts).

wJaLyi7.png


SERVER SIDE:
The hook is a Server hook, and is as follows:
Code:
ADDON_EVENT_ON_MESSAGE                  =     30,       // (event, plr, prefix, msg, type, target)

This is registered with the normal RegisterServerEvent hook.

The method, SendAddonMessage, allows you to send addon messages to the player.
Code:
PlayerObject:SendAddonMessage("Prefix", "Message", type, PlayerObject)

The types available to use with this are as follows:
Code:
CHAT_MSG_PARTY                  = 2,
CHAT_MSG_RAID                   = 3,
CHAT_MSG_GUILD                  = 4,
CHAT_MSG_WHISPER                = 7,
CHAT_MSG_BATTLEGROUND           = 44,


For this reason, I have also created a sample script, and a sample addon, so you all can see how easy you can integrate the server side Lua API with client side UI and functions.

Server-side script:
Code:
-- /script SendAddonMessage("Eluna", "Ping", "GUILD")

local MName = "[ServerAddonHandler]"
print(MName.." Loaded")
local pfx = "Eluna"

function OnReceiveAddonMsg(event, plr, prefix, msg, type, target)
	if (prefix == pfx) then
		if (msg == "Ping") then
			print(MName..": Ping received by "..plr:GetName().."! Sending Pong.")
			plr:SendAddonMessage(pfx, "Pong", type, plr)
		elseif(msg == "GoldRequest") then
			print(MName..": Gold request received from Client.")
			plr:ModifyMoney(100)
		else
			print(MName..": Received unknown msg: "..msg)
		end
	else
		print(MName..": Received unknown prefix: "..prefix)
	end
end

RegisterServerEvent(30, OnReceiveAddonMsg)

The above code is the script that goes into your core folder. It reads the message sent from the client using SendAddonMessage. The Prefix is your addon's identifier. In this example we use Eluna. The msg is the message sent between the client and the server, and this determines what actions are to be taken. The player is also the sender of the said message. This can be used for several security checks, like checking if the individual is a GM or has the correct access or not.




CLIENT SIDE:
For the client you would use the standard CHAT_MSG_ADDON event: http://www.wowwiki.com/Events/C#CHAT_MSG_ADDON
You would also use the Client-side SendAddonMessage method to communicate with the server side script: http://www.wowwiki.com/API_SendAddonMessage

Client-side script (Addon):
Code:
local MName = "[ClientAddonHandler]"
local pfx = "Eluna"

local function CommHandler(self, event, prefix, msg, channel, me)
	if (prefix == pfx) then
		if(msg == "Pong") then
			print(MName..": Received Pong! Sending Gold request to server!")
			SendAddonMessage(pfx, "GoldRequest", "GUILD")
		end
	end
end

local ElunaComm = CreateFrame("Frame");
ElunaComm:RegisterEvent("CHAT_MSG_ADDON");
ElunaComm:SetScript("OnEvent", CommHandler);

The above script is the client-side lua script. As said earlier, this uses the standard CHAT_MSG_ADDON event, and has the same arguments as the server side function. You use prefix for addon identifier, and msg for the communication between the server and the client. self in this case is the player.


How to use the above example:

To use the above example, download the addon and place it inside your interface/addon folder. Download the server-side script and save this inside your lua_script folder. Then go ingame and type the below:
Code:
/script SendAddonMessage("Eluna", "Ping", "GUILD")

NOTE: This requires you to be in a guild, as the addon message uses the GUILD channel. This will also add 1 silver to your character.

The above will trigger the "Eluna" identifier, with the command "Ping". "Ping" will then trigger the event "Pong", which again triggers "GoldRequest". The "GoldRequest" event then gives 1 silver to the player. Pretty straight forward, but still an example as to how this can be used extremely well for client side addons requiring server side scripts!

You can find the downloads below:
Server-side script: http://pastebin.com/RZvwVQiG
Client-side addon: https://mega.co.nz/#!AsUTwaBR!cThDtnCnlYlH_wQflg_w4uxujBqRkEO0C3QKssSU-Fg

Do mind this is an extremely simple representation, but I seriously hope we'll see some cool tools using this in the near future! Don't hesitate asking questions or come with suggestions for improvements!

Special thanks to:
Tommy
Rochet
Laurea
StoneHarry

https://github.com/ElunaLuaEngine/Eluna/commit/cf9bb869e315c2a6b6d3d49d64a08c656f9ac4e9
https://github.com/ElunaLuaEngine/Eluna-TC-Wotlk/commit/c6f3b4aa5ad65afd5f2c3bc537f70af89a1e746c
 

Serrak

Enthusiast
I've been trying to debug this for a few hours and feel like I'm missing something completely simple. But I'm running 1.12.1 Cmangos and everytime I put the command in on the client, the server receives "Recieved unknown prefix: 3" I just can't understand where it's pulling the 3 from.

With a bit more of messing around I've discovered that my server is defining the prefix as 3, the msg as Eluna, and the type as ping. Just not quite sure why though.
 
Last edited:

Rochet2

Moderator / Eluna Dev
I've been trying to debug this for a few hours and feel like I'm missing something completely simple. But I'm running 1.12.1 Cmangos and everytime I put the command in on the client, the server receives "Recieved unknown prefix: 3" I just can't understand where it's pulling the 3 from.

With a bit more of messing around I've discovered that my server is defining the prefix as 3, the msg as Eluna, and the type as ping. Just not quite sure why though.

Here are the args:
ADDON_EVENT_ON_MESSAGE = 30, // (event, sender, type, prefix, msg, target) - target can be nil/whisper_target/guid/group/channel
https://github.com/ElunaLuaEngine/Eluna/blob/master/HookMgr.h#L87

Post if you still have problems.
(Im in a rush atm)

Seems there is a typo in the comment .. should be guild and not guid.
Ah well.
 

Serrak

Enthusiast
Another thing I'm looking at is how would I transfer other variables from addons for the server to see? Does this code allow for a numerical variable to be read along with the msg?
 

Foereaper

Founder
The best way would be for you to either send more messages specifically for the variables, or use a deliminator. Example:

Msg = var1=1|var2=2|var3=3

You'd then use string checking and replacement (gsub etc) to split the messages. | is the most common deliminator to use. Then you could push that to a local cache table.
 
Top