• Print

Author Topic: Scoreboard Help  (Read 8487 times)

0 Members and 1 Guest are viewing this topic.

Offline Decicus

  • Hero Member
  • *****
  • Posts: 552
  • Karma: 81
    • Alex Thomassen
Scoreboard Help
« on: August 16, 2013, 11:02:07 am »
Hello.
So I've been messing around to add custom colors to ranks on the scoreboard. These ranks are shown in it's own column.

Now I've also been trying to be able to be able to add colors to a table(?) and let it read from that. This is an example of the current code.

(Original code is from this post on Zombiemaster.org)
Code: Lua
  1.                 if ply:IsUserGroup("admins") then --Obviously admins
  2.                                 self.cols[5]:SetText("Admin")
  3.                                 self.cols[5]:SetTextColor(Color(204, 204, 0, 255))
  4.                 elseif ply:IsUserGroup("moderators") then --Obviously moderators
  5.                                 self.cols[5]:SetText("Moderator")
  6.                                 self.cols[5]:SetTextColor(Color(102, 153, 51, 255))
  7.                 elseif ply:IsUserGroup("user") then --Normal players
  8.                                 self.cols[5]:SetText("Guest")
  9.                                 self.cols[5]:SetTextColor(Color(255, 255, 255, 255))
  10.                 elseif ply:SteamID() == "STEAM_0:0:00000000" then --Test
  11.                                 self.cols[5]:SetText("Custom")
  12.                                 self.cols[5]:SetTextColor(Color(0, 0, 255, 255))
  13.  

Now, this works fine, although, I wish to make it into something like this:
Code: Lua
  1. local Specials = {
  2. ["STEAM_0:0:00000000"] = {Rank = "Custom rank here"} = {Color = "255, 255, 0, 255"}, --Test
  3. }
  4.  
  5. local special = Specials[ply:SteamID()]
  6.         if special then
  7.                 self.cols[5]:SetText(special.Rank)
  8.                 self.cols[5]:SetTextColor(Color(special.Color))
  9.         end
  10.  

I've tried this, but it only kicked me out with errors (I need to re-do this to find the errors, if you need them).
So it doesn't work like shown above, but I want it use a similar layout. I find it annoying having to add another "elseif" with the whole "ply:SteamID() == "STEAMID"" deal.

Anyone able to help me with this? :)
« Last Edit: August 16, 2013, 11:03:39 am by Decicus »
Contact information:
E-mail: alex@thomassen.xyz.
You can also send a PM.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Scoreboard Help
« Reply #1 on: August 16, 2013, 01:58:44 pm »
Rusty brain. Been long time since I've actually opened up my notepad++ and entered any lua in.
This should give you basic idea though. I renamed the variables. Some are obvious why, some may not be. Ask me questions.

Code: [Select]
local Specials = {
    "Groupname" = {color = "255, 255, 0, 255"},
    "Groupname" = {color = "255, 0, 0, 255},
} -- Groupname MUST be lowercase and match your server group names exactly.
-- lowercase the 'c' in color, to be safe. Color() is a command. MIGHT cause error later if we forget and get mixed up somewhere.

if ulib then
local group = ply:GetUserGroup() -- to my knowledge, this is a ULib only command (ie, server will HAVE to have ULib) - http://ulyssesmod.net/docs/files/lua/ulib/shared/sh_ucl-lua.html#Player:GetUserGroup
if Specials.group.color then
self.cols[5]:SetText(group)
self.cols[5]:SetTextColor(Color(Specials.group.color))
end
end

There _is_ a way to do it without the ULib specific 'getusergroup' command, but, it's messy.
I'll show you if you REALLY demand it.
(But, why not use ULib?) :P
« Last Edit: August 16, 2013, 02:28:25 pm by JamminR »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Decicus

  • Hero Member
  • *****
  • Posts: 552
  • Karma: 81
    • Alex Thomassen
Re: Scoreboard Help
« Reply #2 on: August 16, 2013, 03:01:59 pm »
Although, I understand what you mean, it sort of doesn't really work out with my idea. I should've been more specific, but we have donators on our server (instead of "Specials", I probably should've put "Donators").

So when they donate, they get the ability to request a custom title + color. And while I could still keep to use their SteamID and continue with "elseif ply:SteamID() == "STEAMID"", I wanted it to look a bit more clean by grabbing the rank + color from that variable(? still confused what to call that "local Specials = {}" thing). It would help my fellow serverside admins to add new people to that list as well, since they're pretty much completely blank when it comes to code, and for some reason using that variable(? again, that local thing) helps them.

My idea also conflicts with the "GetUserGroup" you're using. While I appreciate what you put up for me, it's not what I'm looking for.
If you can, post the messy way to do it (I'm demanding it!) .

Thanks JamminR, but yeah, it wasn't entirely what I wanted.
Contact information:
E-mail: alex@thomassen.xyz.
You can also send a PM.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Scoreboard Help
« Reply #3 on: August 16, 2013, 07:21:19 pm »
How are you tracking who donated?
Are you adding them to a new group? (reasonable)
Tracking each individually by steamid? (INSANE)

So your 'ranks' are just randomly created titles?
Are you going to force them to be one word?

I might be able to work with it more if I only knew.
« Last Edit: August 16, 2013, 07:42:35 pm by JamminR »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Scoreboard Help
« Reply #4 on: August 16, 2013, 07:56:58 pm »
This isn't tested, but you'll get less errors and be more likely to figure it out.

Code: [Select]
local donators = {
["STEAM_0:0:00000000"] = {rank = "Custom rank 1 here", color = "255, 255, 0, 255"},
["STEAM_0:0:00000000"] = {rank = "Custom rank 2 here", color = "255, 0, 255, 255"},
}
     
local special = donators[ply:SteamID()]
if special then
    self.cols[5]:SetTextColor(Color(special.color))
    self.cols[5]:SetText(special.rank)
end

You might get errors with color.
I've not used it much, but think I remember seeing before where you couldn't assign it like a string.
If errors with color, try...
Code: [Select]
["STEAM_0:0:00000000"] = {rank = "Custom rank 1 here", color = { r,g,b,a = 255,255,0,255 } },    in the table area and
Code: [Select]
self.cols[5]:SetTextColor( special.color.r, special.color.b, special.color.g, special.color.a )   obviously, where the text color line goes.

Yes, there's some possibly complicated looking table assignments there. Multidimensional, and tricks of assigning variables, within a table.
It's not complicated really, just looks that way.
« Last Edit: August 16, 2013, 08:18:00 pm by JamminR »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Decicus

  • Hero Member
  • *****
  • Posts: 552
  • Karma: 81
    • Alex Thomassen
Re: Scoreboard Help
« Reply #5 on: August 17, 2013, 03:25:46 pm »
That seems to what I was thinking of.

Thanks a lot for your help JamminR, I'll test it out later.
Contact information:
E-mail: alex@thomassen.xyz.
You can also send a PM.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Scoreboard Help
« Reply #6 on: August 17, 2013, 06:41:29 pm »
Just realized in my 'maybe fixed' setcolor line I used rbga instead of the rgba. You can correct.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Bite That Apple

  • Hero Member
  • *****
  • Posts: 858
  • Karma: 416
  • Apple Innovations 2010®
    • Fun 4 Everyone Gaming
Re: Scoreboard Help
« Reply #7 on: August 18, 2013, 05:27:04 am »
This isn't tested, but you'll get less errors and be more likely to figure it out.

Code: [Select]
local donators = {
["STEAM_0:0:00000000"] = {rank = "Custom rank 1 here", color = "255, 255, 0, 255"},
["STEAM_0:0:00000000"] = {rank = "Custom rank 2 here", color = "255, 0, 255, 255"},
}
     
local special = donators[ply:SteamID()]
if special then
    self.cols[5]:SetTextColor(Color(special.color))
    self.cols[5]:SetText(special.rank)
end


I'm quite happy now that I opened this thread by accident. This helped me with an issue, seeing as my table skills are lacking and I can't find a guide that work with garry's mod tables.. Anyways though, this should work for what you want it to do, as this table idea worked what what I need it to do.

Code: [Select]
local donators = {
["STEAM_0:0:00000000"] = {rank = "Custom rank 1 here", color = "255, 255, 0, 255"},
["STEAM_0:0:00000000"] = {rank = "Custom rank 2 here", color = "255, 0, 255, 255"},
}

Though if I recall from what I do know about tables, the comma at the end line 3 should not be there.
« Last Edit: August 18, 2013, 05:29:34 am by chaos13125 »
Quote from: John F. Kennedy 1963
A man may die, nations may rise and fall, but an idea lives on.

Offline Decicus

  • Hero Member
  • *****
  • Posts: 552
  • Karma: 81
    • Alex Thomassen
Re: Scoreboard Help
« Reply #8 on: August 18, 2013, 07:29:54 am »
The line 3 comma doesn't really do anything if there isn't anything after it. Although it's required if I want to add another "entry" to the table.
Contact information:
E-mail: alex@thomassen.xyz.
You can also send a PM.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Scoreboard Help
« Reply #9 on: August 18, 2013, 08:57:47 am »
Chaos, to my knowledge, lua tables are no different in gmod than in any other lua environment.
You just get to hold Source/Gmod specific information in them.
If you can find a good lua table tutorial anywhere, it would be good for any <app/gmod/whatever> that uses lua.
They can and become pretty complex.
Not so much because of the way they store information, but probably more so because they can be multi-layered.

Decicus, did you find that you couldn't use a string for the color object (ie, color = "255, 255, 0, 255" didn't work)?
I'm thinking the object can only hold integers in the variable (the color = { r,g,b,a = 255,255,0,255 } } line I suggested.)

"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Decicus

  • Hero Member
  • *****
  • Posts: 552
  • Karma: 81
    • Alex Thomassen
Re: Scoreboard Help
« Reply #10 on: August 19, 2013, 04:46:09 am »
So I tested it, and got this giant error (it's an image, because the error didn't print to logs...):

Line 246 is the last line on the "code box" on the bottom of this post. I will attach all files that are included in the errors ("gamemode" folder files).


Now, I looked on the util.lua, line 33.
It looks to me that it's conflicting with something that is "GMod default". Not really sure what this does, it's something with colors, of course.
Code: [Select]
--[[---------------------------------------------------------
To easily create a colour table
-----------------------------------------------------------]]
function Color( r, g, b, a )

a = a or 255
return { r = math.min( tonumber(r), 255 ), g =  math.min( tonumber(g), 255 ), b =  math.min( tonumber(b), 255 ), a =  math.min( tonumber(a), 255 ) }

end

And lastly, my code, the code that I used. It's not the whole function.
Code: [Select]
local Custom = {
["STEAM_0:1:18726919"] = {rank = "Custom", color = {r, g, b = 255, 0, 0}}, --Test.
}

local cust = Custom[ply:SteamID()]

elseif cust then
self.cols[5]:SetText(cust.rank)
self.cols[5]:SetTextColor(Color(cust.color.r, cust.color.g, cust.color.b, 255))

If you wish to take a look at all the files mentioned in the giant lua error (.zip file, more than 4 files :P), they're attached to this post (beware, some of them might have messy, unnecessary code).
Contact information:
E-mail: alex@thomassen.xyz.
You can also send a PM.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Scoreboard Help
« Reply #11 on: August 19, 2013, 09:02:38 am »
Few extreme quick things to try (Not sure I have more time to dig deeper or look at the files).
First, though it MIGHT be bit different now, I had trouble years ago setting comments -in- a table.
Set your --test after the last } table bracket.

Second -
Wiki.garrysmod.com says settextcolor should be before the text. Try moving the settextcolor line before your rank set line.

Third, and this is just something I'd do while tinkering in such a unfamiliar-to-me Gmod gui section.
Though settextcolor says it can be a Color object, it states it can be 'or r'.
Try removing the object, and using SetTextColor(cust.color.r, cust.color.g, cust.color.b, 255)
There's something about "using a color object is not recommended to be created prodecural. " too at that page.
I'm not 'trained' coder enough to know what a procedural creation is, so recommend trying just the number values with no Color().

May also try, just for testing purposes, setting the numbers static, not using the table look up.
SetTextColor(Color(255, 0, 0, 255)) or like I said in three, SetTextColor(255, 0, 0, 255)
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Stickly Man!

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 1270
  • Karma: 164
  • What even IS software anymore?
    • XGUI
Re: Scoreboard Help
« Reply #12 on: August 20, 2013, 07:48:29 am »
Just a quick guess- I'm pretty sure the problem is how you're going about storing/using the color. I would recommend doing this:

Code: Lua
  1. local Custom = {
  2.         ["STEAM_0:1:18726919"] = {rank = "Custom", color = Color(255, 0, 0)}, --Test.
  3. }
  4.  
  5. local cust = Custom[ply:SteamID()]
  6.  
  7. elseif cust then
  8.         self.cols[5]:SetText(cust.rank)
  9.         self.cols[5]:SetTextColor(cust.color)
  10.  

.. I *think* that should work lol

(Note that if alpha is not specified in Color(), it automatically assumes that it's 255)

JamminR, your method would work fine as well. Color(r,g,b[,a]) is just a helper function that makes a table out of the rgba values. The wiki is warning that you don't need to create a Color object JUST to pass it into the function-- or as an example, don't do SetTextColor(Color(255,0,0,255)) when SetTextColor(255,0,0,255) accomplishes the same thing without creating the intermediary object.

In Decicus's case as shown in my code snippet above, he can use the convenience of Color() when storing the color in his table (since it should only be called once), and then he can pass that color into whichever function can support the Color object (which is most everything). So it really is there to make the code a bit more readable.

Here's hoping that my lua brain is still working fine-- I've been doing too much perl over here! :P
« Last Edit: August 20, 2013, 08:04:29 am by Stickly Man! »
Join our Team Ulysses community discord! https://discord.gg/gR4Uye6

Offline Decicus

  • Hero Member
  • *****
  • Posts: 552
  • Karma: 81
    • Alex Thomassen
Re: Scoreboard Help
« Reply #13 on: August 20, 2013, 10:54:26 am »
Thanks for all the help I've gotten.

JamminR, I have to say that I appreciate what you have told me so far and such. Stickly Man ended up with the win right here. I did what he told me, and it worked!

Thank you both for helping me out with this, I really appreciate it!
Contact information:
E-mail: alex@thomassen.xyz.
You can also send a PM.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Scoreboard Help
« Reply #14 on: August 20, 2013, 12:44:40 pm »
Welcome. I remembered that color was tricky, and couldn't just be passed a string.
I just didn't remember how I'd seen it passed months/years ago.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

  • Print