View Single Post
10-05-14, 06:17 AM   #25
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
I've come up with a new design which solves the one major drawback my previous method had, which was the 1 frame delay between updates so our nameplate would lag behind the real one.
Lua Code:
  1. local Nameplates, PreviousWorldChildren = {}, 0
  2. WorldFrame:HookScript('OnUpdate', function(self)
  3.     local currentWorldChildren = self:GetNumChildren()
  4.     if currentWorldChildren ~= PreviousWorldChildren then PreviousWorldChildren = currentWorldChildren
  5.         for _, plate in pairs({self:GetChildren()}) do
  6.             if not Nameplates[plate] then
  7.                 local name = plate:GetName()
  8.                 if name and strmatch(name, '^NamePlate%d+$') then
  9.                     local f = CreateFrame('frame', nil, WorldFrame)
  10.                     Nameplates[plate] = f
  11.                     f:SetSize(1, 1)
  12.                    
  13.                     for i = 1, 500 do -- Make a bunch of regions for a stress test
  14.                         local fs = f:CreateFontString(nil, nil, 'GameFontNormalHuge')
  15.                         fs:SetText('LAG')
  16.                         fs:SetPoint('CENTER', f, math.random(-50,50), math.random(-12,12))
  17.                     end
  18.                    
  19.                     -- Attach a frame to the nameplate which will fire OnSizeChanged when it moves
  20.                     local sizer = CreateFrame('frame', nil, f)
  21.                     sizer:SetPoint('BOTTOMLEFT', WorldFrame)
  22.                     sizer:SetPoint('TOPRIGHT', plate, 'CENTER')
  23.                     sizer:SetScript('OnSizeChanged', function(self, x, y)
  24.                         f:Hide() -- Important, never move the frame while it's visible
  25.                         f:SetPoint('CENTER', WorldFrame, 'BOTTOMLEFT', x, y) -- Immediately reposition frame
  26.                         f:Show()
  27.                     end)
  28.                    
  29.                     plate:HookScript('OnHide', function() f:Hide() end)
  30.                     plate:HookScript('OnShow', function() f:Show() end)
  31.                 end
  32.             end
  33.         end
  34.     end
  35. end)

This works by creating an extra frame for every nameplate which is attached to the edge of the screen and the center of the nameplate, so that any time the nameplate moves it calls the OnSizeChanged script which is where we reposition our custom nameplate.

Other than that it's the same as it was before, but now it only updates if the frame has actually moved, and it should stay above the unit's head as the camera moves, rather than lag behind.
  Reply With Quote