WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Misplaced arguments? (https://www.wowinterface.com/forums/showthread.php?t=52014)

Leapin 03-02-15 04:12 AM

Misplaced arguments?
 
So I'm working on my first Lua script and I thought I'd make a simple addon without any XML and without the need of extra frames and stuff, either way, what I am trying to do is whenever someone joins the guild, it will announce in gchat "Welcome name", however, for some reason, the "msg" argument seem to be gone from the function. The event says the args are message, sender, language, channel, target etc etc, but when I try to call the first argument, instead of message, it's sender.

Code:

local gAuto = CreateFrame("Frame")

gAuto:RegisterEvent("CHAT_MSG_GUILD")
gAuto:SetScript("OnEvent", function(self, event, ...)
 self[event](...)
end)

        function gAuto:CHAT_MSG_GUILD(msg, sender, ...) -- msg = senderName instead of message, and sender = language instead of sender name?
                local str = "has joined the guild"
                name = string.gsub(sender, "-TarrenMill", "")
                name = string.gsub(sender, "-Dentarg", "")
                local welcome = "Välkommen "..name.." :)"
               
                --[[
                if string.find(msg, str) then
                                SendChatMessage(welcome, "GUILD")
                else
                        return
                end
                --]]
        end

I am really lost, I have no idea what I am doing wrong, any help is appreciated :)

Elkano 03-02-15 04:44 AM

Your call self[event](...) is the culprit. It resullts in the call self.CHAT_MSG_GUILD(...) instead of self:CHAT_MSG_GUILD(...). The latter is equivalent to self.CHAT_MSG_GUILD(self, ...).

So simply changing self[event](...) to self[event](self, ...) should do the trick.

Leapin 03-02-15 05:00 AM

Quote:

Originally Posted by Elkano (Post 307155)
Your call self[event](...) is the culprit. It resullts in the call self.CHAT_MSG_GUILD(...) instead of self:CHAT_MSG_GUILD(...). The latter is equivalent to self.CHAT_MSG_GUILD(self, ...).

So simply changing self[event](...) to self[event](self, ...) should do the trick.

That seem to have fixed it, I am still curious, why do we write "self[event]()" instead of "self:event()"?

Elkano 03-02-15 05:28 AM

The following three are equivalent:
- self.event(self)
- self:event()
- self["event"](self)

if however event is a variable (in contrast to the string "event"), your only way of calling is
- self[event]()

Leapin 03-02-15 05:39 AM

Quote:

Originally Posted by Elkano (Post 307157)
The following three are equivalent:
- self.event(self)
- self:event()
- self["event"]()

if however event is a variable (in contrast to the string "event"), your only way of calling is
- self[event]()

Ah yes, ofcourse. Thank you :)


All times are GMT -6. The time now is 04:19 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI