WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Frame:SetScript() Not Working? (https://www.wowinterface.com/forums/showthread.php?t=59090)

cosmic_kid 04-06-22 01:39 PM

Frame:SetScript() Not Working?
 
I am about to lose my mind with this issue. I've done it a million different ways, but cannot for the life of me get this to work.

I'm simply trying to create an invisible frame, subscribe it to a few events and print those events when they fire.

Addon.toc

Code:

## Interface: 90200
## Title: |cff93E9BEAddon Title
## Version: 0.1
## Author: cosmic_kid
## SavedVariables: CKNdb
## DefaultState: enabled

Addon.lua

Addon.lua

Lua Code:
  1. local AddonParent = CreateFrame("Frame")
  2.  
  3. print("HELLOOOOOOOO") -- This prints
  4.  
  5. local OnLoad = function(self)
  6.     print("ONLOAD") -- This does not print
  7.     self:RegisterEvent("PLAYER_LOGIN")
  8.     self:RegisterEvent("PLAYER_ENTERING_WORLD")
  9.     self:RegisterEvent("ARENA_OPPONENT_UPDATE")
  10. end
  11.  
  12. local OnEvent = function(self, event, ...)
  13.     print(event) -- This does not print
  14. end
  15.  
  16. AddonParent:SetScript("OnEvent", OnEvent)
  17. AddonParent:SetScript("OnLoad", OnLoad)

Ketho 04-06-22 02:21 PM

You don't need to set the OnLoad script, that's only applicable when your frame is defined in XML
Lua Code:
  1. print("HELLOOOOOOOO") -- This prints
  2.  
  3. local OnEvent = function(self, event, ...)
  4.     print(event)
  5. end
  6.  
  7. local AddonParent = CreateFrame("Frame")
  8. AddonParent:SetScript("OnEvent", OnEvent)
  9. AddonParent:RegisterEvent("PLAYER_LOGIN")
  10. AddonParent:RegisterEvent("PLAYER_ENTERING_WORLD")
  11. AddonParent:RegisterEvent("ARENA_OPPONENT_UPDATE")

cosmic_kid 04-06-22 02:47 PM

Wow, that fixed it. Thank you!

Can you add any information as to why this was breaking my script? Like is the "OnLoad" script of AddonParent not even getting called if it's not defined in XML?

I tried to do this with XML at first, but I kept getting an error like "method AddonParent_OnLoad not found in element OnLoad" for an XML like this.

Code:

<Ui>
    <Script file="Addon.lua"/>
    <Frame name="AddonParent">
        <Scripts>
            <OnLoad method="AddonParent_OnLoad"/>
        </Scripts>
    </Frame>
</Ui>

Addon.lua
Lua Code:
  1. function AddonParent_OnLoad(self)
  2.     ...
  3. end

Fizzlemizz 04-06-22 03:13 PM

The method attribute is used if the script is a method of the frame (usually added to the frame via a mixin attribute included in the <Frame ...> tag).

Because your function is a global (and so long as the .lua file is listed in the .toc before the xml file) you would just call the function:

Lua Code:
  1. <Scripts>
  2.     <OnLoad>
  3.         AddonParent_OnLoad()
  4.     </OnLoad>
  5. </Scripts>

cosmic_kid 04-06-22 03:21 PM

That makes a lot of sense. I was using sArena as a reference and they have a mixin defined in their frame tag. Since I had the other attributes of the frame tag removed (except for name), it didn't know what to do. Thank you both!


All times are GMT -6. The time now is 09:44 AM.

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