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).
SERVER SIDE:
The hook is a Server hook, and is as follows:
This is registered with the normal RegisterServerEvent hook.
The method, SendAddonMessage, allows you to send addon messages to the player.
The types available to use with this are as follows:
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:
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):
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:
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
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