In any online multiplayer game you probably will want your players to communicate, and especially in games like MMORPG’s, where communication is a huge part. I’ll now show an easy way to do an easy chat which is easy to implement into your project. Everything will go into one object and I will explain it event for event.
c_text = "";
ci = 0;
The above variables will be defined in create event. c_text should be used as an array, storing all of our messages. ci will be an index keeping track of where we are in the array. If you get errors because of the array not being initialized you can fix that by doing something like this: for (i=0; i<100; i+=1) c_text[i] = “”;
Next step is to receive text, which goes into step event:
if (mplay_message_receive(0)) {
if (mplay_message_id()==1) {
c_text[ci]+=mplay_message_value();
ci+=1;
}
}
Basically it says ”if we receive a message and it has an id of one then that is a chat-message so we’ll add it to the array”. The index of the array then goes up one such to prepare for the next received message. Anyway, all received messages will be added to array so it will contain the entire chat-history, up to 32 000 inputs before it ends due to the fact that Game Maker arrays can’t have more than 32 000 entries. Now let’s look at how to send messages to everyone else:
mplay_message_send(0,1,keyboard_string);
c_text[ci] += keyboard_string;
ci +=1;
keyboard_string = "";
We know that the other player’s clients will only add the message if it has an id of one, so that is what we use. Using only one row and the mplay_message_send function you can now make the info be added to all the other’s arrays. However, since you do not receive the message you just sent yourself you will need to add it to yourself manually and that is what we do on the two lines thereafter, before we finally reset keyboard_string which we have been using to take input. The whole code should go into whatever event you like, key-release for the enter-key for an example.
Now that everyone can send messages to each other and they are being received all we have to do is to draw them. This mostly a question of layout, I’ll just draw something as an example but you’ll have to customize it yourself. Notice that the object I used had a sprite which I used as background image for the chat. All of the following goes into draw.
draw_sprite(sprite_index,image_single,x,y);
draw_text(x+10,y+sprite_height-20,keyboard_string);
Draws the background of the chat, and then the input you write in the bottom, with 10 pixels marginal from the left border.
w = 0;
for (i=0; i<ci; i+=1) {
w += string_height_ext(c_text[abs(i)],-1,400);
}
Some messages will be too long and then we’ll have to switch row, therefore we can’t really know how high our text is. We find that out in order to know how many messages we can draw.
if (w>sprite_height-30) {
w=0;
for (i=ci; w<sprite_height-30; i-=1) {
w += string_height_ext(c_text[abs(i)],-1,400);
}
}
h = ci-i;
for (i=0; i<h; i+=1) {
draw_text_ext(x+10,y+sprite_height-30,c_text[ci-i],-1,400);
}
What it says is: if the height is larger than what it can be, do the for-loop. The for-loop says start at the last message and add one at a time to our string, w, until we cannot add any more. Then we go on, and draw the messages as a list.
Comment for support if you have any questions.
.
Users logged in:
Comments
Loading comments...