Using a panel as a "base" panel

Hello,

I am making my own inventory system just for some coding challenges, and something occurred to me that might be able to simplify or possibly optimize how I’m doing my item system. Its still in the very very early stages, but this is an example of one of my item panels:

PANEL = {}

function PANEL:ItemConfig()
	self.ItemTitle = "Spartan Armor"
	self.ItemDesc = "Become a Spartan!"
	self.Icon = Material( "materials/squad_icons/Team Gray.png" )
	self.Rarity = "Common"
	self.Type = "armor"
end

function PANEL:Init()
	self:Droppable( "Item" )
	self:Dock( TOP )
	self:ItemConfig()
end

function PANEL:SetItemSize(w, h)
	self:SetSize(w, h)
end

function PANEL:SetItemRarity(rarity)
	self.Rarity = rarity
end

function PANEL:GetItemRarity()
	return self.Rarity
end

function PANEL:GetItemType()
	return self.Type
end

function PANEL:GetTitle()
	return self.ItemTitle
end

function PANEL:GetDesc()
	return self.ItemDesc
end

function PANEL:Paint( w, h )
	surface.SetDrawColor( color_white )
	surface.SetMaterial( self.Icon )
	surface.DrawTexturedRect( 0, 0, w, h )
end

vgui.Register("armor_spartan", PANEL, "DPanel")

As you can see, I have barely started adding features to this item, and there already quite a few helper functions for accessing the information and what not from other places. Would it be possible to make a “base” panel that has all the helper functions in it, then I can make a panel that has this panel as a parent, and has access to all the helper functions without actually having them all in the code? This is a single item, and every item will need all of these functions, some more than others. Is what I’m thinking possible? Is it worth it? Or should I just copy paste all the functions in every item?

Thanks,

Dan

You set the base when you register the panel, it is the last argument, so you would replace “DPanel” with your panel class e.g. “my_base_panel”.

1 Like

Oh, duh. That seems really obvious now, but I did not think of that.

Thank you so much!

No problem :slight_smile:

If you ever need help a function etc just search it up on the wiki and it will tell you most things about it.

1 Like