Most RPG games include real-time chat systems. They are very useful, especially when several players decide to join their forces against a powerful enemy, and then use their chat boxes to devise an attack plan, for example.
This tutorial will show you the full source for a chat system that can be used with most MORPGs exactly as it is, or without editing too much code.
Let's start with the easy part; we've seen similar code in a previous tutorial.
We are defining a bunch of texts, which will be used to display the input text, the messages that will be utilized by the server and clients, and so on. Please note that I have set the text limit to 100 characters for this demo; since the code runs really fast, you can safely increase this limit by up to 500% without seeing any lag. However, you shouldn't forget that MORPGs must be optimized for speed; I'll make sure to discuss this matter in a future tutorial.
Here's the smart coding part for our chat system.
I know that the code may look a bit scary, but don't worry, it is commented thoroughly. First, we've got the server event function, which is triggered as soon as one of the clients has joined the chat. When this happens, player's name is copied to the messages_str string; it will be used to display the client name in the chat box, and can also be used to ban specific players who try to cheat, for example. This feature isn't implemented in my demo, but you can code it easily, by comparing the name of the logged-in player with the one that is stored inside messages_str.
The same function will also monitor client disconnects; they can happen on purpose (the player decides to leave) but can also be triggered because the client has a poor Internet connection. Finally, the server event function is also the one which receives the strings (chat messages) that are sent from one or more clients. Each message that's received is sent back to all the clients right away; since we aren't using a lot of data, the entire text is sent within a frame.
The input_text() function is run when one of the clients (players) pushes the "Enter" key on his/her keyboard. On a side note, "Enter" has an ASCII code value of 13; that's how we determine if a player has sent the chat message by pressing the "Enter" key. The text will be sent to the server at first, of course; as you may remember, clients aren't allowed to communicate with each other directly, because they will get out of sync quite fast.
The last function – main() – implements a few powerful tricks. It limits the frame rate to 60 fps, thus ensuring a smooth gameplay experience and making sure that the server and its clients can communicate properly. Then, it initializes the text input and server event functions. The rest of the code doesn't do anything special; it determines if the server is available or not, and then it runs a code section either as a server, or as a client.
As you can see, multiplayer chat isn't that hard; simple code snippets like the one posted above will do the job most of the time. The following tutorial will present a turn-based game, so stay tuned!
Need help with code for your MORPG? I can help!