[Q]Error with ContentPanel integration.

Hey, I’m getting an error from the following code and I’m not sure what’s wrong, any help would be GREATLY appreciated! I’ve posted the error below.


if engine.ActiveGamemode( ) == "sandbox" then return end

local PANEL = PANEL or {}

PANEL.Base = "Panel"

local ContentPanel = nil

function PANEL:Init()

	self:Dock( TOP )
	self:SetHeight( 20 )
	self:DockMargin( 0, 0, 0, 3 )

	self.Search = self:Add( "DTextEntry" )
	self.Search:Dock( FILL )
	--self.Search:SetWidth( 150 )

	self.Search.OnEnter = function() self:RefreshResults() end
	self.Search.OnFocusChanged = function( _, b ) if ( b ) then self:RefreshResults() end end
	self.Search:SetTooltip( "Press enter to search" )
	
	local btn = self.Search:Add( "DImageButton" )

	btn:SetImage( "icon16/magnifier.png" )
	btn:SetText( "" )
	btn:Dock( RIGHT )
	btn:DockMargin( 4, 2, 4, 2 )
	btn:SetSize( 16, 16 )
	btn:SetTooltip( "Press to search" )
	btn.DoClick = function()
		self:RefreshResults()
	end

	self.Search.OnKeyCode = function( p, code )

		if ( code == KEY_F1 ) then hook.Run( "OnSpawnMenuClose" ) end
		if ( code == KEY_ESCAPE ) then hook.Run( "OnSpawnMenuClose" ) end

	end

	hook.Add( "StartSearch", "StartSearch", function()

		if ( Pointshop2.SpawnMenu:IsVisible() ) then return hook.Run( "OnSpawnMenuClose" ) end

		hook.Run( "OnSpawnMenuOpen" )
		hook.Run( "OnTextEntryGetFocus", self.Search )		

		self.Search:RequestFocus()
		self.Search:SetText( "" );

		--
		-- If we don't call this we'd have to press F1 twice to close it!
		-- It's in a timer because of some good reason that!
		--
		timer.Simple( 0.1, function() Pointshop2.SpawnMenu:HangOpen( false ) end )

		ContentPanel:SwitchPanel( self.PropPanel );

	end);

	hook.Add( "SearchUpdate", "SearchUpdate", function()

		if ( !Pointshop2.SpawnMenu:IsVisible() ) then return end
		self:RefreshResults()

	end);

	self.PropPanel = vgui.Create( "PS2_ContentContainer", self )
	self.PropPanel:SetVisible( false )
	self.PropPanel:SetTriggerSpawnlistChange( false )
	
	Pointshop2.SpawnMenu.SearchPropPanel = self.PropPanel
	
end

function PANEL:RefreshResults() 

	if ( self.Search:GetText() == "" ) then return end

	self.PropPanel:Clear()

	local results = search.GetResults( self.Search:GetText() )

	local Header = self:Add( "ContentHeader" )
		Header:SetText( #results .. " Results for \""..self.Search:GetText().."\"" )
		self.PropPanel:Add( Header )

	for k, v in pairs( results ) do
		self:AddSearchResult( v.text, v.func, v.icon )
	end

	self.PropPanel:SetParent( ContentPanel )
	ContentPanel:SwitchPanel( self.PropPanel );

end

function PANEL:AddSearchResult( text, func, icon )

	if ( !IsValid( icon ) ) then return end

	icon:SetParent( self.PropPanel )
	self.PropPanel:Add( icon )

end


hook.Add( "PS2_SpawnMenu_PopulateContent", "AddSearchContent", function( pnlContent, tree, node )

	-- Add a node to the tree
	local node = tree:AddNode( "Search", "icon16/magnifier.png" );
	
	-- If we click on the node populate it and switch to it.
	node.DoClick = function( self )
	
		self:DoPopulate()		
		pnlContent:SwitchPanel( self.PropPanel );
	
	end

	ContentPanel = pnlContent

end )


The error is as follows:




[ERROR] addons/pointshop2/lua/ps2/modules/pointshop2/hat/spawnmenu/creationmenu/content/cl_contentsearch.lua:94: attempt to index upvalue 'ContentPanel' (a nil value)
  1. RefreshResults - addons/pointshop2/lua/ps2/modules/pointshop2/hat/spawnmenu/creationmenu/content/cl_contentsearch.lua:94
   2. OnEnter - addons/pointshop2/lua/ps2/modules/pointshop2/hat/spawnmenu/creationmenu/content/cl_contentsearch.lua:19
    3. unknown - lua/vgui/dtextentry.lua:104



Thanks once again for any help or insight into this issue. Cheers!

You are trying to use ContentPanel before you assign a value to it.
I also don’t see where you create/register your PANEL.



PANEL.Base = "Panel"

This is not how you normally define Base panel. You might just as well remove that because the default base panel is “Panel” afaik.

And you shouldn’t put hook.Add stuff into PANEL:Init or any other function. ( Not a restriction, just general advice, if you know what you are doing then it’s ok )