Label does not run PANEL:DoClick() after being used in vgui.register()

Well, I’m using this code to register a kind of custom button:



local PANEL = {}
function PANEL:Init()
    self = vgui.Create("DLabel", self)
    self.text = "Dummy"
    self:SetText("")
    self:SetSize(100, 25)
    self:SetMouseInputEnabled(true)
    self.hover = false
end
function PANEL:OnCursorEntered()
    self:SetCursor("hand")
    self.hover = true
end
function PANEL:OnCursorExited()
    self.hover = false
end
function PANEL:SetText(t)
    self.text = t
end
function PANEL:SetWide(w)
    self:SetSize(w+16, 40)
end
function PANEL:Paint(w, h)
    if self.hover then
        draw.RoundedBox( 4, 0, 0, w, h, Color( 110, 216, 56 ) )
        draw.SimpleText( self.text, "somefontKappa", w/2+1, h/2+1, Color( 255, 255, 255, 200 ), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
        draw.SimpleText( self.text, "somefontKappa", w/2, h/2, Color( 30, 30, 30, 255 ), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
    else
        draw.RoundedBox( 4, 0, 0, w, h, Color( 135, 243, 81 ) )
        draw.SimpleText( self.text, "somefontKappa", w/2+1, h/2+1, Color( 255, 255, 255, 100 ), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
        draw.SimpleText( self.text, "somefontKappa", w/2, h/2, Color( 30, 30, 30, 255 ), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
    end
end
vgui.Register("m_RButton", PANEL)

and this one to deploy it on UI:


		local button = vgui.Create( "m_RButton", parent )
		button:SetPos(250, 290)
		button:SetText("Do stuff!")
		button.DoClick = function()
			print("ive been pressed!")
		end

For some reason when I click on that button it does not call DoClick function. Since I’m not overwriting it when defining custom UI element and allowing mouse input this should be running fine but it does not.

This is stupid and simple question but I’m out of mind with that.

self = vgui.Create(“DLabel”, self)

I don’t know what the hell were you thinking, but this isn’t how it works.

Use the 3rd argument of vgui.Register.

I suppose it’s just other way of setting base element. It ports all element’s functions to it before registering it so I can use it to configure it more widely. It allows to use base’s functions during initialization i.e. self:SetText(""), which does not work when you are using “3rd argument” way to define base element. It works with other elements which I define just like this one.

However, I’ve tried your suggestion and it did not work.


local PANEL = {}
function PANEL:Init()
    self.text = "Dummy"
    self:SetText("")
    self:SetSize(100, 25)
    self.hover = false
end
function PANEL:OnCursorEntered()
    self:SetCursor("hand")
    self.hover = true
end
function PANEL:OnCursorExited()
    self.hover = false
end
function PANEL:SetText(t)
    self.text = t
end
function PANEL:SetWide(w)
    self:SetSize(w+16, 40)
end
function PANEL:Paint(w, h)
    if self.hover then
        draw.RoundedBox( 4, 0, 0, w, h, Color( 110, 216, 56 ) )
        draw.SimpleText( self.text, "somefont", w/2+1, h/2+1, Color( 255, 255, 255, 200 ), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
        draw.SimpleText( self.text, "somefont", w/2, h/2, Color( 30, 30, 30, 255 ), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
    else
        draw.RoundedBox( 4, 0, 0, w, h, Color( 135, 243, 81 ) )
        draw.SimpleText( self.text, "somefont", w/2+1, h/2+1, Color( 255, 255, 255, 100 ), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
        draw.SimpleText( self.text, "somefont", w/2, h/2, Color( 30, 30, 30, 255 ), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
    end
end
vgui.Register("m_RButton", PANEL, "DLabel")

This did not work. Also self:SetText("") and self:SetSize(…) does not work any more and default “Label” sign appears on my button over my custom text. Also it does not react to hovering mouse and stuff.

You are a goddamn idiot.

All you achieve by doing this:


self = vgui.Create("DLabel", self)

Is replace the variable self for current scope ( read: Only for PANEL:Init ) with another variable meaning you will not be able to modify the actual m_RButton and instead will be modifying a DLabel.

It doesn’t magically “set base element” or “port all element’s functions to it”.

In all other functions, including after you have created a m_RButton with vgui.Create and trying to modify it, you will be modifying the m_RButton and not DLabel, DLabel with all its methods are already lost to your panel because there are no references to it from your panel.

So, to solve your problem, either use what I told you to use and don’t remove the self:SetMouseInputEnabled(true) to try to prove me wrong, or use self.Label and then use Mym_RButtonVariable.Label.DoClick, and do not forget to self:SetMouseInputEnabled(true) on the Dlabel.

Proof of concept:

http://pastebin.com/LLFJrnrm

[editline]14th October 2016[/editline]

That’s because that’s the actual DLabel’s text. You cannot set it anymore because you already override SetText.

[editline]14th October 2016[/editline]

Something like this will work:

http://pastebin.com/WQXe8EXs

[editline]14th October 2016[/editline]

And by the way, you can just use self.Hovered for testing if a panel is hovered instead of doing it yourself.

Don’t get mad bro. I saw that way on FP and this worked with stuff such as


local PANEL = {}
function PANEL:Init()
    self = vgui.Create("DLabel", self)
    self.text = "Dummy"
    self:SetText("")
end
function PANEL:SetText(t)
    self.text = t
end
function PANEL:SetWide(w)
	self:SetSize(w, 25)
end
function PANEL:Paint(w, h)
	draw.SimpleText( self.text, "H2", 1, 1 )
	draw.SimpleText( self.text, "H2", 0, 0 )
end
vgui.Register("m_H2", PANEL)


And I wondered if I could get it to work with other stuff.

and i did not mean to troll you with removing self:SetMouseInputEnabled(true) that was accidential and i did not even observe that until u told me :stuck_out_tongue:

works now, solved :3

Please don’t spread misinformation.

That DLabel in your code literally does absolutely nothing.

Same as:



local PANEL = {}
function PANEL:Init()
    self.text = "Dummy"
end
function PANEL:SetText(t)
    self.text = t
end
function PANEL:SetWide(w)
	self:SetSize(w, 25)
end
function PANEL:Paint(w, h)
	draw.SimpleText( self.text, "H2", 1, 1 )
	draw.SimpleText( self.text, "H2", 0, 0 )
end
vgui.Register("m_H2", PANEL)

Huh, thanks. Did not know that, just saw code snippet and started using it ¯_(ツ)_/¯