What are cloud variables, KV database, and Simple MMO？
Most Scratch games are single-player. To make it possible to share data between all computers running a project, the Scratch team designed a feature called cloud variables. Scratchers can store number-containing variables in the cloud and build online multiplayer games with cloud variables.
Because of its mechanism, this feature has some limitations. Therefore, permissions are set for the feature:
Cloud data can not be used by New Scratchers. The Scratch Team does not want people new to Scratch misusing cloud variables, as it could put a large load on the system that it cannot handle, most likely leading to the feature not working. - Cloud Data wiki page
To solve issues with cloud variables, Gandi IDE built the KV database in Data Utils and Simple MMO.
In this article, these three features will be introduced. You can make your choice based on your situation.
There are four concepts that need to be considered in network transmission:
- Permission: Who can read or write the data?
- Load: How much data is transferred each time?
- Time limit: How often does the data need to be synchronized?
- Persistence: Does the data need to be stored for a long time?
Cloud variables are like public spaces. Everyone can read and write. Creators should build a permission system to avoid conflicts. For example, set up several cloud variables, where each one can only be written by one player.
Cloud variables don’t supports differential transmission. That means all data will be transferred to everyone when only a small part of it is changed.
The more frequently data is written, the larger the amount of data. Because of the forever loop, this could happen in Scratch projects easily.
The data of cloud variables will be persistent.
Cloud variables are maintained through a secure Websockets connection. To avoid overloading the cloud data infrastructure, cloud data updates are limited to a certain number per second when a project is being run. One should always avoid attempting to update a cloud variable in a fast loop that does not wait between updates. Generally, one should carefully consider how often a cloud variable is updated and try to limit any updates to only times when it is needed, such as when the value actually changes, and to limit how often the variable is updated on the server. If a variable is being updated too often, the cloud data server will drop the connection temporarily and updates will not be sent to the cloud data servers until the connection is automatically re-opened after a variable waiting period. There is a limit of ten (10) cloud variables per project. Cloud variables can contain only numbers (unlike regular variables, they cannot contain letters or symbols). A character limit of 256 digits per variable has also been implemented (formerly 128 digits). Hexadecimal numbers are no longer supported. - Cloud Data wiki page
KV (Key-value) database holds key-value pairs. For example, if a variable is named ‘a’, and the value of it is 123, it will be saved as a=123 in KV database. It is easy to read and use.
In K-V database, each project has a public space and private spaces. For example, if a key-value pair is saved in private spaces with the key ‘password’, each player can write and read their own value of ‘password’ but cannot write or read others’.
KV database supports differential transmission. If a user updates the value of a key, only this pair will be transferred.
KV database supports high-frequency writing and reading and the data is cached. In other words, when the data is not updated, you can read the data without network transmission.
The data of KV database will be persistent.
Creators can replace cloud variables with KV database for better performance in load and permission.
Simple MMO supports communication. The data can be high-speed synced between different players. It will be a better choice to make a game that needs real-time communication, such as PUBG or Among Us.
The mechanism of network transmission is totally different between cloud variables and KV database. The most obvious advantage of Simple MMO is speed.
The permission system in Simple MMO is very strict. Each player can only write their own states. In addition, players can broadcast messages to others. It can be used to build a chat system or dispatch events in local projects. It’s like the broadcast between sprites in Scratch.
Simple MMO supports differential transmission. Only changed parts will be transferred. It will greatly reduce the excess load in the transmission to support real-time communication.
Thanks to the low load, Simple MMO has the best real-time performance.
Simple MMO is not designed to record. When the player is disconnected, the data will be deleted.
Public and private
Private writing permission
Easy to use, hard to understand
1. Learn about network
1. Save players’ settings 2. Build a save & load system
1. Build real-time chat rooms 2. Make online multiplayer games
Only use for learning 1. Use KV database to store data 2. Use Simple MMO for real-time communication
Use to store players’ data and game settings 1. Use Simple MMO for real-time chat room and state syncing
Use for real-time communication and event communication 1. Use KV database to store scores and data
No.1 Adjust game settings without re-sharing the project
Some creators want to adjust the difficulty of a game based on feedback from players.
Creators want to adjust some settings in a game without updating and re-sharing the project.
In this situation, you can use two blocks in KV database.
For example, you want to adjust the difficulty. The project needs to identify you as the creator and run the script to adjust difficulty.
Step 1: Build a difficulty system controlled by parameters
In most games, there are parameters determining the difficulty, such as the amount of enemies, the speed of characters, and the damage of weapons.
Step 2: Identify the creator
You can use the username in Sensing to identify yourself. For example:
Step 3: Write parameters
You can define rules to write parameters in KV database. For example, you can press number keys to write parameters:
Step 4: Read parameters
Other players who run the project should read the latest values of parameters and set them to match local variables . For example:
No.2 Store the coin count
This scenario is similar to the first one. In this case, the count of coins can only be written and read by players themselves. You need to save data in private spaces.
You can make a block to change the count of coins. First, read the value of ‘coins’ from the private space. Then, change the value. Finally, write it back to the database. For example:
No.3 Create a real-time chat room or a real-time PvP game
[This part will be updated soon. Check it later.]
Is it impossible to build an online multiplayer game with cloud variables? The answer is absolutely not.
Some Scratchers have built well-functioning online multiplayer engines and games in Scratch based on cloud variables.
All these solutions have built-in encoding and decoding system. It is a challenge for developers to build an efficient one. These creators are brilliant.
Gandi IDE built Simple MMO and Data Utils to provide basic functionalities which are more suitable for developing online services and make doing so more achievable for new creators.
Thanks to the Scratch team. They build cloud variables that inspired a lot of creators to make online games.
Thanks to all creators and developers who helped Gandi IDE to build Data Utils and Simple MMO. Some of them are: Nick, yk1boy, pikachu, Oscar, Arkos, bob, Jamin.