Join Michael House for an in-depth discussion in this video Simple chat clients, part of Advanced Unity 3D Game Programming.
- View Offline
In this video, I'm going to show you how you can create a Chat client. Chat clients, typically a subcomponent of many multi player games, are great examples for showing how to manage connected users, and share data. We want the Chat client to accept users that connect to it, and send messages they type to everyone else who is connected. Let's look at some code. The chatter component is going to take care of all the chat client needs. It's going to require a NetworkView because we're going to be using RPC calls. It's local variables set information about display message timeout, it's representation on screen, and the current user name.
Additionally it's going to contain a usersByID list, which will be a look up table for NetworkPlayers to their names. Much of the GUI functionality from a chat window comes from our message window class which we learned about in a previous video. After the client is connected, the first thing they'll do is set their name. If we are the server, we're going to directly join that user. Otherwise we're going to ask the server to join the user. And then send a request to the server to send us all the current users connected. The process input method is going to be called when the user hits Enter after entering a line of text.
It'll trigger an RPC call for the LogMessage method and send in our chat input. This RPC call will include the user who sent the message. In the LogMessage method. We're going to take a message and the sender. If we're the sender we're simply going to AddMessage to our screen. If someone else sent us a message, we're going to add their name, and said, plus the message to the screen. If we receive a message from an unknown user, we're going to ask the server to send us their current users, because something went wrong and our user list is out of date.
When the server is asked to send current users, it's gong to go through its user list. And for each user in the list, it's going to send an RCP call join user directly to the recipient who requested it, passing on the user name, and NetworkPlayer. When joining a new user, all users will add that user to the ID list and if it's a server, it's going to call JoinUser for all other connected clients and send a SystemMessage telling all connected clients that a user Joined chat. Likewise we'll have a command for removing user where all users will be notified when they leave chat.
The difference between system message and LogMessage is that system messages are generally sent from the Server, and will displayed differently. Let's take a look at this in Unity. I went ahead and started our master server, and went through the build process to start a new client. And I'm going to launch another client here in the Unity Editor, and start a Server. The first things it's going to do is ask for a name. I'll call myself Server. I'll switch to my other client and join the Server, and enter my name as Client.
Here we see it's just the messages that have been send saying that client has joined chat and when sending the message. On the Sender's end it will just print the message you typed and on the Receiver end it will say Client said hello. To better understand the communication process, when a Client Joins and when a message is sent, let's take a look at some slides. When a Client Joins a server it is going to connect to the Server and the first thing it is going to do is set it's name. When setting it's name it's going to send a JoinUser Cmd to the Server. The Server will then add that user to it's local list and send a message to all other Clients saying that user has joined.
This includes the Client who's just joined. After setting the name it's going to SendCurrentUsers request to the Server. This SendCurrentUser's request. Is going to iterate through all the users it has in it's list, and send a JoinUser RPC call to the Client that requested it. This will add all connected users to the local client's user list. When a Client is sending a message, it simply sends a LogMessage Cmd to all connected users. This includes a Server and All Clients. In this video, I've shown you how to create a Check Client.
Check Clients are very common in multi-player games. This Check Client shows you how you might manage users joining to the game, and let other players know about those users joining. It also shows you how you can send information to everyone pure to pure style like when we process input to send message. Or we can send information to the Server and have the Server distribute it, Client Server style. Like when a new player joins. The Chat Client recreated fulfils the basic requirements. But, there are some features that are missing to be a full featured Chat Client. For example, I suggest you add a Tell feature to allow the Client to send messages to specific users.
NOTE: This course requires Unity 4.5.5. The newer versions of Unity have done away with the GUI system used in this course, so the interfaces included for many of the scenes will not work with 4.6 and higher. You can download Unity 4.5.5 at http://unity3d.com/get-unity/download/archive.
- Enabling/disabling with scripts
- Translating, rotating, and scaling objects with scripts
- Working with mouse and keyboard input
- Creating custom GUI controls like progress bars
- Loading assets with scripts
- Saving games
- Creating prefabs dynamically
- Making remote procedure calls
- Synchronizing object transforms
- Finding slow code
- Optimizing data access
- Extending the editor