Serverside chat.AddText

Hey,

I’m sure some of you who have already worked with the function chat.AddText got a bit annoyed over the fact that you need to write your own usermessage system to be able to use it on clients. Perhaps someone has already written something like this for himself, but I’ve written a serverside chat.AddText function. Let me explain how it works.

Usage

1. First, put this script somewhere in your addon or include it. Make sure it’s also sent to the clients.
[lua]/-------------------------------------------------------------------------------------------------------------------------
chat.AddText([ Player ply,] Colour colour, string text, Colour colour, string text, … )
Returns: nil
In Object: None
Part of Library: chat
Available On: Server
-------------------------------------------------------------------------------------------------------------------------
/

if SERVER then
chat = { }
function chat.AddText( … )
if ( type( arg[1] ) == “Player” ) then ply = arg[1] end

	umsg.Start( "AddText", ply )
		umsg.Short( #arg )
		for _, v in pairs( arg ) do
			if ( type( v ) == "string" ) then
				umsg.String( v )
			elseif ( type ( v ) == "table" ) then
				umsg.Short( v.r )
				umsg.Short( v.g )
				umsg.Short( v.b )
				umsg.Short( v.a )
			end
		end
	umsg.End( )
end

else
usermessage.Hook( “AddText”, function( um )
local argc = um:ReadShort( )
local args = { }
for i = 1, argc / 2, 1 do
table.insert( args, Color( um:ReadShort( ), um:ReadShort( ), um:ReadShort( ), um:ReadShort( ) ) )
table.insert( args, um:ReadString( ) )
end

	chat.AddText( unpack( args ) )
end )

end[/lua]
2. Now, to use it. It works pretty much the same as the clientside chat.AddText, but there’s one exception of course. The first argument is the player who should see the message. If you want to display the message to everyone, simply omit this parameter. Here’s the full prototype:
[lua]chat.AddText([ Player ply,] Colour colour, string text, Colour colour, string text, … )[/lua]

Example

Here’s a little example, ran from the console:

Conclusion

Tell me what you think of it and I’d love to know whether this is used in any mods!

http://sa.tweek.us/emots/images/biggrin.gif

Interesting. I haven’t gotten around to using chat.AddText, but this seems to be quite convenient and efficient.

What’s wrong with omitting? You don’t have to remember what’s the nil for.

If you omit, it’ll think the first color you pass is the player it needs to be sent to.

Why don’t you check if the first argument is a player or a normal table.

This is very nice good work

The player argument is now completely optional.

Yes, I know. From your post I assumed you didn’t add omit checks intentionally. That is why I asked what’s wrong with it.

Made something myself already, but would be cool if you release it! :slight_smile:

Release what? the code is in the first post.

I did something like this awhile back when I was making my custom chatbox for my server.

This is a lot nicer than what I had though.

Huh what’s wrong with

BroadcastLua( "chat.AddText( Entity(1), “said your mom is a cunt”) " );

:ughh:

Possible Lua injection maybe?

Doesn’t that broadcast to every client though? I guess that would work unless you wanted to send something to one client.

It’s a lot longer.

Colours are no longer 3 bytes, instead up to 9. Then you’ve got all those commas, spaces, quotes, etc.

chat.AddText really is intended for client side usage, if you want to use it for something other than chat, you should be firing a custom umsg for it, and hooking that and dealing with it client side.

This could be usefull to some people… but yer it takes < 5 seconds to write a simple usermessage to cover it…

Problem is, I’m using it in my admin mod and different plugins can use completely different messages. Here’s an example:

This is just to cover all kinds of messages you could ever want to send.

Because this is a nicer way to do it.

According to Garry, chat.AddText ignores alpha values. You could save some overhead and only transmit the RGB part of the colour.

Other than that, it’s a nice bit of code. :buddy: