Basic Chatbox

From Garry's Mod
Jump to: navigation, search


Why basic?

This chatbox type can be considered basic because it merely acts a reskin of the default chatbox and doesn't actually have much in the way of user interaction. Its easier for sure and is perfect for someone who is trying to just spice up their chatbox without having to code a whole lot.

Getting started

Setting up

I wont teach you it from scratch but at point I am assuming you have a DFrame for the background, DTextEntry for entering chat, and a RichText for storing the previous chats. I would recommend that you do not make these panels global but instead store their references in a global table to minimize confliction.

myChat = {}

myChat.dFrame = ...
myChat.dTextEntry = ...
myChat.dRichText = ...

Here is a basic run down of how this type of chat box works by Facepunch user PortalGod

  • Hide default chatbox in GM:StartChat, display vgui chatbox instead
  • Overwrite GM:OnPlayerChat and add any messages to vgui chatbox
  • Use GM:ChatTextChanged to change the vgui text entry (doesn't even have to be a text entry)

Hiding the default

Garry's Mod has a handy function that makes it easy to hide the default chat box, you just return true in this function. This is one function you should override

function GAMEMODE:StartChat()
    return true

You also want to hide the default chat

hook.Add("HUDShouldDraw", "noMoreDefault", function( name )
	if name == "CHudChat" then
		return false

Adding messages

Now we are going to hook onto OnPlayerChat, despite what PortalGod's instructions were, and update our chatbox

hook.Add("OnPlayerChat", "myChat", function( player, strText, bTeamOnly, bPlayerIsDead )
	local col = GAMEMODE:GetTeamColor( player) -- Get the player's team color
	myChat.dRichText:InsertColorChange( col.r, col.g, col.b, 255 ) -- Make their name that color
	myChat.dRichText:AppendText( obj:Nick() )

	myChat.dRichText:InsertColorChange( 255, 255, 255, 255 )
	myChat.dRichText:AppendText( ": "..strText.."\n" )

Lastly we hook ChatTextChanged to update our DTextEntry since technically the user won't have it focused because this chatbox has limited interaction

hook.Add("ChatTextChanged", "myChat_Update", function( text )
	myChat.dTextEntry:SetText( text )



That about covers how the basic reskin of the default chatbox works. You dont actually allow the player to interact with the chatbox but give the illusion of doing so.

Advanced Chatboxes

Advanced Chatbox

Personal tools