• Need help with Panel:isHovered
    8 replies, posted
I'm working on a little client side script and I want to make it when i hover my mouse over a DButton it will change color and when i'm not it will change back. Any help will be appreciated thank you in advance. [CODE]Mainbutton = vgui.Create("DButton") Mainbutton:SetPos(0, 60) Mainbutton:SetSize(50, 30) Mainbutton:SetText("") Mainbutton.paint = function() if MainMods:IsHovered == true then draw.RoundedBox(0,0,0, Mainbutton:GetWide(), Mainbutton:GetTall(), Color(255,0,0,255)) end else draw.RoundedBox(0,0,0, Mainbutton:GetWide(), Mainbutton:GetTall(), Color(0,0,0,200)) end end[/CODE]
It looks like you're checking if another element is being hovered over. Is that intentional?
[QUOTE=zippy36jr;47690645] [CODE] if MainMods:IsHovered == true then [/CODE][/QUOTE] IsHovered is an function, also you need to call it like an function. the correct way would be: [lua]if MainMods:IsHovered() == true then[/lua] but you don't even need the true part, since you check an boolean [lua]if MainMods:IsHovered() then[/lua] works also. The same is for checks if its not. then you can do following [lua]if not MainMods:IsHovered() then[/lua] or [lua]if !MainMods:IsHovered() then[/lua]
Also, you are ending an if statement, and then starting an else statement. Remove the "end" of the if statement if you intend to also use an else statement. [lua] if this then else end [/lua]
here is the whole script it still wont work [CODE]NotifyPanel = vgui.Create( "DNotify" ) NotifyPanel:SetPos(ScrW()/2-100, 40 ) NotifyPanel:SetSize( 200, 40 ) local lbl = vgui.Create( "DLabel", NotifyPanel ) lbl:Dock( FILL ) lbl:SetText( "ZMenu V1 has loaded!\nHit F9 to open the menu." ) lbl:SetTextColor(Color(math.random(1,255),math.random(1,255),math.random(1,255),255)) lbl:SetFont( "GModNotify" ) lbl:SetDark( true ) NotifyPanel:AddItem( lbl ) concommand.Add("open_zmenu", function() local MainFrame = vgui.Create("DFrame") MainFrame:SetPos(ScrW() - 400, 0) MainFrame:SetSize(300, ScrH()) MainFrame:SetTitle("") MainFrame:ShowCloseButton(true) MainFrame:SetDraggable(false) MainFrame:MakePopup() MainFrame.Paint = function() draw.RoundedBox(0,0,0, MainFrame:GetWide(), MainFrame:GetTall(), Color(0,0,0,200)) draw.SimpleText( "ZMenu V1", "DermaLarge", MainFrame:GetWide()/2, 10, Color(200,0,0,255), TEXT_ALIGN_CENTER) end local MainMods = vgui.Create("DButton", MainFrame) MainMods:SetPos(0, 60) MainMods:SetSize(MainFrame:GetWide(), 30) MainMods:SetText("") MainMods.paint = function() draw.RoundedBox(0,0,0, MainMods:GetWide(), MainMods:GetTall(), HoverColor) end if MainMods:IsHovered() then HoverColor = Color(0,255,0,255) else HoverColor = Color(0,0,0,0) end end)[/CODE]
I think you have to capitalize the first letter of the second paint, it's lowercase.
wow little things like that make me mad when i dont see them. Thank you for the help
[QUOTE=zippy36jr;47690759]here is the whole script it still wont work [CODE] local MainMods = vgui.Create("DButton", MainFrame) MainMods:SetPos(0, 60) MainMods:SetSize(MainFrame:GetWide(), 30) MainMods:SetText("") MainMods.paint = function() -- called every frame draw.RoundedBox(0,0,0, MainMods:GetWide(), MainMods:GetTall(), HoverColor) end --// Called ONCE when you open the menu, not anytime after it. To check that you need to put it back into the paint hook. if MainMods:IsHovered() then HoverColor = Color(0,255,0,255) else HoverColor = Color(0,0,0,0) end end)[/CODE][/QUOTE] comments above, the correct way would be: [lua]local HoverColor = Color(0,255,0) MainMods.Paint = function() if MainMods:IsHovered() then HoverColor = Color(0,255,0,255) else HoverColor = Color(0,0,0,0) -- btw the last arg is the alpha. with 0 you make it invis, with 255 you make it black. end draw.RoundedBox(0,0,0, MainMods:GetWide(), MainMods:GetTall(), HoverColor) end [/lua]
[QUOTE=Tomelyr;47690799]comments above, the correct way would be: [lua]local HoverColor = Color(0,255,0) MainMods.Paint = function() if MainMods:IsHovered() then HoverColor = Color(0,255,0,255) else HoverColor = Color(0,0,0,0) -- btw the last arg is the alpha. with 0 you make it invis, with 255 you make it black. end draw.RoundedBox(0,0,0, MainMods:GetWide(), MainMods:GetTall(), HoverColor) end [/lua][/QUOTE] Indeed, except it would be MainMods.Paint = function(), not MainMods.paint = function(). Also, you can use self in that function, to above using the variable name for it. It wouldn't break anything in a paint function where you are only referring to the vgui being drawn, but would matter in other circumstances.
Sorry, you need to Log In to post a reply to this thread.