Let's Make A Multiplayer Game #10: Error Messages
In this tutorial, we will show error messages when a client tries to connect to an invalid room or the game of the room already started.
In the JoinDialog
script, create a new function called “show_error” with a parameter that will contain the text of the error. Inside the function, put the error message in the label and show it.
Open the Client
script and create a function with the same name and the same parameter. Add remote
, since we are going to call this function from the server. Call the show_error
function of the JoinDialog
, passing the message as argument.
Now, open the server project and create 2 constants with the error messages:
- one in case of connecting to a room that does not exist,
- and the other for connecting to a room where the game started.
Go to the join_room
function. Let’s add some conditionals. If there is no room with the id specified by the client, call the show_error
function, passing the corresponding error message. Disconnect the player with the disconnect_peer
function.
If the state of the room is STARTED
, it means the game already started, call the show_error
function and pass the error message. Like before, disconnect the player.
If none of these 2 conditions are true, execute the _add_player_to_room
function to add the player to the room.
Let’s try it:
It works. But we have a problem, the join dialog is closed and we can’t see the message. We have to find a way of not hiding it.
We can do it adding a variable in the Menu
script. In the show_error
function of the Client
script, change the keep_join_dialog_open
variable of the Menu
scrip to true
.
But we have not declared this variable yet. So, go to the menu script and declare it. Initialize it to false
, since we want to close the dialog by default.
The function that closes the join dialog is the remove_all_players
function. Scroll down until you find it and add an if before hiding the join dialog. If keep_join_dialog_open
is true
, change the value of the variable to false
, so the next time the popup will be closed. Add an else, so the join dialog is hided only if keep_join_dialog_open
is false
.
So, if keep_join_dialog_open
is true
, the join dialog will not be closed, because we are not calling his hide
function. If keep_join_dialog_open
is false
, we call his hide
function like before.
It works now. When the player tries to join a room that does not exist, a message appears.
Make the message beautiful
Let’s make some improvements to the message.
Since it’s an error message, change his color to red using the modulate
property of the label. To avoid the text overflow, let’s add another font, one that is smaller.
The error message looks better now.
Other error
Let’s try to reproduce the other error, the one that shows up when a player wants to join a game that already started. I opened 2 clients. If I start a game in the room 0 with the first client and I try to join this room with the second client, the error message will appear.
That’s all. I hope you liked the series and learned as much as I did.