Ulysses
General => Developers Corner => Topic started by: easer7 on January 03, 2013, 08:44:35 PM
-
How would I give tags to moderators and admins?
This is the code I have so far.
if (SERVER) then
AddCSLuaFile("autorun/tags.lua")
end
if (CLIENT) then
function Insert( pl, msg )
local tab = {}
if pl:IsUserGroup("headmanagement") then
table.insert( tab, Color( 255, 0, 0, 255 ) )
table.insert( tab, "[Head Management] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("superadmin") then
table.insert( tab, Color( 0, 255, 0, 255 ) )
table.insert( tab, "[Head Management] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("admin") then
table.insert( tab, Color( 0, 255, 0, 255 ) )
table.insert( tab, "[Administrator] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("leadmanagement") then
table.insert( tab, Color( 0, 255, 0, 255 ) )
table.insert( tab, "[Lead Management] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("management") then
table.insert( tab, Color( 0, 35, 175, 255 ) )
table.insert( tab, "[Management] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, ply:Nick( ply ) )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("leadmoderator") then
table.insert( tab, Color( 0, 74, 175, 255 ) )
table.insert( tab, "[Lead Moderator] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("moderator") then
table.insert( tab, Color( 0, 185, 35, 255 ) )
table.insert( tab, "[Moderator] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("premium") then
table.insert( tab, Color( 255, 0, 175, 255 ) )
table.insert( tab, "[Premium] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("trusted") then
table.insert( tab, Color( 255, 0, 175, 255 ) )
table.insert( tab, "[Trusted] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("supporter") then
table.insert( tab, Color( 255, 0, 175, 255 ) )
table.insert( tab, "[Supporter] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl )
table.insert( tab, ": " )
table.insert( tab, msg )
else
table.insert( tab, Color( 86, 185, 80, 255 ) )
table.insert( tab, "[User] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl )
table.insert( tab, ": " )
table.insert( tab, msg )
end
chat.AddText( unpack(tab) )
return true
end
end
hook.Add("OnPlayerChat", "InsertTags", Insert)
The result is that it is saying "admin" and in the correct color but it completely gets rid of the name, how would I fix this?
-
ply:Nick() for the player's name
-
Yes but where and how would I add it? And thank you very much for responding =D
-
I changes all the if/elses to look like this:
elseif pl:IsUserGroup("admin") then
table.insert( tab, Color( 0, 255, 0, 255 ) )
table.insert( tab, "[Administrator] " )
table.insert( tab, ply:Nick() )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl )
table.insert( tab, ": " )
table.insert( tab, msg )
But that didnt work either, what is wrong?
-
When speaking outright about code, we often use 'ply' as the 'player object'.
So when Megiddo stated ply:Nick(), you would actually need to use the variable of YOUR player object in your code, which is 'pl', not 'ply'.
So in your case, you'd need 'pl:Nick()'
Remove the original edit you did where you added ply:Nick(), and
I'm pretty sure you would not need your "table.insert( tab, pl )" parts, and instead replace those with table.insert( tab, pl:Nick() )
I think that would get you on your way, may have to adjust for insertion order after that.
-
Thank you! You deserve a medal =3
-
How do I make it so that the names are not the color of the tag though?
-
Research/figure out if there's a way to turn color off/default, and set that command after the tag.
-
Insert the color you want before the player's name.
-
Well I don't want it to be a certain color, I just want it to be the default. So how would I add these tags in their specific colors, without affecting the colors of the names?
-
Default color is approximately Color(150,200,255)
-
This is for a Trouble in Terrorist Town server so the color of the name changes if you are innocent or detective in the chat though. So I don't know if there is any way to set it to be like that.
-
It's could be their team color (http://wiki.garrysmod.com/page/Libraries/team/GetColor).
-
But they change teams, like so if trusted is purple, and if they are detective, the code clashes. This is trying to make it purple and the other is trying to make him blue (detective). But in another round he could be traitor, so his name would be green, but the code would again clash b/c it would try to make his name purple.
-
Ok, just to confuse you, here's how I would do it.
ULib has a command that I'd hoped Garry would just create, but apparently never did.
We wrote a 'GetUserGroup' (http://ulyssesmod.net/docs/files/lua/ulib/shared/sh_ucl-lua.html#Player:GetUserGroup) command IN ULib. Simply enough, it returns the group name the user is a part of. (not including inherits, just the top group)
Using that, I got rid of all your if/else IsUserGroup statements.
Now, the code below probably won't work as is.
See if you can figure it out though.
First I set a table of 'tags' by group/color/title.
Then, guessing that your gamemode uses teams for each job, I set a group of 'job' team colors. That is where you'd have to look in the original game mode to see what colors for each team they used.
ProTip, learn to concatenate in Lua. Not having so many table inserts will save you lua code time. Even if you end up going back to your if/then/elseif tree (because you want your code to run on NON ULib servers too), you can still save some table.inserts by using
table.insert( tab, jobcolor .. pl:Name() .. ": " .. msg )
rather than
table.insert( tab, msg )
table.insert( tab, ": ")
table.insert( tab, pl:Nick() )
table.insert( tab, jobcolor )
Again, below is example, untested. 98% guaranteed not to work out of the box.
if (SERVER) then
AddCSLuaFile("autorun/tags.lua")
end
-- Table of TAG colors and name per group
taggroup_color = { }
taggroup_color.headmanagement = { Color( 255, 0, 0, 255 ) .. "[Head Management] " }
taggroup_color.superadmin = { Color( 0, 255, 0, 255 ) .. "[Head Management] " }
taggroup_color.admin = { Color( 0, 255, 0, 255 ) .. "[Administrator] " }
taggroup_color.leadmanagement = { Color( 0, 255, 0, 255 ) .. "[Lead Management] " }
-- and so on .
-- Table of JOB (team??) colors, as specified somewhere in your gamemode already.
tagjob_color = { }
tagjob_color.1 = { Color( #whatever, #color, #here ) }
tagjob_color.2 = { Color( #whatever, #color, #here ) }
tagjob_color.someotherjobteam# = { Color( #whatever, #color, #here ) }
-- and so on for each job(team?) color as used in your gamemode.
if (CLIENT) then
function TagInsert( pl, msg )
local tab = {}
local group = pl:GetUserGroup() -- THIS IS A ULib ONLY command. If you don't want the code ULib required, you must then use your 'if/then' for every group test tree as your originally had
local job = pl:GetTeam()
local grouptag = taggroup_color[group]
local jobcolor = tagjob_color[job]
--error checks
if grouptag == nil then grouptag = Color( 255, 0, 0, 255 ) .. "[Unknown] " end
if jobcolor =- nil then jobcolor = Color( #some, #generic, #color ) end
table.insert( tab, grouptag )
table.insert( tab, jobcolor .. pl:Nick() .. ": " .. msg )
chat.AddText( unpack(tab) )
return true
end
end
hook.Add("OnPlayerChat", "InsertTags", TagInsert)
-
Quick question about the else/if part though.
if (SERVER) then
AddCSLuaFile("autorun/tags.lua")
end
if (CLIENT) then
function Insert( pl, msg )
local tab = {}
if pl:IsUserGroup("headmanagement") then
table.insert( tab, Color( 255, 0, 0, 255 ) )
table.insert( tab, "[Head Management] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("leadmanagement") then
table.insert( tab, Color( 255, 0, 0, 255 ) )
table.insert( tab, "[Lead Management] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("management") then
table.insert( tab, Color( 255, 0, 0, 255 ) )
table.insert( tab, "[Management] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("leadmoderator") then
table.insert( tab, Color( 33, 110, 252, 255 ) )
table.insert( tab, "[Lead Moderator] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("moderator") then
table.insert( tab, Color( 33, 110, 252, 255 ) )
table.insert( tab, "[Moderator] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("premium") then
table.insert( tab, Color( 245, 226, 20, 255 ) )
table.insert( tab, "[Premium] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("trusted") then
table.insert( tab, Color( 194, 19, 153, 255 ) )
table.insert( tab, "[Trusted] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("supporter") then
table.insert( tab, Color( 86, 185, 80, 255 ) )
table.insert( tab, "[Supporter] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
else
table.insert( tab, Color( 86, 185, 80, 255 ) )
table.insert( tab, "[User] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
end
chat.AddText( unpack(tab) )
return true
end
end
hook.Add("OnPlayerChat", "InsertTags", Insert)
I'm getting the error: /tags.lua.lua:0
[ERROR] lua/autorun/tags.lua.lua:8: unfinished string near '"headmanagement) then' 1. unknown - lua/autorun/tags.
What is this?
-
Try this...
btw.. do you use something like Notepad++ to edit your lua? Your tabulation is atrocious. lol
if (SERVER) then
AddCSLuaFile("autorun/tags.lua")
end
if (CLIENT) then
function Insert( pl, msg )
local tab = {}
if pl:IsUserGroup("headmanagement") then
table.insert( tab, Color( 255, 0, 0, 255 ) )
table.insert( tab, "[Head Management] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("leadmanagement") then
table.insert( tab, Color( 255, 0, 0, 255 ) )
table.insert( tab, "[Lead Management] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("management") then
table.insert( tab, Color( 255, 0, 0, 255 ) )
table.insert( tab, "[Management] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("leadmoderator") then
table.insert( tab, Color( 33, 110, 252, 255 ) )
table.insert( tab, "[Lead Moderator] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("moderator") then
table.insert( tab, Color( 33, 110, 252, 255 ) )
table.insert( tab, "[Moderator] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("premium") then
table.insert( tab, Color( 245, 226, 20, 255 ) )
table.insert( tab, "[Premium] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("trusted") then
table.insert( tab, Color( 194, 19, 153, 255 ) )
table.insert( tab, "[Trusted] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
elseif pl:IsUserGroup("supporter") then
table.insert( tab, Color( 86, 185, 80, 255 ) )
table.insert( tab, "[Supporter] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
else
table.insert( tab, Color( 86, 185, 80, 255 ) )
table.insert( tab, "[User] " )
table.insert( tab, Color( 255, 255, 255, 255 ) )
table.insert( tab, pl:Nick() )
table.insert( tab, ": " )
table.insert( tab, msg )
end
chat.AddText( unpack(tab) )
return true
end
hook.Add("OnPlayerChat", "InsertTags", Insert)
end
-
I do use notepad++ =( I thought notepad++ was of the highest quality, what should I use?
-
I do use notepad++ =( I thought notepad++ was of the highest quality, what should I use?
Notepad++ is fine. Just keep your lines together.
Also, in case you missed what MrPresident did, from your error, your missing the closing quotation mark.
'"headmanagement) then' should have been '"headmanagement") then'
Also, why use so many table.inserts?
Table manipulation is time consuming (for any programming language, even Lua).
It would be better, optimization wise, to use this ...
elseif pl:IsUserGroup("management") then
table.insert( tab, Color( 255, 0, 0, 255 ) .. "[Management] " .. Color( 255, 255, 255, 255 ) .. pl:Nick() .. ": " .. msg )
elseif ...
than what you're doing currently.
Look up "lua concatenation".
Those ".." bring together many string items.
-
Also, why use so many table.inserts?
Table manipulation is time consuming (for any programming language, even Lua).
It would be better, optimization wise, to use this ...
elseif pl:IsUserGroup("management") then
table.insert( tab, Color( 255, 0, 0, 255 ) .. "[Management] " .. Color( 255, 255, 255, 255 ) .. pl:Nick() .. ": " .. msg )
elseif ...
than what you're doing currently.
Look up "lua concatenation".
Those ".." bring together many string items.
It's necessary to do it the way he currently is. The color objects are not strings, and chat.AddText wants a list of string or color objects.
-
Sorry I'm very new to this. I've been going through the tutorials but im not done yet! :) Also, how do I get it to say |TEAM| when they talk in team chat and |DEAD| When dead players are talking? Would this work?
if ( pl:Alive == false ) then
table.insert( tab, Color( 255, 30, 40 ) )
table.insert( tab, "|DEAD| " )
end
if ( bTeamOnly ) then
table.insert( tab, Color( 30, 160, 40 ) )
table.insert( tab, "|TEAM|" )
end