Thread Tools Display Modes
08-08-14, 06:18 PM   #1
MaLarsson
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: Aug 2014
Posts: 13
Restoring frame to previous position

I'm currently working on an addon that tracks the dots on your target.
I can now display both Hemorrhage and Rupture and move the icons around but the problem is that when I /reload or relog the positioning of the frame is reset to the center of the screen.
I tried using saved variables but did not manage to get any good results even tho I waited until the ADDON_LOADED event fired to use the variable.
I've attached the source code.
DoTracker.lua
  Reply With Quote
08-08-14, 10:06 PM   #2
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
There's nothing in your code that would save or restore a position.

The simplest method would be to let the game handle it:
Code:
anchor:RegisterForDrag("LeftButton")
anchor:SetMovable(true)
anchor:SetUserPlaced(true)
anchor:SetScript("OnDragStart", anchor.StartMoving)
anchor:SetScript("OnDragStop", anchor.StopMovingOrSizing)
Or, if you want to do it yourself, then you need to save the position into your saved variables table in your OnDragStop script, and restore it when your addon loads.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
08-09-14, 10:36 AM   #3
MaLarsson
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: Aug 2014
Posts: 13
Thanks for the quick reply.

I tried to use the
Lua Code:
  1. anchor:SetUserPlaced(true)
but it did not work and after reading this page wowwiki I omitted that line from my code since:
There is usually no need to explicitly call this function; the flag is automatically set by Frame:StartMoving(), and when the frame's position is restored from the layout cache.
Is there something I'm missing? Do I need to add a saved variable in my .toc file for the SetUserPlaced method to work properly?
  Reply With Quote
08-09-14, 02:44 PM   #4
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
The frame needs to have a name, for one. (I didn't check your file) The only time you should need to call SetUserPlaced is when you want to save its position after positioning it with SetPoint.
__________________
Grab your sword and fight the Horde!

Last edited by Lombra : 08-09-14 at 02:47 PM.
  Reply With Quote
08-10-14, 01:10 PM   #5
MaLarsson
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: Aug 2014
Posts: 13
I did give the frame a name, this is how i declared all the frames:
Lua Code:
  1. local frame, events = CreateFrame("Frame", "frame", UIParent), {}
  2. local name, realm = nil
  3. local anchor = CreateFrame("Frame", "anchor", UIParent)
  4. local icons = CreateFrame("Frame", "icons", anchor)
  5. local cd = CreateFrame("Cooldown", "cd", icons)
Maybe giving it the same name as the local variable name is a problem.
I will try changing the code a bit when I get home from vacation.
I'll keep you updated when I do, thanks for all the help!
  Reply With Quote
08-10-14, 02:50 PM   #6
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
Hmm no, the local variable doesn't matter, but frame names are global, and you should absolutely not use such generic names for them. (and not name them at all if avoidable) Use 'DoTrackerFrame' for example. If there's a conflict with some other global variable I guess that might cause a mess. Other than that I'm not sure why it wouldn't work. See if you have an entry for the frame in your layout-cache.txt in the character WTF folder.
__________________
Grab your sword and fight the Horde!
  Reply With Quote
08-11-14, 02:27 AM   #7
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Ew, don't know how I missed that. Your frame names (the global names passed to CreateFrame, not the local variable names) are so generic it's likely that even if you get everything else working, some other addon (or even the default UI) will leak globals (hopefully accidentally) with the same names, screwing up your addon or vice versa.

Code:
local frame, events = CreateFrame("Frame", "DoTrackerFrame", UIParent), {}
local name, realm = nil
local anchor = CreateFrame("Frame", "DoTrackerAnchor", UIParent)
local icons = CreateFrame("Frame", "DoTrackerIcon", anchor)
local cd = CreateFrame("Cooldown", "DoTrackerCD", icons)
It's conceivable this is already affecting you; if some other addon is creating globals with the same names (again, hopefully by accident, since this is something you should never, ever do on purpose) then the game is probably not able to save and restore the positions of your frames, just as if they had no global names at all.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
08-11-14, 06:34 AM   #8
zork
A Pyroguard Emberseer
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 1,740
Just in case. Make sure that when using frame:SetUserPlaced(true) the frame is created when you initialize the addon and not via PLAYER_LOGIN event or the like.
__________________
| Simple is beautiful.
| WoWI AddOns | GitHub | Zork (WoW)

"I wonder what the non-pathetic people are doing tonight?" - Rajesh Koothrappali (The Big Bang Theory)
  Reply With Quote
08-11-14, 06:49 AM   #9
MaLarsson
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: Aug 2014
Posts: 13
Oh, didn't realize that the names where in the global namespace. Will fix this when I get home!

Thanks for all the feedback!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Restoring frame to previous position

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