Garry's Mod Wiki

Revision Difference

Basic_Chatbox#513957

<cat>Dev.Lua</cat>⤶ # 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 <page>DFrame</page> for the background, <page>DTextEntry</page> for entering chat, and a <page>RichText</page> 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](http://facepunch.com/member.php?u=235325)⤶ ⤶ * `Hide default chatbox in <page>GM:StartChat</page>, display vgui chatbox instead`⤶ * `Overwrite <page>GM:OnPlayerChat</page> and add any messages to vgui chatbox`⤶ * `Use <page>GM:ChatTextChanged</page> 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⤶ end⤶ ```⤶ ⤶ ⤶ You also want to hide the default chat⤶ ⤶ ```⤶ hook.Add("HUDShouldDraw", "noMoreDefault", function( name )⤶ if name == "CHudChat" then⤶ return false⤶ end⤶ end)⤶ ```⤶ ⤶ ⤶ ## 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" )⤶ end)⤶ ```⤶ ⤶ ⤶ 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 )⤶ end⤶ ```⤶ ⤶ ⤶ # Finishing ⤶ ⤶ ## Summary ⤶ 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 ⤶ <page>Advanced Chatbox</page>⤶ ⤶ ⤶