• Terribly bad code, finds resource hole?
    1 replies, posted
Z Code: [LUA] BuddyList = BuddyList or {} function TheMenu( Panel ) Panel:ClearControls() Panel:Clear() Panel:AddControl("Label", {Text = "\nSuper duper basic buddy list:\nClick to obtain a friend!"}) local BList = vgui.Create("DListView") BList:SetParent(Panel) BList:SetPos(10, 100) BList:SetSize(300, 275) BList:SetText("Buddies:") BList:AddColumn("No"):SetFixedWidth(25) BList:AddColumn("Online Players") if (BuddyList) then for k,bud in pairs(player.GetAll()) do BList:AddLine(k,bud) end end function BList:OnClickLine(line) AddBuddy(line:GetValue(2)) TheMenu(Panel) end local BList2 = vgui.Create("DListView") BList2:SetParent(Panel) BList2:SetPos(10, 380) BList2:SetSize(300, 275) BList2:AddColumn("No"):SetFixedWidth(25) BList2:AddColumn("Double click to remove") if (BuddyList) then for k,bud in pairs(BuddyList) do if IsValid(bud) then BList2:AddLine(k,bud) end end end function BList2:OnClickLine(line) RemoveBuddy(line:GetValue(2)) TheMenu(Panel) end timer.Create("Panel refresh",5,0, function() if (FPPBuddyList) then for k,v in pairs(FPPBuddyList) do for _,ply in pairs(player.GetAll()) do if ply:SteamID() == v then AddBuddy(ply) end end end end TheMenu(Panel) end) end function createthemenu() spawnmenu.AddToolMenuOption( "Options", "ByB", "CustomMenu", "Prop Buddies", "", "", TheMenu, {}) end hook.Add( "PopulateToolMenu", "pleasework", createthemenu ) [/LUA] So this is an option inside the Q menu, but I'm curious about something. I know it's just plainly stupid code, it was made while I was learning some stuff a year or so ago. The code in itself is creating a timer, that makes itself run every 5 seconds and creates the timer again. Pure idiocy I agree, however. Why does this cause an escalation in memory and CPU usage whenever the client opens the Q menu? The timer should be recreating itself, not causing major memory leaks and escalating CPU usage. Also: While this threads up, I'm obviously remaking the above part of the Q menu edits. But how do I repopulate the list for stored new players without having to redraw the entire panel each time? When I add entries to the list (BLIST) for example it simply doesn't show unless I redraw the entire thing. [editline]8th October 2013[/editline] The temporary capture breaks post editing: It's not a direct CPU spike I always see, however performance when the Q menu open drops to 4fps etc.
@Edit: Just noticed you aren't talking about the thing I thought. Sorry.
Sorry, you need to Log In to post a reply to this thread.