Thread Tools Display Modes
03-03-09, 10:56 PM   #1
tsadok
An Aku'mai Servant
 
tsadok's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 32
Getting OnLoad to work

I'm converting my old XML file to Lua (something I don't understand to something I do). Here's the relevant code:

Code:
BGGeneral.primaryFrame = CreateFrame("Frame", "BGGMainFrame", UIParent)
BGGeneral.primaryFrame:EnableMouse(true)
BGGeneral.primaryFrame:SetMovable(true)
BGGeneral.primaryFrame:RegisterForDrag("LeftButton")
BGGeneral.primaryFrame:SetScript("OnLoad", BGGeneral.OnLoad)
BGGeneral.primaryFrame:SetScript("OnDragStart", function() self:StartMoving() end)
BGGeneral.primaryFrame:SetScript("OnDragStop", function()
    print("OnDragStop running...")
    self:StopMovingOrSizing()
    self.framePosX = self:GetLeft()
    self.framePosY = self:GetTop()
end)
The "OnLoad" and "OnDragStop" handlers never seem to execute. Am I missing something obvious? I can call BGGeneral.OnLoad() as the last statement in my last file, but that's not pretty or elegant.
  Reply With Quote
03-04-09, 03:00 AM   #2
IBLJerry
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: Dec 2006
Posts: 12
Originally Posted by tsadok View Post
The "OnLoad" and "OnDragStop" handlers never seem to execute. Am I missing something obvious? I can call BGGeneral.OnLoad() as the last statement in my last file, but that's not pretty or elegant.
OnLoad executes at Frame Creation, so you really can't set a OnLoad handler in lua. But that does not matter at all as you can simply call your function by hand.

Your OnDragStop doesn't get called because your OnDragStart handler is incorrect, and your Frame never gets dragged.

Code:
local f = CreateFrame("Frame", "BGGMainFrame", UIParent)
f:EnableMouse(true)
f:SetMovable(true)
f:RegisterForDrag("LeftButton")
-- f:SetScript("OnLoad", BGGeneral.OnLoad)
BGGeneral.OnLoad(f) -- let's just call it ourself

f:SetScript("OnDragStart", function(self) self:StartMoving() end)
f:SetScript("OnDragStop", function(self)
    print("OnDragStop running...")
    self:StopMovingOrSizing()
    self.framePosX = self:GetLeft()
    self.framePosY = self:GetTop()
end)
BGGeneral.primaryFrame = f
  Reply With Quote
03-04-09, 12:58 PM   #3
Akryn
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 479
I'm pretty sure I've done OnLoad successfully in Lua before, although now that I look I can't find an example of it..

Either way, note that
Code:
BGGeneral.primaryFrame:SetScript("OnLoad", BGGeneral.OnLoad)
will not work properly for any script unless BGGeneral.OnLoad is already defined at this point (i.e. higher in the file than this call) because SetScript passes the function itself to the frame which then keeps its own copy of it until you give it something else with another SetScript call.

Also, IBLJerry is correct that you need:
Code:
"OnDragStart", function(self)
"OnDragStop", function(self)

Last edited by Akryn : 03-04-09 at 01:02 PM.
  Reply With Quote
03-04-09, 06:35 PM   #4
tsadok
An Aku'mai Servant
 
tsadok's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 32
Ah, of course, it evalutates BGGeneral.OnLoad at that point which is nil. The strange thing is that the frame would actually move. Maybe RegisterForDrag had something to do with that.

And no colon means I have to specify 'self'.

Thank you!
  Reply With Quote
03-05-09, 03:58 AM   #5
tsadok
An Aku'mai Servant
 
tsadok's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 32
I've been trying out the new code, and found that the problem was that I was using a Title Region. This let the frame be dragged whether or not it was set as moveable or registered for clicks or whatever. I've changed the title region to a normal region and the frame drags around as it should and runs its OnDragStart and OnDragStop handlers.

Given that the only documentation on Frame:CreateTitleRegion() all leads back to Slouken's post, it's probably worth giving a miss if you can get your main frame dragging properly anyway.
  Reply With Quote
03-05-09, 04:36 AM   #6
Mera
Retired of WoW, In ESO :)
 
Mera's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Apr 2008
Posts: 331
registerfordrag is not mandatory and rarely used
__________________
If you need to reach me I'm in ESO, @class101 or "Fathis Ules i"
addons: SpamBayes, BrokerCPU
projects: ThunderBayes
Mera[xeh]? - La CroisadeEcarlate (wow)
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Getting OnLoad to work


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