font ChatFont = "Arial#12"; string STR_Received = "#100"; string STR_Sent = "#100"; string STR_Input = "#100"; string STR_ChatLine01 = "#100"; string STR_ChatLine02 = "#100"; string STR_ChatLine03 = "#100"; string STR_ChatLine04 = "#100"; string STR_ChatLine05 = "#100"; text TXT_ChatWindow // The "Chatwindow" { strings = 5; string = STR_ChatLine01,STR_ChatLine02,STR_ChatLine03,STR_ChatLine04,STR_ChatLine05; pos_x = 10; pos_y = 10; font = ChatFont; } text TXT_ChatInput // The input line to type new messages { string = STR_Input; pos_x = 10; pos_y = 100; font = ChatFont; } function chat_init() // Call this function in your MAIN FUNCTION { TXT_ChatWindow.visible = on; TXT_ChatInput.visible = on; } /* As you can only send strings to the server as client, we must send any chat message to the serve first */ function SendMsgToServer() { // Prepare string str_cpy(STR_Sent, player_name); // First, we'll add the player name str_cat(STR_Sent, ": "); // then a : str_cat(STR_Sent, STR_Input); // and finally the message // Send string send_string(STR_Sent); // Now we send the string to the server wait(1); // Wait 1 frame for the message to be sent -- VERY IMPORTANT // Empty the strings to avoid duplication str_cpy(STR_Sent, ""); str_cpy(STR_Input, ""); } function InputText() { if(inkey_active){ return; } // If already active, RETURN! inkey(STR_Input); // Input focus on STR_Input if(result == 13) // If input is ended by ENTER { SendMsgToServer(); // Send message } } on_enter = InputText; // Bind Input function to enter key function ReceiveMessage() // Function when the client receives messages from the server { wait(1); // wait one frame -- IMPORTANT /* To avoid that a server that has been started as "-sv -cl" gets the message twice, use ifndef server; */ ifndef server; if(str_len(STR_Received) > 0) // If we received a valid chat message { // Reorder Chat str_cpy(STR_ChatLine01, STR_ChatLine02); // 2 > 1 str_cpy(STR_ChatLine02, STR_ChatLine03); // 3 > 2 str_cpy(STR_ChatLine03, STR_ChatLine04); // 4 > 3 str_cpy(STR_ChatLine04, STR_ChatLine05); // 5 > 4 str_cpy(STR_ChatLine05, STR_Received); // New message > 5 wait(1); // Wait one frame -- IMPORTANT, as always str_cpy(STR_Received,""); // Empty the received string } endif; // Close the ifndef ifdef server; wait(1); // To avoid an empty function. endif; } on_client = ReceiveMessage; // Bind ReceiveMessage function on the Client-Receive-Data event function ReceiveMessage_Server() // The same function as above, but for the server and slightly changed { wait(1); // wait 1 frame -- IMPORTANT if(str_len(STR_Sent)>0) // If we got a valid chat message { // Copy str_cpy(STR_Received,STR_Sent); // Order str_cpy(STR_ChatLine01, STR_ChatLine02); // 2 > 1 str_cpy(STR_ChatLine02, STR_ChatLine03); // 3 > 2 str_cpy(STR_ChatLine03, STR_ChatLine04); // 4 > 3 str_cpy(STR_ChatLine04, STR_ChatLine05); // 5 > 4 str_cpy(STR_ChatLine05, STR_Received); // New message > 5 // IMPORTANT -- DON'T empty the message yet // Send it to all clients first! send_string(STR_Received); wait(1); // Wait 1 frame for the message to be sent -- IMPORTANT // Empty str_cpy(STR_Received,""); str_cpy(STR_Sent,""); } } on_server = ReceiveMessage_Server; // Bind ReceiveMessage_Server to the Server-Receive-Event