Hello, I have a issue with something I am trying to do. I hope someone here can help me!
I have different player classes, and I just want to display the class name on a hud. Here is my sad attempts.
Here is the HUD code, I know the issue is here.
[CODE]
function HUD()
-- Variable to make the player the client
local client = LocalPlayer()
-- if Client is not alive then return don't display hud.
if !client:Alive() then
return
end
draw.RoundedBox(0, 0, ScrH() - 145, 250, 40, Color(0, 0, 0, 125))
draw.SimpleText("Race: " .. CLASS['DisplayName'], "DermaDefaultBold", 100, ScrH() - 140, Color(255, 255, 255, 255), 0) -- This line is the issue.
end
hook.Add("HUDPaint", "MainHUD", HUD)
[/CODE]
I don't think I need to post the class file on here as it's just a regular class table...
I know my syntax is just wrong, I just can't find what I'm doing wrong so I'm posting here.
Please go easy on me here.
Thanks for your time!
What is the error that is tossed? Is the table CLASS declared somewhere in the file? Maybe instead of CLASS['DisplayName'] do CLASS["DisplayName"] or try CLASS.DisplayName
It says CLASS is a nil value on both those suggestions, and as well as my original attempts.
I will also point out that if I put this in my class file it will work
[CODE]function CLASS:OnSpawn( ply )
print(CLASS['DisplayName'])
end[/CODE]
Am I missing something with passing it to the hud file by chance?
[QUOTE=Severity;50625368]It says CLASS is a nil value on both those suggestions, and as well as my original attempts.[/QUOTE]
Then that means CLASS isn't initialized. You need a reference to the CLASS table to be able to do that.
Think of it like this. You are hungry, and you crave a cookie. You know there is a cookie inside of the fridge, so you open it, and you look around. The problem is the fridge is so big you couldn't possible just find the cookie without knowing where in the fridge it is first.
You need the location of the cookie, and in this case, a variable referencing the table CLASS.
[editline]30th June 2016[/editline]
[QUOTE=Severity;50625368]It says CLASS is a nil value on both those suggestions, and as well as my original attempts.
I will also point out that if I put this in my class file it will work
[CODE]function CLASS:OnSpawn( ply )
print(CLASS['DisplayName'])
end[/CODE]
Am I missing something with passing it to the hud file by chance?[/QUOTE]
How are you passing it to the HUD file?
Assuming that CLASS is meant to represent the player class system, there's no easy way to do this because the player_manager library that handles the player class stuff has no method to access the actual table of a player class, despite the stock player classes including DisplayName properties themselves. Your best bet would be to include something like this:
[code]
function CLASS:GetDisplayName() return self.DisplayName end
[/code]
in your class, then call it by doing this:
[code]
local class_name = player_manager.RunClass( client, "GetDisplayName" )
[/code]
in your drawing code.
Okay let me try this for a few minutes, I will report back.
[editline]1st July 2016[/editline]
[QUOTE=Joeyl10;50625416]Assuming that CLASS is meant to represent the player class system, there's no easy way to do this because the player_manager library that handles the player class stuff has no method to access the actual table of a player class, despite the stock player classes including DisplayName properties themselves. Your best bet would be to include something like this:
[code]
function CLASS:GetDisplayName() return self.DisplayName end
[/code]
in your class, then call it by doing this:
[code]
local class_name = player_manager.RunClass( client, "GetDisplayName" )
[/code]
in your drawing code.[/QUOTE]
This is my exact issue, but my attempt didn't work. I added the function to the class, then I added the variable to the hud and it's giving me this error
attempt to concatenate local 'class_name'
Is this right by chance?
draw.SimpleText("Race: " .. class_name, "DermaDefaultBold", 100, ScrH() - 140, Color(255, 255, 255, 255), 0)
[editline]1st July 2016[/editline]
[QUOTE=overki11;50625395]Then that means CLASS isn't initialized. You need a reference to the CLASS table to be able to do that.
Think of it like this. You are hungry, and you crave a cookie. You know there is a cookie inside of the fridge, so you open it, and you look around. The problem is the fridge is so big you couldn't possible just find the cookie without knowing where in the fridge it is first.
You need the location of the cookie, and in this case, a variable referencing the table CLASS.
[editline]30th June 2016[/editline]
How are you passing it to the HUD file?[/QUOTE]
I'm not exactly passing it to the hud, I figured that was my issue and couldn't figure out how to.
Sorry, you need to Log In to post a reply to this thread.