Multiplayer Network Games - Creating and Joining Games
-
Introduction
In multiplayer games, multiple users can play the same game together over the network. All players run the same project on their own computers. One player is the host who creates a new game, and the other players are the guests who join the game.A game server is provided by the CreatiCode platform, which helps to keep track of the games and the players in each game. The game server will also help relay messages between the players over the computer network.
Multiplayer game programming is a very complex task. However, it has been greatly simplified by CreatiCode, so you can build most types of multiplayer games using a dozen blocks.
Create a New Game as the Host
To create a new game as the host, you can use the following block in the “multiplayer” extension:
It accepts the following inputs:-
Game Name: the host player needs to specify a unique name for the game, so that other players can easily find the game to join from a list of games
-
Password: the host player can specify a password, which defaults to “123”, and other players will be required to provide this password to join this game. This allows the host to make sure only players he knows can join the game he creates.
-
Display Name: the host player can specify a name for themselves that will be displayed in the game.
-
Role: the host player can also specify a role for themselves. The role can be freely defined by the game creator, and its meaning should depend on the type of game. For example, for a 2-player chess game, the roles can be “black” and “white”. For a 2 vs 2 shooting game, the roles can be “red team” and “blue team”.
-
Server: the host needs to specify a server to be used to run the game. The host should pick a server that is closest so that messages can be transferred quickly between the player computers and the game server.
-
Capacity: the maximum number of players can play the game together. If this number is reached for a game, new players won’t be able to join that game.
-
Game World Width and Height: The host can specify the width and height of the game world, and player sprites will not move outside this boundary. By default we can use the stage’s width and height, but if a game has a larger map, you can specify it here.
Note that each host user can only host one game at any given time for a given project. So, if a user tries to create a second game using the same project, the first game will be destroyed.Also, if the game has been idle for at least 180 seconds (no messages sent from any player in the game), then it will be destroyed to release the server resource.
List All Games
To find out all the existing games created by the current project, we can use the following block:
The first input is the server specified when the game was created.
The second input is the name of a table, which will store the information on the games currently running.
For example, suppose 2 games have been created by 2 host players using the same project, you should see 2 games listed in the table like this:
Each row represents one game, including the display name and game name specified when the host creates the game, and also how many players are currently in the game (including the host).
Join a Game
To join a game created by another host player, we can use the following block:
It takes the following inputs:- Game Name: The exact name of the game specified by the host.
- Host Name: The display name specified by the host.
- Server: The server where the game is running.
- Password: The exact password of the game specified by the host.
- Display Name: The display name of this player
- Role: The role of this player.
Note that the game name and host name can both be read from the games table written by the “list multiplayer games” block above, so usually the program should allow the user to choose from a dropdown list instead of typing them manually.The password should be input by the user manually, and the host user should tell that to the user.
List Players In a Game
We can also find out which players are in a game, using the following block:
It takes the following inputs:- Game Name: The exact name of the game specified by the host.
- Host Name: The display name specified by the host.
- Server: The server where the game is running.
- Table: The table to store the list of players as a result.
For example, when 2 players are in a game, the result table may look like this:
Check If Connected to Game
After creating or joining a game, you can use the following boolean block to check if this computer/tab is properly connected to the game server:
If for any reason this game is no longer running at the game server, or if this user’s computer is no longer connected to the game server via the Internet, this block will become false.
Test Your Project with 2 Accounts
To test a multiplayer project, you will need at least 2 user accounts. Here are the detailed steps:
-
Log in to CreatiCode in your default browser tab. Create the multiplayer project, and share it. You can use the “share unlisted” option so that only you would see it, as only you have the URL of this project.
-
Click the 3 dots on the top right of the browser (Chrome browser), and open a new browser window in the “incognito” mode:
-
In the new browser window, log into CreatiCode using your second account.
-
In the new browser window, open the shared project.
After these steps, you can start testing the project. For example, one browser tab can be used as the host user, and the other as the guest user.
Demo Project
You can use the following project to test the 4 blocks explained above:
https://play.creaticode.com/projects/6604937325dc753d5b676424
Run the first stack of blocks in the first account, and then run the second stack of blocks in the second account. Observe the content of the tables for the games and players. -