WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   How to send arguments in OnEvent? (https://www.wowinterface.com/forums/showthread.php?t=58372)

lungdesire 11-07-20 04:42 PM

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)


Fizzlemizz 11-07-20 05:29 PM

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.

SDPhantom 11-07-20 06:24 PM

Quote:

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).

lungdesire 11-08-20 03:58 AM

Thanks, but how send "hello" and "bye" from the loop in OnEvent?

Quote:

Originally Posted by Fizzlemizz (Post 337540)
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.


Xrystal 11-08-20 06:26 AM

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.

Fizzlemizz 11-08-20 07:03 AM

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.

lungdesire 11-08-20 02:56 PM

Thanks everyone for help.


All times are GMT -6. The time now is 07:23 AM.

vBulletin © 2021, Jelsoft Enterprises Ltd
© 2004 - 2020 MMOUI