Multiplayer Network Games - Broadcasting Messages to All
-
Introduction
In traditional MIT Scratch, we can broadcast a message that all sprites running on the stage can receive and handle. For multiplayer network games, this capability is extended so you can broadcast a message to all players’ computers.
The message will be sent to the game server first, which then relays the message to all other players. Therefore, there might be some delay before all players receive that message.
Broadcast Block
To broadcast the message, you can use the following block in the multiplayer category:
It accepts these inputs:-
Message Type: you can define any message type using the “when I receive” block, then choose that message type in this dropdown.
-
Parameter: you can optionally attach a parameter to this message
-
Receiver Scope: you can choose between “All Sprites” or “Original Sprites”. If “All Sprites” is selected, all sprites on all players’ playgrounds will be able to receive this message using the “when I receive” block. If “Original Sprites”, then only the original sprites added to the game will be able to receive this message, and the copies made by the game server on other computers/tabs will not receive this message, neither would the local sprites. For example, if a message is about giving a reward to a sprite, then it should only be handled once by the original sprite (not the copies).
This block is an easy way to do almost anything synchronously across all the player computers. For example, to make a sprite change its costume, you can broadcast a message to all players, and then change the sprite’s costume when the message is received by each player’s computer.The only exception is that you should not change the speed or position of sprites using this method, since the game server takes full control of the motion and position of every sprite added to the game network.
Demo 1 - Broadcast to All Sprites
In this demo project, we repeatedly broadcast a message to all sprites, with the current timer value as the parameter. And for every sprite, when it receives this message, it will “say” it on the stage.
https://play.creaticode.com/projects/6607f888b57a3da838c32b3f
To run it, follow these steps:- Create a game as the host on one computer/tab, and add the “BlueBall” sprite to the game network. Also, add a handler for the event of “greeting” that prints out the parameter briefly:
2. In another computer or an incognito tab, run the project as the guest, and add the obstacle sprite to the game network. Also, add a handler for the event of “greeting” that prints out the parameter briefly:
3. Switch back to the host player, and run the repeat loop to broadcast the “greeting” message to all sprites on all computers every 2 seconds:
As a result, all sprites on both computers, we will see both sprites saying the timer value every 2 seconds:
Demo 2 - Broadcast to Original Sprites Only
For the same project, let’s change the broadcast target to “Original Sprites”:
As a result, only the original sprites (ball on left and square on right) will receive this message: -