Overwriting previously defined method for custom vgui panel
4 replies, posted
I am developing an extension to a gamemode and wanted to make use of the the already defined custom vgui panels from the gamemode and work on top of them.
So I made a table with the new methods I wanted and vgui.register() them citing the old custom method as base.
My question is: While adding new methods to this derived custom panel is pretty straight forward, how do I overwrite methods that were inherited from the base custom panel if they are defined with a local table in another file?
registering methods with the same name in the new custom panel has the effect of both methods the new and old functions being called.
If there isn't a way to do this, how would I go about achieving what I wanted?
Give examples.
This is the intended way, except for the Init method, which will always be called on parent panel classes.
ty for the response!
what I meant was:
If I have something like
local OLDPANEL = {}
function OLDPANEL:Init()
self:SetZPos(100)
self:SetTitle("")
self:SetSize(100,100)
self.b1 = vgui.Create("DButton",self)
self.b1:SetText("B1")
self.b1:SetSize(24,24)
self.b1:SetPos(0,0)
end
function OLDPANEL:UsefulStuff()
end
function OLDPANEL:OtherUsefulStuff()
end
vgui.Register("gamemode_old",OLDPANEL, "DFrame")
and I do
local NEWPANEL = {}
function NEWPANEL:Init()
self:SetZPos(100)
self:SetTitle("")
self:SetSize(100,100)
self.b1 = vgui.Create("DButton",self)
self.b1:SetText("B1")
self.b1:SetSize(24,24)
self.b1:SetPos(24,24)
end
vgui.Register("gamemode_new",NEWPANEL, "gamemode_old")
then create gamemode_new, I end up with 2 buttons instead of just the new one
Is there a wait I could suppress OLDPANEL:Init() from being called when creating gamemode_new?
If not, is there another way I can inherit methods from gamemode_old?
I just don't want to have to copy useful code from the old panel to the new one redundantly
No, as I said, this is intended behavior for PANEL:Init.
You can change the button position in the init of the child to your liking.
( Since the variable names are the same in your case, just dont create the button in the child element )
You could also try moving button creation into a separate function and call that from the Init(), then override that function in your child panel ( But don't call that function in the child panel directly ) Not sure if this'll work, but it might.
Oh, it hadn't occurred to me to just set b1's parameters like that. I guess that makes a lot of sense hahhah.
Ty for the help man, and expect to see me me in the next "what are you working on?"
Sorry, you need to Log In to post a reply to this thread.