Problem with Align text.

I don’t know why I have this error.

[lua]cLabel = {}; – We need a table to define and store our custom control in.
cLabel.Text = nil; – The text we are displaying
cLabel.TextAlign = nil; – How to align the text; 0 means left, 1 means center.
cLabel.Font = nil;
cLabel.Color = nil;

– Initialize function; this is called when the object is created
function cLabel:Init()
– Inside these functions, we use self.(property), because we are referencing
self.Text =""; –
self.TextAlign = 0; – unless otherwise specified, we will want to align left.
self.Font = “ScoreboardText”; – This is our default font
self.Color = Color( 255,255,255,255 );
end

function cLabel:Paint()
– This is where we do all the painting.
if ( self.TextAlign == 1 ) then
x = self:GetWide() / 2;
else
x = 0;
end

– Arguments: Text, Font, X Pos, Y Pos, Color, Text Alignment
draw.DrawText( self.Text, self.Font, x, 0, self.Color, self.TextAlign );
return true;
end

cFrame = vgui.Create( “frame” );
cFrame:SetSize( 200,100 );
cFrame:SetPos( 0,0 );
cFrame:PostMessage( “SetTitle”, “text”, “cLabel Test” );
cFrame:SetVisible( true );

cLabelTest = vgui.Create( “cLabel”, cFrame );
cLabelTest:SetSize( 50, 10 );
cLabelTest:SetPos( 4, 20 );
cLabelTest:SetText( “Test” );
cLabelTest:SetAlign( 1 );

– Properties
– Here, we are basically making it so that instead of accessing the variables directly,
– we are making it so that we can check to make sure that the value is valid for what
– we are setting. Also, if you need to use it with a timer, you can use these instead of
– creating other functions.

function cLabel:SetText( text )
self.Text = text; – Make sure that you’re using self!
end
function cLabel:GetText()
return self.Text;
end

function cLabel:SetAlign( align )
if ( align == 0 || align == 1 ) then
self.TextAlign = align;
else
Msg( “Invalid align [” … align … "] passed to control!
" );
end
end
function cLabel:GetAlign()
return self.TextAlign;
end

function cLabel:SetFont( font )
self.Font = font;
end
function cLabel:GetFont()
return self.Font;
end

function cLabel:SetColor( color )
if ( string.lower( type( color ) ) != “color” ) then
Msg( "Invalid color passed to label!
" );
return false;
else
self.Color = color;
end
end
function cLabel:GetColor()
return self.Color;
end

cButton = {};
cButton.OnMouseDown = nil; – More on these two later
cButton.OnMouseUp = nil;
cButton.State = nil;
– States:
– 0: Idle
– 1: User is holding down the mouse on this control

function cButton:Paint()
return true; – We don’t want the cLabel:Paint() function being called
end

function cButton:OnMousePressed( m )
end
function cButton:OnMouseReleased( m )
end

myButton.OnMouseDown = function() msg( “Mouse down!” ) end
myButton.OnMouseUp = function() msg(“Mouse up!” ) end

function cButton:OnMousePressed( m )
self.State = 1; – Mouse Down
if ( self.OnMouseDown != nil ) then
self.OnMouseDown( m );
end
end
function cButton:OnMouseReleased( m )
self.State = 0; – Mouse Up
if ( self.OnMouseUp != nil ) then
self.OnMouseUp( m );
end
end

function cButton:Paint()
// Draw Border

if ( self.State == 1 ) then
surface.SetDrawColor( 0,0,200,255 ); – Blue
surface.DrawRect( 4,4,self:GetWide()-8,self:GetTall()-8 );
else // The only other state should be 0, but if it’s something else for some weird reason, we still want it.
surface.SetDrawColor( 200,100,0,255 ); – Orange
surface.DrawRect( 0,0,self:GetWide(),self:GetTall() );
end

// Draw Inside
surface.SetDrawColor( 100,100,100,255 ); – Orange
surface.DrawRect( 4,4,self:GetWide()-8,self:GetTall()-8 );
// Draw Text
//Text, font, x, y, color, align
draw.DrawText( self.Text, self.Font, self:GetWide() / 2, 3, Color(255,255,255,255 ), 1);
return true

end

cFrame = vgui.Create( “frame” );
cFrame:SetSize( 200,100 );
cFrame:SetPos( 0,0 );
cFrame:PostMessage( “SetTitle”, “text”, “cLabel Test” );
cFrame:SetVisible( true );

cLabelTest = vgui.Create( “cLabel”, cFrame );
cLabelTest:SetSize( 100, 25 );
cLabelTest:SetPos( 50, 20 );
cLabelTest:SetText( “Test” );
cLabelTest:SetAlign( 1 );

cButtonTest = vgui.Create( “cButton”, cFrame );
cButtonTest:SetSize( 100, 25 );
cButtonTest:SetPos( 55, 35 );
cButtonTest:SetText( “Click Me!” );
cButtonTest.OnMouseDown = function() cLabelTest:SetText(“Mouse Down!”) end
cButtonTest.OnMouseUp = function() cLabelTest:SetText(“Mouse Up!”) end

vgui.Register( “cLabel”, cLabel, “Panel” );
[/lua]



autorun\client\cl_test.lua:59: attempt to concatenate global 'aling' (a nil value)


It means you misspelled “align” on line 59. It looks like you fixed here in this code, so that wouldn’t be a problem.

My guess is it would be this line here:


Msg( "Invalid align [" .. align .. "] passed to control!
" );  

Weird I don’t see misspelling

Now I get this error :


autorun\client\cl_test.lua:40: attempt to index global 'cLabelTest' (a nil value)


[lua]cLabel = {}; – We need a table to define and store our custom control in.
cLabel.Text = nil; – The text we are displaying
cLabel.TextAlign = nil; – How to align the text; 0 means left, 1 means center.
cLabel.Font = nil;
cLabel.Color = nil;

– Initialize function; this is called when the object is created
function cLabel:Init()
– Inside these functions, we use self.(property), because we are referencing
self.Text =""; –
self.TextAlign = 0; – unless otherwise specified, we will want to align left.
self.Font = “ScoreboardText”; – This is our default font
self.Color = Color( 255,255,255,255 );
end

function cLabel:Paint()
– This is where we do all the painting.
if ( self.TextAlign == 1 ) then
x = self:GetWide() / 2;
else
x = 0;
end

– Arguments: Text, Font, X Pos, Y Pos, Color, Text Alignment
draw.DrawText( self.Text, self.Font, x, 0, self.Color, self.TextAlign );
return true;
end

cFrame = vgui.Create( “frame” );
cFrame:SetSize( 200,100 );
cFrame:SetPos( 0,0 );
cFrame:PostMessage( “SetTitle”, “text”, “cLabel Test” );
cFrame:SetVisible( true );

cLabelTest = vgui.Create( “cLabel”, cFrame );
cLabelTest:SetSize( 50, 10 );
cLabelTest:SetPos( 4, 20 );
cLabelTest:SetText( “Test” );
cLabelTest:SetAlign( 1 );

– Properties
– Here, we are basically making it so that instead of accessing the variables directly,
– we are making it so that we can check to make sure that the value is valid for what
– we are setting. Also, if you need to use it with a timer, you can use these instead of
– creating other functions.

function cLabel:SetText( text )
self.Text = text; – Make sure that you’re using self!
end
function cLabel:GetText()
return self.Text;
end

function cLabel:SetAlign( align )
if ( align == 0 || align == 1 ) then
self.TextAlign = align;
else
Msg( “Invalid align [” … align … "] passed to control!
" );
end
end
function cLabel:GetAlign()
return self.TextAlign;
end

function cLabel:SetFont( font )
self.Font = font;
end
function cLabel:GetFont()
return self.Font;
end

function cLabel:SetColor( color )
if ( string.lower( type( color ) ) != “color” ) then
Msg( "Invalid color passed to label!
" );
return false;
else
self.Color = color;
end
end
function cLabel:GetColor()
return self.Color;
end

cButton = {};
cButton.OnMouseDown = nil; – More on these two later
cButton.OnMouseUp = nil;
cButton.State = nil;
– States:
– 0: Idle
– 1: User is holding down the mouse on this control

function cButton:Paint()
return true; – We don’t want the cLabel:Paint() function being called
end

function cButton:OnMousePressed( m )
end
function cButton:OnMouseReleased( m )
end

myButton.OnMouseDown = function() msg( “Mouse down!” ) end
myButton.OnMouseUp = function() msg(“Mouse up!” ) end

function cButton:OnMousePressed( m )
self.State = 1; – Mouse Down
if ( self.OnMouseDown != nil ) then
self.OnMouseDown( m );
end
end
function cButton:OnMouseReleased( m )
self.State = 0; – Mouse Up
if ( self.OnMouseUp != nil ) then
self.OnMouseUp( m );
end
end

function cButton:Paint()
// Draw Border

if ( self.State == 1 ) then
surface.SetDrawColor( 0,0,200,255 ); – Blue
surface.DrawRect( 4,4,self:GetWide()-8,self:GetTall()-8 );
else // The only other state should be 0, but if it’s something else for some weird reason, we still want it.
surface.SetDrawColor( 200,100,0,255 ); – Orange
surface.DrawRect( 0,0,self:GetWide(),self:GetTall() );
end

// Draw Inside
surface.SetDrawColor( 100,100,100,255 ); – Orange
surface.DrawRect( 4,4,self:GetWide()-8,self:GetTall()-8 );
// Draw Text
//Text, font, x, y, color, align
draw.DrawText( self.Text, self.Font, self:GetWide() / 2, 3, Color(255,255,255,255 ), 1);
return true

end

cFrame = vgui.Create( “frame” );
cFrame:SetSize( 200,100 );
cFrame:SetPos( 0,0 );
cFrame:PostMessage( “SetTitle”, “text”, “cLabel Test” );
cFrame:SetVisible( true );

cLabelTest = vgui.Create( “cLabel”, cFrame );
cLabelTest:SetSize( 100, 25 );
cLabelTest:SetPos( 50, 20 );
cLabelTest:SetText( “Test” );
cLabelTest:SetAlign( 1 );

cButtonTest = vgui.Create( “cButton”, cFrame );
cButtonTest:SetSize( 100, 25 );
cButtonTest:SetPos( 55, 35 );
cButtonTest:SetText( “Click Me!” );
cButtonTest.OnMouseDown = function() cLabelTest:SetText(“Mouse Down!”) end
cButtonTest.OnMouseUp = function() cLabelTest:SetText(“Mouse Up!”) end

vgui.Register( “cLabel”, cLabel, “Panel” );[/lua]

vgui.Create returned nil, something went wrong with creating clabel.

Umm, you register cLabel after you create a cLabel. That won’t work. You have to register, THEN create.