Simple MMO (Online Game)
Last updated
Last updated
Multiplayer mode can make a game more replayable, but it was hard for creators to design an MMO system. You must build a matchmaking system, a broadcast system, a sync system, and so on.
It is noteworthy that making an MMO game in Scratch is extremely hard because of the cloud variable. For examples:
The cloud variable doesn't accept strings so you must encode and decode.
Normally, you need to share more than one project to build multiple servers.
To simplify the production of MMO games, Cocrea built this extension.
The following blocks help you build a matchmaking system.
Use this stack block to join or create a room with room ID and extra data.
There are three types of rooms.
Broadcast: A normal room for players to join, broadcast messages, and sync states without limitation.
Match room: A special broadcast room. You can set a max player count and lock a match room during a session.
Lobby: A lobby is a collection of match rooms. Players can join a lobby to load information of match rooms sharing the same room ID with the lobby.
The following stack block is similar. It will pause the script until the player has joined the room successfully so you can get the data in this room.
The room ID in this block is the name of the room. The extra data is the data according to the player set by the developer.
This event will be dispatched when the player just joined or created a room.
These reporter blocks will return some data about the room to which the player just is connected.
There are three room type parameters.
broadcast
The player is connected to a broadcast room.
match
The player is connected to a match room.
lobby
The player is connected to a lobby.
The room ID and the session ID are 9-digit codes. A room ID represents a room. A session ID represents a player in the room. The player can get the room ID and player's session ID after the player is connected to a room. For example, the following scripts add the room ID, room type, and session ID to an info list after the player is connected to a room.
Use the following reporter blocks to get players' data in the room.
⚠️ Caution: These blocks should be used only in broadcast rooms and match rooms.
This reporter block will get the online player count of a room.
This reporter block will get players' data in the room. The data will contain all players' session IDs, user IDs, and user names. For example, there are two players in the room. Each player's data is separated by a semicolon. IDs and the user name are separated by a comma.
Normally, there will be several match rooms in the lobby. A player can get some basic data from each match room in the lobby.
This event will be dispatched when a match room changes in the lobby.
Use this reporter block to get the match room count in the lobby.
Use the following reporter block to get some basic data of a match room from the lobby.
The match room numbers start from 0. The player can get the following data.
room ID
The room ID of the room.
room type
The room type of the room.
clients
The player count in the room.
max clients
The maximum number of players the room can hold. If there is no limitation of the player count, the value will be 'infinity'.
created at
The time when the room was created. For example: Thu Jun 16 2022 14:55:31 GMT+0800
metadata
A JSON file contains the locked data and gid of the room.
locked?
Whether the room locked? If the room is locked, the value will be true. Otherwise, the value will be false.
Use the following stack block to fill the data of rooms in the lobby into lists. To use this block, you must make a list at first.
For example, when rooms in the lobby changed, the following script will update the value in the lists.
when there are two rooms in the lobby, the result could be like this.
Use the following stack blocks to join or create a match room in a lobby.
The IDs in these blocks should be the same as the ID of the lobby.
You can set the max player count in this block. When the player count in the room reached this value, the room will be locked.
Use the following stack blocks to join a particular room in the lobby.
The first parameter should be the room ID.
Use the following stack block to lock or unlock a room.
If a room was locked, the player is not able to connect to it. You can lock a match room during a session.
Use the following block to disconnect a room.
This event will be dispatched when the player is disconnected abnormally because of network failure and so on.
Use this stack block to reconnect to the room.
Normally, MMO game developers can't test the game by themself because one user could get only one ID in a game.
Use the following stack block to enable multi-IDs for one player.
With multi-IDs, developers could create several characters in an MMO game by themselves.
The following blocks help you build a broadcast system to broadcast and receive messages.
Use the following block to broadcast a message.
The first parameter is the type of the message. The second parameter is the content of the message.
This event will be dispatched when the player received a message from others.
Use the following block to get the latest message. You can choose to keep or delete it.
Use the following reporter to get the data of the message the player just got.
The player can get the following data.
type
The type of the message.
sender's Session ID
The session ID of the sender.
sender's name
The user name of the sender.
content
The content of the message.
For example, when the player receives a message, the following script will add the data of the message into the message list.
The result will be like this.
The following blocks help you to build a system to sync all players' states in the game.
Use the following stack blocks to set the data of the player.
The player can set the following data.
Name
The user name of the last state-changed player.
X
An undefined parameter can be set to any value. It's good to be set to the x position of the player.
Y
An undefined parameter can be set to any value. It's good to be set to the y position of the player.
Scale
An undefined parameter can be set to any value. It's good to be set to the scale of the player.
Direction
An undefined parameter can be set to any value. It's good to be set to the direction of the player.
Data
An undefined parameter can be set to any value.
Use the following reporter block to get the data of a player with the session ID.
Use the following reporter block to get the data of a player with the number of a player in the room.
the Player can get the following data.
Session ID
The session ID of the last state-changed player.
Name
The user name of the last state-changed player.
X
An undefined parameter can be set to any value. It's good to be set to the x position of the player.
Y
An undefined parameter can be set to any value. It's good to be set to the y position of the player.
Scale
An undefined parameter can be set to any value. It's good to be set to the scale of the player.
Direction
An undefined parameter can be set to any value. It's good to be set to the direction of the player.
Cocrea User ID
The user ID of the last state-changed player.
connected?
Whether the last state-changed player connected? If the player is connected, the value will be true. Otherwise, the value will be false.
Data
An undefined parameter can be set to any value.
In MMO games, we recommend you create clones to represent each player.
Use the following block to create a clone of a player with a session ID.
Some Scratchers are used to create clones with clone ID by using a variable for a sprite only.
This stack block will create a clone of the sprite with the session ID as a clone ID.
Use the following reporter block to get the data from a clone.
The types of data are the same as the types of data you can get from a player.
Use the following boolean block to determine whether the clone's session ID is the value in the block.
Use the following boolean block to determine whether the clone represents yourself.
Use the following boolean block to determine whether the sprite is a clone.
This event will be dispatched when a player's state changed.
Use the following reporter block to get the data of the last state-changed player.
This event will be dispatched when a new player is connected to the room.
Use the following reporter block to get the data of the last connected player.
This event will be dispatched when a player is disconnected.
Use the following reporter block to get the data of the last disconnected player.
In a typical PVP multiplayer game, players should be assigned to different rooms when they joined the game.
Use the following script to build a simple matchmaking system.
You can broadcast 'join a room' when the player runs the game. Then, if there is no unlocked match room, the player will create a new match room with 'lobby id'. The match room will only accept 4 players. If there are unlocked match room with 'lobby id', the player will be connected to an unlocked room. You can define a variable to save the player number.
A match room should be locked during a session no matter whether there are 4 players.
Coming soon