Thread Tools Display Modes
04-06-22, 01:39 PM   #1
cosmic_kid
A Murloc Raider
Join Date: Oct 2021
Posts: 9
Question 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)
  Reply With Quote
04-06-22, 02:21 PM   #2
Ketho
A Pyroguard Emberseer
 
Ketho's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,026
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")

Last edited by Ketho : 04-06-22 at 03:38 PM.
  Reply With Quote
04-06-22, 02:47 PM   #3
cosmic_kid
A Murloc Raider
Join Date: Oct 2021
Posts: 9
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

Last edited by cosmic_kid : 04-06-22 at 02:50 PM.
  Reply With Quote
04-06-22, 03:13 PM   #4
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
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>
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
04-06-22, 03:21 PM   #5
cosmic_kid
A Murloc Raider
Join Date: Oct 2021
Posts: 9
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!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Frame:SetScript() Not Working?

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