WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Hooking into OnUpdate for moving unitframes (https://www.wowinterface.com/forums/showthread.php?t=39259)

DrGlenn 03-13-11 03:23 PM

Hooking into OnUpdate for moving unitframes
 
I've been fiddling with a very basic UI modification where I need to scale and move the default unitframes.

I initially just made a macro for re-scaling and re-positioning the unitframes, but wanted something more permanent to avoid having to run the macro after each loading screen. I tried using a wide variety of "move anything"-addons out there, but they all had issues with vehicle unitframes.

I tried to come up with my own solution, and ended up with the following bit of code, which actually works even with vehicle unitframes:

Code:

local timer = 2

local f = CreateFrame("Frame")



f:SetScript("OnUpdate", function(self, elapsed)

  timer = timer - elapsed

  if (timer > 0) then return end

 PlayerFrame:SetScale(1.1)

 PetFrame:SetScale(1.1)

 FocusFrame:SetScale(1.1)

 TargetFrame:SetScale(1.1)

 PlayerFrame:ClearAllPoints()

 PlayerFrame:SetPoint("CENTER", -250, -150)

 TargetFrame:ClearAllPoints()

 TargetFrame:SetPoint("CENTER", 250, -150)

 FocusFrame:ClearAllPoints()

 FocusFrame:SetPoint("CENTER", -500, -150)


 timer = timer + 2 -- or just timer = 5 if you don't need to be super exact

end)

My question is: would this be considered bad programming practice, considering the way I'm hooking into OnUpdate ? This is pretty much my first time working with LUA for WoW, so I'm worried that the code could be a resource-hog :(

Chibi 03-13-11 04:23 PM

You might try using one of the events related to entering the world or addons being loaded. Trying to do it on-update will try to do it every frame, and throw errors when it fires in combat for frames that can't be moved then.

bjp91 04-18-11 12:23 PM

Quote:

Originally Posted by Chibi (Post 231662)
You might try using one of the events related to entering the world or addons being loaded. Trying to do it on-update will try to do it every frame, and throw errors when it fires in combat for frames that can't be moved then.

Yeah the ideal way to set up this would be

Code:

local f = CreateFrame("Frame")
f:RegisterEvent("PLAYER_ENTERING_WORLD")
f:SetScript("OnEvent", function(self, event)
        PlayerFrame:SetScale(1.1)
        PetFrame:SetScale(1.1)
        FocusFrame:SetScale(1.1)
        TargetFrame:SetScale(1.1)
        PlayerFrame:ClearAllPoints()
        PlayerFrame:SetPoint("CENTER", -250, -150)
        TargetFrame:ClearAllPoints()
        TargetFrame:SetPoint("CENTER", 250, -150)
        FocusFrame:ClearAllPoints()
        FocusFrame:SetPoint("CENTER", -500, -150)
end)


Mischback 04-18-11 02:54 PM

OnUpdate is the very last spot you should do stuff like this, because it is run way too often.

As mentioned: Use PLAYER_ENTERING_WORLD and perhaps disable the SetPoint-functions for the frames in question by setting

lua Code:
  1. frame.SetPoint = function() return end


All times are GMT -6. The time now is 12:12 PM.

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