Jump to content
AKtor

Why the event processing does not work correctly in my code?

Recommended Posts

Hi everybody.

I try to write an addon. Need to handle some events, for this I created a frame. In the Internet there are many examples of code similar to this one:

local frame = CreateFrame("Frame");

frame:RegisterEvent("PLAYER_ENTERING_WORLD");
frame:RegisterEvent("PLAYER_REGEN_DISABLED");
frame:RegisterEvent("PLAYER_REGEN_ENABLED");

frame:SetScript("OnEvent", function(self, event, ...)
    DEFAULT_CHAT_FRAME:AddMessage("OnEvent");   --[[Message is displayed--]]

    if event == "PLAYER_ENTERING_WORLD" then
        DEFAULT_CHAT_FRAME:AddMessage("entering the world");   --[[Condition does not triggers --]]
    elseif event == "PLAYER_REGEN_DISABLED" then
        DEFAULT_CHAT_FRAME:AddMessage("entering combat");   --[[Condition does not triggers--]]
    elseif event == "PLAYER_REGEN_ENABLED" then
        DEFAULT_CHAT_FRAME:AddMessage("leaving combat");   --[[Condition does not triggers--]]
    end;
end);

The frame is created, subscribed to the events, the handler is triggered by these events, but the conditions in the handler do not work. What am I doing wrong?

However, everything works if I create a frame through XML, and there I hang a handler function on it. I can stop at this, but curiosity does not give rest - why the above code does not work?

Thanks for any help.

Share this post


Link to post
Share on other sites

In vanilla the script functions take no parameters. OnEvent use the global `event` variable, all other scripts use globals `arg1`, `arg2`, `arg3`, etc. Removing the paramenters of the function should be enough to have something working.

Also, global `this` variable is set instead of self.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×