Metatable problem (can't call the function from it)
5 replies, posted
[b] experience_system.lua [/b]
[lua]local metatableExp = FindMetaTable( "Player" )
local Client_Exp = "TotalExp"
-- Starting the system
local ExpSystem = {}
-- Used as ply:ResetExp( amount ) to reset a players Exp to an amount
function metatableExp:ResetExp(amount)
self:SetPData(Client_Exp, tonumber(amount))
self:SetNWInt(Client_Exp , tonumber(amount))
end
-- Used as ply:StartExp( amount) to give a starting amount of Exp ( in other words, ply:GiveExp( amount))
function metatableExp:StartExp(amount)
self:SetNWInt(Client_Exp, tonumber(amount))
end
-- Used as ply:GetExp() to get the player's Expsss
function metatableExp:GetExp()
return tonumber(self:GetPData(Client_Exp))
end
-- Used as ply:Add_ManhackExp( amount ) to give the player Exp when he kills a manhack.
-- ** Hooking and ConCommanding required as it is not defaulted to that task **
function metatableExp:Add_ManhackExp(amount)
self:SetPData(Client_Exp, tonumber(self:GetNWInt("TotalExp")) + tonumber(amount))
self:SetNWInt(Client_Exp, tonumber(self:GetPData(Client_Exp)))
end
function metatableExp:RemoveExp(amount)
self:SetPData(Client_Exp, tonumber(self:GetPData(Client_Exp)) - tonumber(amount))
self:SetNWInt(Client_Exp, tonumber(self:GetPData(Client_Exp)))
end
-- Used as ply:Add_PlayerExp( amount ) to give the player Exp when he kills another player.
-- ** Hooking and ConCommanding required as it is not defaulted to that task **
function metatableExp:Add_PlayerExp(amount)
self:SetPData(Client_Exp, tonumber(self:GetPData(Client_Exp)) + tonumber(amount))
self:SetNWInt(Client_Exp, tonumber(self:GetPData(Client_Exp)))
end
-- Used as ply:BonusExp( amount ) to add Exp to the player as a bonus
-- ** Not defaulted to a Bonus Task **
function metatableExp:BonusExp(amount)
self:SetPData(Client_Exp, tonumber(self:GetPData(amount)) + tonumber(amount))
self:SetNWInt(Client_Exp, tonumber(self:GetPData(amount)))
end
[/lua]
[b] level_system.lua [/b]
[lua]local metatableLvl = FindMetaTable( "Player" )
local Level = "PlayerLevel"
-- Starting the System
local LevelSystem = {}
-- Used as ply:SetLevel( amount ) to give the player a level
function metatableLvl:SetLevel(amount)
self:SetPData(Level, tonumber(amount))
self:SetNWInt(Level, tonumber(amount))
end
-- Used as ply:LevelUp( amount ) to add a level to the player
function metatableLvl:LevelUp(amount)
self:SetPData(Level, tonumber(self:GetPData(Level)) + tonumber(amount))
self:SetNWInt(Level, tonumber(self:GetPData(Level)))
end
-- Used as ply:GetLevel() to know the player's Level
function metatableLvl:GetLevel()
return tonumber(self:GetPData(Level))
end
[/lua]
[b] console error [/b] [code]
Hook 'PaintOurHud' Failed: ManhackSurvival2.0/gamemode/cl_init.lua:207: attempt to concatenate local 'Exp' (a nil value)
[/code]
before you start saying that Exp is not defined, Exp [b]IS[/b] defined in cl_init.lua as GetExp()
problem is that the function IS defined in my metatable. It should be called but it doesn't.
Can anyone just tell me if my metatables are alright so I can search the problem somewhere else?
PS: Same problem with the level_system.lua...
The code where the error actually happens would be useful.
here it is
[b]cl_init.lua[/b]
[lua]-- This Code is nasty, we really need to make a clean up shoax.
-- Its fucking important, it is poorly organized!
AddCSLuaFile( "experience_system.lua" )
AddCSLuaFile( "level_system.lua" )
include( 'shared.lua' )
include( 'experience_system.lua' )
include( 'level_system.lua' )
function GM:HUDPaint()
self.BaseClass:HUDPaint()
gm_hud = { };
local function clr( color ) return color.r, color.g, color.b, color.a; end
function gm_hud:PaintBar( x, y, w, h, colors, value )
self:PaintPanel( x, y, w, h, colors );
x = x + 1; y = y + 1;
w = w - 2; h = h - 2;
local width = w * math.Clamp( value, 0, 1 );
local shade = 4;
surface.SetDrawColor( clr( colors.shade ) );
surface.DrawRect( x, y, width, shade );
surface.SetDrawColor( clr( colors.fill ) );
surface.DrawRect( x, y + shade, width, h - shade );
end
function gm_hud:PaintPanel( x, y, w, h, colors )
surface.SetDrawColor( clr( colors.border ) );
surface.DrawOutlinedRect( x, y, w, h );
x = x + 1; y = y + 1;
w = w - 2; h = h - 2;
surface.SetDrawColor( clr( colors.background ) );
surface.DrawRect( x, y, w, h );
end
function gm_hud:PaintBackGround( x, y, w, h )
x = x + 1; y = y + 1;
w = w - 2; h = h - 2;
draw.RoundedBox( 10, x, y, w, h, Color( 0, 0, 0, 150 ))
end
function gm_hud:PaintText( x, y, text, font, colors )
surface.SetFont( font );
surface.SetTextPos( x + 1, y + 1 );
surface.SetTextColor( clr( colors.shadow ) );
surface.DrawText( text );
surface.SetTextPos( x, y );
surface.SetTextColor( clr( colors.text ) );
surface.DrawText( text );
end
function gm_hud:TextSize( text, font )
surface.SetFont( font );
return surface.GetTextSize( text );
end
local vars =
{
font = "TargetID",
padding = 10,
margin = 20,
text_spacing = 2,
bar_spacing = 5,
bar_height = 16,
width = 0.25
};
local colors =
{
background =
{
border = Color( 190, 255, 128, 0 ),
background = Color( 120, 240, 0, 0 )
},
text =
{
shadow = Color( 0, 0, 0, 200 ),
text = Color( 255, 255, 255, 255 )
},
health_bar =
{
border = Color( 0, 0, 0, 255 ),
background = Color( 255, 0, 0, 50 ),
shade = Color( 255, 5, 5, 255 ),
fill = Color( 220, 15, 15, 255 )
},
exp_bar =
{
border = Color( 0, 0, 0, 255 ),
background = Color( 255, 165, 0, 50 ),
shade = Color( 255, 190, 0, 255 ),
fill = Color( 255, 165, 0, 255 )
},
lvl_bar =
{
border = Color( 0, 255, 0, 255 ),
background = Color( 20, 255, 20, 50 ),
fill = Color( 50, 225, 50, 255 ),
shade = Color( 20, 255, 20, 255 )
}
};
local ply = LocalPlayer()
local level = ply:GetLevel()
local maxhudbarxp = 0
if level == 0 then maxhudbarxp = 15
ply:ResetExp( 0 )
ply:SetLevel( 1 )
elseif level == 1 then maxhudbarxp = 75
ply:ResetExp( 0 )
ply:SetLevel( 2 )
elseif level == 2 then maxhudbarxp = 150
ply:ResetExp( 0 )
ply:SetLevel( 3 )
elseif level == 3 then maxhudbarxp = 300
ply:ResetExp( 0 )
ply:SetLevel( 4 )
elseif level == 4 then maxhudbarxp = 600
ply:ResetExp( 0 )
ply:SetLevel( 5 )
elseif level == 5 then maxhudbarxp = 1200
ply:ResetExp( 0 )
ply:SetLevel( 6 )
elseif level == 6 then maxhudbarxp = 2400
ply:ResetExp( 0 )
ply:SetLevel( 7 )
elseif level == 7 then maxhudbarxp = 4800
ply:ResetExp( 0 )
ply:SetLevel( 8 )
elseif level == 8 then maxhudbarxp = 9600
ply:ResetExp( 0 )
ply:SetLevel( 9 )
elseif level == 9 then maxhudbarxp = 19200
ply:ResetExp( 0 )
ply:SetLevel( 10 )
elseif lvl == 10 then maxhudbarxp = 38400
end
local ply = LocalPlayer()
local lvl = ply:GetLevel()
local function HUDPaint( )
client = client or LocalPlayer( );
ply = LocalPlayer() -- set a shortcut to the client
if( !client:Alive( ) ) then return; end -- don't draw if the client is dead
local _, th = gm_hud:TextSize( "TEXT", vars.font ); -- get text size( height in this case )
local i = 2; -- shortcut to how many items( bars + text ) we have
local width = vars.width * ScrW( ); -- calculate width
local bar_width = width - ( vars.padding * i ); -- calculate bar width and element height
local height = ( vars.padding * i ) + ( th * i ) + ( vars.text_spacing * i ) + ( vars.bar_height * i ) + vars.bar_spacing;
local x = vars.margin; -- get x position of element
local y = ScrH( ) - vars.margin - height; -- get y position of element
local cx = x + vars.padding; -- get x and y of contents
local cy = y + vars.padding;
gm_hud:PaintPanel( x, y, width, height, colors.background ); -- paint the background panel
gm_hud:PaintBackGround( x, y, width, height )
local by = th + vars.text_spacing; -- calc text position
local text = string.format( "Health: %i HP", client:Health( ) ); -- get health text
gm_hud:PaintText( cx, cy, text, vars.font, colors.text ); -- paint health text and health bar
gm_hud:PaintBar( cx, cy + by, bar_width, vars.bar_height, colors.health_bar, client:Health( ) / 100 );
gm_hud:PaintBar( ScrW() - 140, ScrH() - 58, 80, 12, colors.lvl_bar, ply:GetNWInt("PlayerLevel") / 10 )
by = by + vars.bar_height + vars.bar_spacing; -- increment text position
local shade = 2
local level = ply:GetLevel()
local Exp = ply:GetExp()
local text = string.format( "Experience: ".. Exp .. " XP", "hudtxt" );
gm_hud:PaintText( cx, cy + by, text, vars.font, colors.text );
gm_hud:PaintBar( cx, cy + by + th + vars.text_spacing, bar_width, vars.bar_height, colors.exp_bar, maxhudbarxp );
draw.RoundedBox( 12, ScrW() - 150, ScrH() - 125, 100, 105, Color( 0, 0, 0, 150 ))
draw.SimpleText("Lvl: ".. level .. " / 10", "hudtxt", ScrW() - 137 , ScrH() - 100, Color(255, 255,255, 255), 0, 0)
end
hook.Add( "HUDPaint", "PaintOurHud", HUDPaint );
function hidehud(name)
for k, v in pairs{"CHudHealth", "CHudBattery", "CHudAmmo", "CHudSecondaryAmmo"} do
if name == v then return false end
end
end
hook.Add("HUDShouldDraw", "hidehud", hidehud)
end -- End of HUD DERMA FOLLOWING
----------------------------------------------------------------
-- Derma Menus
function GMMenu()
local MainMenu = {}
local Menu = vgui.Create("DFrame")
Menu:SetSize( 600, 400 )
Menu:SetVisible( true )
Menu:SetTitle( "Team Selection" )
Menu:SetDraggable( true )
Menu:SetPos( ScrW() / 2 - 300, ScrH() / 2 - 300 )
Menu:ShowCloseButton( true )
Menu:MakePopup()
function Menu:Paint()
surface.SetDrawColor( 0, 0, 0, 0 )
draw.RoundedBox( 10, 0, 0, Menu:GetWide(), Menu:GetTall(), Color( 0, 0, 0, 190 ) )
end
local ply = LocalPlayer()
local MenuBox = vgui.Create("DPanel", Menu)
MenuBox:SetSize( 500, 300 )
MenuBox:SetPos( 50, 50 )
MenuBox.Paint = function()
surface.SetDrawColor( 0, 0, 0, 0 )
draw.RoundedBox( 10, 0, 0, MenuBox:GetWide(), MenuBox:GetTall(), Color( 23, 23, 23, 200 ) )
end
local button = vgui.Create( "DButton", MenuBox );
button:SetSize( 100, 100 );
button:SetPos( 100, 100 );
button:SetText( "Team Humans" );
button.Paint = function() -- The paint function
surface.SetDrawColor( 0, 0, 0, 0 ) -- What color do You want to paint the button (R, B, G, A)
draw.RoundedBox( 15, 0, 0, button:GetWide(), button:GetTall(), Color( 70, 250, 70, 255 ) ) -- Paint what cords (Used a function to figure that out)
end
button.DoClick = function( button )
RunConsoleCommand( "team_1" )
ply:ChatPrint( " " .. ply:GetName() .. " joined Team Humans" )
Menu:SetVisible( false )
end
local button2 = vgui.Create( "DButton", MenuBox );
button2:SetSize( 100, 100 );
button2:SetPos( 300, 100 );
button2:SetText( "Team Combine" );
button2.Paint = function() -- The paint function
surface.SetDrawColor( 0, 0, 0, 0 ) -- What color do You want to paint the button (R, B, G, A)
draw.RoundedBox( 15, 0, 0, button2:GetWide(), button2:GetTall(), Color( 70, 70, 250, 255 ) ) -- Paint what cords (Used a function to figure that out)
end
button2.DoClick = function( button2 )
RunConsoleCommand( "team_2" )
ply:ChatPrint( " " .. ply:GetName() .. " joined Team Combine" )
Menu:SetVisible( false )
end
end
concommand.Add( "MHGM_MainMenu", GMMenu )
[/lua]
[b]Edit : [/b]Exp [b]seems[/b] to work but level refuses to work...
[lua]function metatableExp:GetExp()
return tonumber(self:GetPData(Client_Exp)) or 0
end[/lua]
thanks dude it worked!
Also, replace that leveling stuff with this:
[lua]if( level == 0 ) then
maxhudbarxp = 15
else
maxhudbarxp = ( 2^(level-1) * 75 )
end
ply:ResetExp( 0 )
ply:SetLevel( level + 1 )[/lua]
Sorry, you need to Log In to post a reply to this thread.