WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   2 Script Questions (https://www.wowinterface.com/forums/showthread.php?t=45582)

laukond 01-06-13 07:39 AM

2 Script Questions
 
Hello, I would appreciate if you could help me with 2 questions.
http://i.imgur.com/7Ehq2.jpg


1.
As you can see on the screenshot the rested expbar has a bigger height than the not-rested expbar.

I use this code to decrease the height of MainMenuExpBar and I would like it to reduce the width of the rested bar too, but I cannot locate its name:

Lua Code:
  1. MainMenuExpBar:SetHeight(5)

I have looked here, but there doesn't seem to be anything about the height:
http://wowprogramming.com/utils/xmlb...ainMenuBar.lua


2.


I searched google for NUM_ADDONS_TO_DISPLAY and stumpled upon the code below.
It attaches AddOn memory usage to the Tracking Button.
Is there a way to make it not generate as much memory usage?

Currently when I am hovering the Tracking Button it keeps updating the usage.
My thought would be to make it only update once, which would reduce the amount of info it passes therefore less usage by itself?

Lua Code:
  1. local Stats = CreateFrame('Button')
  2. Stats:SetScript('OnEvent', function(self) self:Load() end)
  3. Stats:RegisterEvent('PLAYER_LOGIN')
  4.  
  5.  
  6. --[[ Startup ]]--
  7.  
  8. function Stats:Load()
  9.     local f = _G['MiniMapTrackingButton']
  10.     f:SetScript('OnEnter', self.OnEnter)
  11.     f:SetScript('OnLeave', self.OnLeave)
  12.     setmetatable(f, {__index = self})
  13. end
  14.  
  15.  
  16. --[[ Stats Frame Widget ]]--
  17.  
  18. local NUM_ADDONS_TO_DISPLAY = GetNumAddOns()
  19. local UPDATE_DELAY = 1
  20. local topAddOns
  21.  
  22. function Stats:OnEnter()
  23.     GameTooltip:SetOwner(self, 'ANCHOR_BOTTOMLEFT', 0, self:GetHeight())
  24.     self:UpdateTooltip()
  25. end
  26.  
  27. function Stats:OnLeave()
  28.     GameTooltip:Hide()
  29. end
  30.  
  31. function Stats:UpdateTooltip()
  32.     --clear topAddOns list
  33.     if topAddOns then
  34.         for i,addon in pairs(topAddOns) do
  35.             addon.value = 0
  36.         end
  37.     else
  38.         topAddOns = {}
  39.         for i=1, NUM_ADDONS_TO_DISPLAY do
  40.             topAddOns[i] = {name = '', value = 0}
  41.         end
  42.     end
  43.  
  44.     --add FPS and latency
  45.     local down, up, latency = GetNetStats()
  46.     local fps = format('FPS: %.1f', GetFramerate())
  47.     local net = format('Ping: %d ms', latency)
  48.  
  49.     GameTooltip:SetOwner(self, 'ANCHOR_BOTTOMLEFT', 0, self:GetHeight())
  50.     GameTooltip:ClearLines()
  51.     GameTooltip:AddDoubleLine(fps, net, 1, 1, 1, 1, 1, 1)
  52.     GameTooltip:AddLine('--------------------------------------------------')
  53.  
  54.     self:UpdateAddonsList(GetCVar('scriptProfile') == '1' and not IsModifierKeyDown())
  55. end
  56.  
  57. function Stats:UpdateAddonsList(watchingCPU)
  58.     if watchingCPU then
  59.         UpdateAddOnCPUUsage()
  60.     else
  61.         UpdateAddOnMemoryUsage()
  62.     end
  63.  
  64.     local total = 0
  65.     for i=1, GetNumAddOns() do
  66.         local value = (watchingCPU and GetAddOnCPUUsage(i)/1000) or GetAddOnMemoryUsage(i)
  67.         local name = (GetAddOnInfo(i))
  68.         total = total + value
  69.  
  70.         for j,addon in ipairs(topAddOns) do
  71.             if value > addon.value then
  72.                 for k = NUM_ADDONS_TO_DISPLAY, 1, -1 do
  73.                     if k == j then
  74.                         topAddOns[k].value = value
  75.                         topAddOns[k].name = GetAddOnInfo(i)
  76.                         break
  77.                     elseif k ~= 1 then
  78.                         topAddOns[k].value = topAddOns[k-1].value
  79.                         topAddOns[k].name = topAddOns[k-1].name
  80.                     end
  81.                 end
  82.                 break
  83.             end
  84.         end
  85.     end
  86.  
  87.     if total > 0 then
  88.         if watchingCPU then
  89.             GameTooltip:AddLine('Addon CPU Usage')
  90.         else
  91.             GameTooltip:AddLine('Addon Memory Usage')
  92.         end
  93.         GameTooltip:AddLine('--------------------------------------------------')
  94.  
  95.         for _,addon in ipairs(topAddOns) do
  96.             if watchingCPU then
  97.                 self:AddCPULine(addon.name, addon.value)
  98.             else
  99.                 self:AddMemoryLine(addon.name, addon.value)
  100.             end
  101.         end
  102.  
  103.         GameTooltip:AddLine('--------------------------------------------------')
  104.         if watchingCPU then
  105.             self:AddCPULine('Total', total)
  106.         else
  107.             self:AddMemoryLine('Total', total)
  108.         end
  109.     end
  110.     GameTooltip:Show()
  111. end
  112.  
  113. function Stats:AddCPULine(name, secs)
  114.     if secs > 3600 then
  115.         GameTooltip:AddDoubleLine(name, format('%.2f h', secs/3600), 1, 1, 1, 1, 0.2, 0.2)
  116.     elseif secs > 60 then
  117.         GameTooltip:AddDoubleLine(name, format('%.2f m', secs/60), 1, 1, 1, 1, 1, 0.2)
  118.     elseif secs >= 1 then
  119.         GameTooltip:AddDoubleLine(name, format('%.1f s', secs), 1, 1, 1, 0.2, 1, 0.2)
  120.     elseif secs > 0 then
  121.         GameTooltip:AddDoubleLine(name, format('%.1f ms', secs * 1000), 1, 1, 1, 0.2, 1, 0.2)
  122.     end
  123. end
  124.  
  125. function Stats:AddMemoryLine(name, size)
  126.     if size > 1000 then
  127.         GameTooltip:AddDoubleLine(name, format('%.2f mb', size/1000), 1, 1, 1, 1, 1, 0.2)
  128.     elseif size > 0 then
  129.         GameTooltip:AddDoubleLine(name, format('%.2f kb', size), 1, 1, 1, 0.2, 1, 0.2)
  130.     end
  131. end

Taken from https://github.com/Tuller/tullaTweak...ster/stats.lua

Any help is appreciated. Have a good day. ;)

Phanx 01-06-13 10:43 PM

1. In the Lua file you linked, searching for "GetXPExhaustion" -- the API function that tells you how much rested XP you have -- quickly reveals the Lua function the default UI calls to update the bar, "ExhaustionTick_OnEvent". Looking at the contents of this function, it appears that the rest bar is named named "ExhaustionLevelFillBar". Looking in the corresponding XML file reveals that "ExhaustionLevelFillBar", is actually a texture, not a statusbar, so it will have SetWidth and SetHeight methods, but not SetValue or other frame/statusbar methods.

2. I'm not sure what you mean by "make it not generate as much memory usage". The tooltip just shows you how much memory your addons are using. It doesn't "generate" any memory. If you're seeing your tooltip addon occupy increasing amounts of memory, it's probably because you're generating a new table on every update, which is very expensive and wasteful. You should only create the "topAddOns" table once, outside of the "UpdateTooltip" method. Since the number of table entries (NUM_ADDONS_TO_DISPLAY) never changes you don't even need to clear the table between uses; you can just overwrite the values like you're already doing.

laukond 01-07-13 08:50 AM

Thanks for your reply Phanx :)

What I mean by "make it not generate as much memory usage" is:

The above code in 2. uses ~40kb of memory, but when I am hovering the TrackButton its memory usage raises with ~1kb/s. I believe it's doing this because the tooltip keeps updating.

Is there a way to rewrite the code so it does not update the FPS, ping, and memory all the time when hovering the TrackButton?

SDPhantom 01-07-13 04:14 PM

There are a couple issues I'm seeing. First of all, it's not a good idea to replace the metatable of a frame unless you absolutely need to. Also, you're creating an extra frame that you're not using for any specific purpose that needs a frame. Taking these issues in hand and some further optimizations, here is the code that I'd propose.

Lua Code:
  1. --  Settings
  2. local MaxDisplay=5;
  3. local UpdateSpeed=1;
  4.  
  5. --  Init table
  6. local AddonData={};
  7. for i=1,GetNumAddOns() do
  8.     AddonData[i]={ID=i,Name=GetAddOnInfo(i),Value=0};
  9. end
  10.  
  11. --  Format time func
  12. local function AddCPULine(tooltip,name,secs)
  13.     if secs > 3600 then
  14.         tooltip:AddDoubleLine(name,format('%.2f h',secs/3600),1,1,1,1,0.2,0.2);
  15.     elseif secs > 60 then
  16.         tooltip:AddDoubleLine(name,format('%.2f m',secs/60),1,1,1,1,1,0.2);
  17.     elseif secs >= 1 then
  18.         tooltip:AddDoubleLine(name,format('%.1f s',secs),1,1,1,0.2,1,0.2);
  19.     elseif secs > 0 then
  20.         tooltip:AddDoubleLine(name,format('%.1f ms',secs*1000),1,1,1,0.2,1,0.2);
  21.     end
  22. end
  23.  
  24. --  Format memory func
  25. local function AddMemoryLine(tooltip,name, size)
  26.     if size>1000 then
  27.         tooltip:AddDoubleLine(name,format('%.2f mb',size/1000),1,1,1,1,1,0.2);
  28.     elseif size>0 then
  29.         tooltip:AddDoubleLine(name,format('%.2f kb',size),1,1,1,0.2,1,0.2);
  30.     end
  31. end
  32.  
  33. --  GameTooltip calls this OnUpdate if it exists
  34. local LastUpdate=0;
  35. function MiniMapTrackingButton:UpdateTooltip()
  36. --  FPS and latency
  37.     local down,up,latency=GetNetStats();
  38.     local fps=format('FPS: %.1f',GetFramerate());
  39.     local net=format('Ping: %d ms',latency);
  40.  
  41.     GameTooltip:SetOwner(self,"ANCHOR_BOTTOMLEFT",0,self:GetHeight());
  42.     GameTooltip:ClearLines();
  43.     GameTooltip:AddDoubleLine(fps,net,1,1,1,1,1,1);
  44.     GameTooltip:AddLine("--------------------------------------------------");
  45.  
  46. --  Update throttle
  47.     local now=GetTime();
  48.     if now-LastUpdate>UpdateSpeed then
  49.         local watchingCPU=(GetCVar("scriptProfile")=="1" and not IsModifierKeyDown());
  50.  
  51. --      Request game client to update internal cache
  52.         if watchingCPU then
  53.             UpdateAddOnCPUUsage();
  54.         else
  55.             UpdateAddOnMemoryUsage();
  56.         end
  57.  
  58. --      Update data
  59.         local total=0;
  60.         for i,j in ipairs(AddonData) do
  61.             local val=(watchingCPU and GetAddOnCPUUsage(j.ID)/1000 or GetAddOnMemoryUsage(j.ID));
  62.             j.Value=val;
  63.             total=total+val;
  64.         end
  65.  
  66. --      Sort data
  67.         for i=1,#AddonData-1 do
  68.             local max,id=0;
  69.             for j=i+1,#AddonData do
  70.                 local val=AddonData[j].Value;
  71.                 if val>max then max,id=val,j; end
  72.             end
  73.  
  74.             if AddonData[i].Value<max then
  75.                 AddonData[i],AddonData[id]=AddonData[id],AddonData[i];
  76.             end
  77.         end
  78.  
  79.         if watchingCPU then
  80.             GameTooltip:AddLine("Addon CPU Usage");
  81.             GameTooltip:AddLine("--------------------------------------------------");
  82.  
  83.             for i,j in ipairs(AddonData) do
  84.                 if i>MaxDisplay then break; end
  85.                 AddCPULine(GameTooltip,j.Name,j.Value);
  86.             end
  87.             GameTooltip:AddLine("--------------------------------------------------");
  88.             AddCPULine(GameTooltip,"Total",total);
  89.         else
  90.             GameTooltip:AddLine("Addon Memory Usage");
  91.             GameTooltip:AddLine("--------------------------------------------------");
  92.  
  93.             for i,j in ipairs(AddonData) do
  94.                 if i>MaxDisplay then break; end
  95.                 AddMemoryLine(GameTooltip,j.Name,j.Value);
  96.             end
  97.             GameTooltip:AddLine("--------------------------------------------------");
  98.             AddMemoryLine(GameTooltip,"Total",total);
  99.         end
  100.     end
  101.     GameTooltip:Show()
  102. end
  103.  
  104. MiniMapTrackingButton:SetScript("OnEnter",function(self)
  105.     GameTooltip:SetOwner(self,"ANCHOR_BOTTOMLEFT",0,self:GetHeight());
  106.     self:UpdateTooltip();
  107. end);
  108.  
  109. MiniMapTrackingButton:SetScript("OnLeave",function() GameTooltip:Hide(); end);

Phanx 01-07-13 04:58 PM

Quote:

Originally Posted by laukond (Post 271469)
Is there a way to rewrite the code so it does not update the FPS, ping, and memory all the time when hovering the TrackButton?

Yes, and I already told you how:

Quote:

Originally Posted by Phanx (Post 271446)
If you're seeing your tooltip addon occupy increasing amounts of memory, it's probably because you're generating a new table on every update, which is very expensive and wasteful. You should only create the "topAddOns" table once, outside of the "UpdateTooltip" method. Since the number of table entries (NUM_ADDONS_TO_DISPLAY) never changes you don't even need to clear the table between uses; you can just overwrite the values like you're already doing.

The code SDPhantom posted includes a number of other improvements/optimizations, so you should probably just use that, but you could solve the specific problem you asked about by making the one specific change I suggested (moving the "topAddOns" table creation outside of the "UpdateTooltip" method).

laukond 01-08-13 09:14 AM

Thank you both for your reply I really appreciate it. :)

I get this error: "attempt to compare number with string" in this line: (line 71)
Lua Code:
  1. if val>max then max,id=val,j; end

Phanx, I am sorry I don't understand how to do as you say :o
Could you please show me?
I tried moving the topAddOns table a couple of places outside of functions:
Once it would not show AddOn memory usage at all.
Another time the AddOn itself would start at 600kb usage and raise with 120kb/s.

I am really bad a programming, but I believe the AddOn is not creating a new table on every update; it is refilling it. What I want is just to make it not refill.

Offtopic: Do you know a good place for newbies to start learning Lua? A book, website, etc.

Again I really appreciate your help, I am sorry to be so bad at receiving it :confused:

SDPhantom 01-08-13 03:01 PM

Fixed the problem on line 62 of my posted code. I had j.Name being set there too, but removed it in favor of the addon name and ID being loaded into the table at initialization instead of on update. I had forgotten to remove GetAddOnInfo(j.ID) from the value list too.


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

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