Quantcast
How to send arguments in OnEvent? - WoWInterface
 
Thread Tools Display Modes
11-07-20, 04:42 PM   #1
lungdesire
A Deviate Faerie Dragon
Join Date: May 2020
Posts: 13
How to send arguments in OnEvent?

Hello everyone. How to send arguments in OnEvent?

Code:
h = { "hello", "bye" }

for key, val in pairs(h) do
-- How send "hello" and "bye" in OnEvent ? 
end

local function OnEvent(self, event, ...)
print(arg1)
end

local f = CreateFrame("Frame")
f:RegisterEvent("PLAYER_ENTERING_WORLD")
f:SetScript("OnEvent", OnEvent)

Last edited by lungdesire : 11-08-20 at 04:20 AM.
  Reply With Quote
11-07-20, 05:29 PM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,429
The arguments (payload) part of OnEvent is the ...
It means a variable number of arguments (each event has zero to lots or arguments depending on the event received)

Lua Code:
  1. Frame:SetScript("OnEvent", function(self, event, ...)
  2.     local arg1, arg2, arg3 = ...
  3.     print(arg1, arg3)
  4. end)

You can see for the PLAYER_ENTERING_WORLD event, it contains two arguments in its payload, isInitalLogin and isReloadUI so you could:

Lua Code:
  1. Frame:SetScript("OnEvent", function(self, event, ...)
  2.     if event == "PLAYER_ENTERING_WORLD" then
  3.         local isInitalLogin, isReloadUI = ...
  4.     elseif event == "BAG_OPEN" then
  5.         local bagID = ...
  6.     end
  7. end)
  8. Frame:RegisterEvent("PLAYER_ENTERING_WORLD")
  9. Frame:RegisterEvent("BAG_OPEN")
for example, to make it more readable what the arguments are for a particular event.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 11-07-20 at 05:58 PM.
  Reply With Quote
11-08-20, 03:58 AM   #3
lungdesire
A Deviate Faerie Dragon
Join Date: May 2020
Posts: 13
Thanks, but how send "hello" and "bye" from the loop in OnEvent?

Originally Posted by Fizzlemizz View Post
The arguments (payload) part of OnEvent is the ...
It means a variable number of arguments (each event has zero to lots or arguments depending on the event received)

Lua Code:
  1. Frame:SetScript("OnEvent", function(self, event, ...)
  2.     local arg1, arg2, arg3 = ...
  3.     print(arg1, arg3)
  4. end)

You can see for the PLAYER_ENTERING_WORLD event, it contains two arguments in its payload, isInitalLogin and isReloadUI so you could:

Lua Code:
  1. Frame:SetScript("OnEvent", function(self, event, ...)
  2.     if event == "PLAYER_ENTERING_WORLD" then
  3.         local isInitalLogin, isReloadUI = ...
  4.     elseif event == "BAG_OPEN" then
  5.         local bagID = ...
  6.     end
  7. end)
  8. Frame:RegisterEvent("PLAYER_ENTERING_WORLD")
  9. Frame:RegisterEvent("BAG_OPEN")
for example, to make it more readable what the arguments are for a particular event.
  Reply With Quote
11-08-20, 06:26 AM   #4
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,641
What are you trying to achieve ?

You do not send values to events. The values are returned from events for you to process as needed.

If you want to debug the OnEvent function use the print(xxxx) function to output text/values etc to identify a problem in the addon.
__________________
  Reply With Quote
11-07-20, 06:24 PM   #5
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,050
When a function is called, the list of arguments is adjusted to the length of the list of parameters, unless the function is a vararg function, which is indicated by three dots ('...') at the end of its parameter list. A vararg function does not adjust its argument list; instead, it collects all extra arguments and supplies them to the function through a vararg expression, which is also written as three dots. The value of this expression is a list of all actual extra arguments, similar to a function with multiple results. If a vararg expression is used inside another expression or in the middle of a list of expressions, then its return list is adjusted to one element. If the expression is used as the last element of a list of expressions, then no adjustment is made (unless that last expression is enclosed in parentheses).
__________________
ESOUI AddOns | WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 11-09-20 at 11:18 AM.
  Reply With Quote
11-08-20, 07:03 AM   #6
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,429
Lua Code:
  1. local h = { "hello", "bye" }
  2.  
  3. local function OnEvent(self, event, ...)
  4.     for key, val in ipairs(h) do
  5.         print(val)
  6.     end
  7.     -- prints:
  8.     -- hello
  9.     -- bye
  10.     -- or:
  11.     print(h[1]) -- prints hello
  12.     print(h[2]) -- prints bye
  13. end
  14.  
  15. local f = CreateFrame("Frame")
  16. f:RegisterEvent("PLAYER_ENTERING_WORLD")
  17. f:SetScript("OnEvent", OnEvent)

That will print hello and bye in your chat frame but only you will be able to see it. If you want to broadcast a message to others in the game like a /say, you would need to use SendChatMessage instead of print.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 11-08-20 at 02:01 PM.
  Reply With Quote
11-08-20, 02:56 PM   #7
lungdesire
A Deviate Faerie Dragon
Join Date: May 2020
Posts: 13
Thanks everyone for help.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » How to send arguments in OnEvent?

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off