View Single Post
08-20-17, 07:37 PM   #1
stako
A Deviate Faerie Dragon
 
stako's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 11
Function to setup dragging & save the vars

Hello,

I'm sure everybody is familiar with making a frame movable and saving its position by doing something like this:

Lua Code:
  1. frame:SetScript("OnMouseDown", function()
  2.     if not frame.isMoving then
  3.         frame:StartMoving()
  4.         frame.isMoving = true
  5.     end
  6. end)
  7.  
  8. frame:SetScript("OnMouseUp", function()
  9.     if frame.isMoving then
  10.         frame:StopMovingOrSizing()
  11.         frame.isMoving = false
  12.        
  13.         MyAddon.db.profile.position = {frame:GetPoint()}
  14.     end
  15. end)

Now I'm doing this for numerous frames, so I'm trying to make a function out of it:

Lua Code:
  1. function MyAddon:SetupDrag(frame, dbsetting)
  2.     frame:SetScript("OnMouseDown", function()
  3.         if not frame.isMoving then
  4.             frame:StartMoving()
  5.             frame.isMoving = true
  6.         end
  7.     end)
  8.  
  9.     frame:SetScript("OnMouseUp", function()
  10.         if frame.isMoving then
  11.             frame:StopMovingOrSizing()
  12.             frame.isMoving = false
  13.            
  14.             -- Parameters get passed as values, so this won't work
  15.             -- dbsetting = {frame:GetPoint()}
  16.            
  17.             -- This works
  18.             dbsetting[1], dbsetting[2], dbsetting[3], dbsetting[4], dbsetting[5] = frame:GetPoint()
  19.         end
  20.     end)
  21. end

And example function call:
MyAddon:SetupDrag(ExampleFame, MyAddon.db.profile.position)

This works a little. Once I reset or change Ace profiles, it stops functioning. I think after the profile changes, MyAddon.db.profile.position will reference properly, but dbsetting will not?

Can't figure out a fix for that. Any ideas?
  Reply With Quote