Advanced Chatbox
Benefits over the Basic Chatbox
Making a chat box using this method is slightly harder than using the Basic Chatbox and requires a little more attention to detail but it gives you full control over the chatbox as opposed to just simply reskinning it. You can add buttons, image panels, and more by basically coding the chatbox from the bottom up.
Getting started
Setting up
Same set up as the basic chatbox... DFrame, DTextEntry, RichText and a table.
Here is a basic run down of how this type of chat box works by Facepunch user PortalGod.
- Disable "messagemode" in GM:PlayerBindPress, open vgui chatbox and focus its text entry instead.
- In
myChat.dTextEntry:OnEnter
(or OnKeyCodeTyped and check for enter),RunConsoleCommand( "say", myChat.dTextEntry:GetText() )
and hide the chatbox. - Hijack chat.AddText and parse the arguments yourself (see RichText), call the original chat.AddText inside it though for colored console text.
Adding hooks
The first thing you need to do is override function called when the player's chat binds are pressed, you do this in a PlayerBindPress hook. This will prevent the default chat box from appearing when the player presses their chat keys. In this function is where you would open up your chat box and I would highly recommend you add a boolean argument to it for team chat. It would be bad if your players tried to talk to their team and it went global.
Now you want to display messages for stuff like sv_cheats being changed or a player leaving the game, that kind of stuff is important.
Lastly you should also hide the default chat messages since you will add your own to your chat box
chat.AddText
If you do not override chat.AddText, all the calls to that function will appear in the default chatbox which you have hidden. We want the player to see this so we will override the original function with one of our own. The source code for this function is not available, it likely isn't in Lua, so we will save the old function and just call that. You might notice that the arguments are ellipses (...), this is a deprecated Lua functionality that gLua has called varargs that basically means you can pass pretty much any number of arguments to that function.
Restoring functionality
Now that we have overridden the default chat box with one of our own, the hooks that are called by the default chat box won't be called anymore. We need to fix this.
In your chat box's text entry, you need to tell the player to say the text so the proper gamemode hooks are called for that. It would also be a good idea to make sure that the Escape key closes your chat box so we account for that too
When opening the chat box you are going to want to call the function for that even and then make sure your DTextEntry is available to be typed in
When the player closes your chat box you need to make sure they can move around again and that once again the proper gamemode function are called.
Finishing
Summary
Although this is quick and covers very little code wise, it should hopefully be a useful tool for anyone looking to code their own chatbox and add their own spin to it. It contains the basics of what is used to create a chatbox of this scale without any of the visual code, a backend of sorts. This doesn't do anything fancy by default like having embeddable images or any of that, but it is possible using this as a base
Examples
- Earu's Chatbox - https://github.com/Earu/EasyChat
- Exho's Chatbox - https://github.com/Exho1/eChat