player class function doesn't work on a certain case
4 replies, posted
hi everybody !
this function draw a icon over the head of each players ( in a hook "HUDPaint" )
there a problem when i use " player_manager.RunClass() " in this function, he doesn't work
but when i use this in an another file like shared.lua he works ...
playerhud.lua
[CODE]AddCSLuaFile()
if CLIENT then
local function DrawThePlayerHUD()
for k,v in pairs(player.GetAll()) do
if v:GetPos():Distance(LocalPlayer():GetPos()) <= 100000 then
if v:Alive() then
-- Some stuff ....
print( player_manager.GetPlayerClass(v) ) -- this print fine "player_nobby"
print( player_manager.RunClass( v, "getGradeIcon" ) ) -- doesnt work, print a empty string
surface.SetTexture( surface.GetTextureID("gradeicons/nobby") ) -- but this works fine
surface.SetDrawColor(Color(255,255,255,255))
surface.DrawTexturedRect(XPos - 80,YPos,CSize,CSize)
draw.DrawText(v:Name(),"Trebuchet24",XPos,YPos-20, Color(PlyClr.x,PlyClr.y,PlyClr.z,alpha) ,TEXT_ALIGN_CENTER)
draw.DrawText(v:Health() ,"Trebuchet24",XPos+10,YPos+5, Color(PlyClr.x,PlyClr.y,PlyClr.z,alpha) ,TEXT_ALIGN_CENTER)
end
end
end
end
hook.Add("HUDPaint","nameitHUD",DrawThePlayerHUD)
end
[/CODE]
The player class : (everything works in shared.lua
[CODE]
AddCSLuaFile()
-- Class nobby :
DEFINE_BASECLASS( "player_default" )
local PLAYER = {}
--class attributes :
PLAYER.WalkSpeed = 160
PLAYER.RunSpeed = 160
PLAYER.playerModel = "models/player/Group03/Male_03.mdl"
PLAYER.weapons = {"weapon_pistol","hogs_rifle","hogs_grenade"}
PLAYER.gradeIcon = "gradeicons/nobby"
--Methods :
PLAYER.setPlayerModel = function( tbl )
local ply = tbl.Player
ply:SetModel(PLAYER.playerModel)
end
PLAYER.getClassWeapons = function()
return PLAYER.weapons
end
PLAYER.getGradeIcon = function()
return PLAYER.gradeIcon
end
player_manager.RegisterClass( "player_nobby", PLAYER, "player_default" )
[/CODE]
so i dont know what is wrong ....
(also i know how poo works in C# , but not at all about lua poo)
thanks :)
[lua]-- Your code:
PLAYER.getGradeIcon = function()
return PLAYER.gradeIcon
end
-- Fixed code:
PLAYER.getGradeIcon = function(ply)
return ply.gradeIcon
end
-- More self-descriptive code (works the same as the fixed code above):
function PLAYER:getGradeIcon()
return self.gradeIcon
end[/lua]
The PLAYER table doesn't exist when functions run.
[editline]16th September 2016[/editline]
[QUOTE=BALIST0N;51059595]also i know how poo works in C# , but not at all about lua poo[/QUOTE]
I'm pretty sure that poo comes out of your butt no matter where you're from
thanks,
but console say :
[CODE][ERROR] .../gamemode/playernameshud.lua:38: attempt to call method 'getGradeIcon' (a nil value)
[/CODE]
playerclass :
[CODE]
PLAYER.gradeIcon = "gradeicons/nobby"
function PLAYER:getGradeIcon()
return self.gradeIcon
end
[/CODE]
hudpaint :
[CODE]
for k,v in pairs(player.GetAll()) do
print( v:getGradeIcon() )
end
[/CODE]
also tried to print this somewhere else but i have the same error
do i did something wrong ?
I think you still need to use RunClass. I'm a bit confused about this myself to be honest.
[QUOTE=NeatNit;51059762]I think you still need to use RunClass. I'm a bit confused about this myself to be honest.[/QUOTE]
player_manager.RunClass still return a empty string :/ ..
-- EDIT --
[CODE]print( type(player_manager.RunClass(ply, "getGradeIcon")) )[/CODE]
correction , its not a string its "no value"
Sorry, you need to Log In to post a reply to this thread.