Cloud Game Tutorial | Part 3 | Multiplayer
Key Takeaways
Create a multiplayer cloud game in Scratch 3, expanding the cloud engine to support 4 players, and learn about clones, custom blocks, and numeric vs. string comparisons
Full Transcript
hi I'm Griff fetch and welcome to part 3 of my series on creating multiplayer cloud games in scratch with the rather tricky part 2 out of the way we can now focus on getting more than one player in our multiplayer game if you missed out in part 1 or 2 then you'll want to go back and watch them as we're going to pick right up where we left off however I have shared the finished scripts from my part 2 on my growth patch shooter account and you can find a link under the video if it helps to feel free to remix that and then off we go okay let's just clean up our scripts from part 2 I don't need these test blocks but I'm going to remove the background costume the P 1 X and P 1 y cloud variables also no longer needed there we're ready to begin we were designing our scripts to allow for players to join our project and we'll use sprite cloning to do this as it avoids having to manually create 4 copies of all the costumes and scripts that we will be designing to prepare our project for this change let's rename the player variable to my player number hash here and serve number this variable is used to let the game know which player we are controlling now let's introduce some new variables we already have the p1 cloud variable but let's add clown variables P 2 3 & 4 remembering to take the cloud variable box then for this sprite only the new variable play a hash or player number each player clone will have its own player number and one more variables needed we're going to call this max players all in uppercase because this is for all sprites ok let's set max players to 4 this is the maximum number of players we want to be able to join the game right now we'll set this right after the green flag is clicked and in case you are wondering yes we can make this cloud engine work for up to 9 or 10 players if needed but right now let's stick to 4 I'm going to separate off the forever loop from the green flag stack but please don't delete it just leave it down here as we'll need it later right time to create our players clones start by setting player hash to 1 that's for our first player player 1 we want a repeat block to loop around for each player so we'll use the max available within the loop we create a clone of myself and then change player hash by one to progress on to the next player now a quick explanation here when scratch creates a clone an exact copy of the sprite being cloned is created in this case the scratch cat with its costume position and all its variables that will include the player hash variable which we just set to one so at this point both the original sprite and the clone we've created have a player hash of one but right after creating a clone we immediately changed the original sprites player hash variable by one before continuing around the repeat loop what this means is that the cloned sprite remains as player one but the original sprite now becomes player to the next clone will then be created player two and the original sprite will move on to become player three and the loop continues okay join the new script to the green flag stack now to allow us to see this working let's temporarily add a script that repositions the clones as they are created otherwise they would all go on top of each other and we want me to see them when I start as a clone go to random position if we click the green flag now we should see that the player's clones appear in random positions great I cept those who can count may notice that we have five players whose match I'm not complaining but I thought you said the maximum players were four yes do not fear we asked for four new clones but don't forget we already had one player sprite before we created those clones and four plus one is five so to remedy this we simply take away one from the number of clones we create like so and there now the green flag rewards us with four players excellent but hold on slow-motion mode activated did you see how the players do not appear at once oh no that's just not professional this is because scratch always assumes that loops are being used to create moving animations and so slows down the scripts to allow us to see that animation which is all very nice but not here please we can remedy this with the Handy run without screen refresh feature of custom blocks make a new custom block and name it set up players tick run without screen refresh and click OK move into the new custom block all the scripts that we don't want to show as an animation and then use the new set of players block in its place now when the green flag is clicked pal all four players appear at once much better that makes me happy cool I'm going to delete the random positioning scripts now as we've seen that the cloning works to begin the game loops on all four players sprites dragged in a broadcast block click to create a new message and we'll name it begin drag out a when I receive block and select begin now bring back those scripts we separated off earlier and join it back up to the new begin receiver this script will now begin running on each of the four players sprites however we still have a bit of work ahead because we only designed this script to control player 1 we can quickly replace my player equals 1 with my player equals player number remember my player is the number of the player I am controlling in the game and player number is a number of this sprite so these variables will only be equal when the current sprite is the player I am controlling next depending on this sprites player number we need to set the right cloud variable p1 p2 p3 or p4 start by creating a new custom block and name it set cloud hash add the input player another label 2 and an input named value tick run without screen refresh and ok we're going to do this the easy way for now so drag in an if/else block and compare the input player hash to the number 1 if it is the same then we set clown variable p1 to the input value now duplicate this up only change it to check for and set player 2 3 & 4 if we like we can remove the last if as there's no player five at this time nice now we can use this in place of the set p1 block like this just plug in the sprites player hash and encoded variables and dispose of the old set block hmm our scripts are getting a bit crowded let me just make a little space okay we still need to replace this begin decode of p1 variable to fetch the correct cloud variable for this player sprite make a new custom block a name it available equals cloud hash at an input named player and choose to run without screen refresh ok let's simply duplicate the set cloud block scripts as this is going to work similarly but we change all the set blocks to be set value and then get the value from the corresponding cloud variable for each player P one two three and four great we now make use of this new custom block drag it into just above the begin decode block here and we'll feed it with the player hash variable this will set value to the corresponding players cloud variable so replace the variable p1 here with the variable value I'm feeling really good about where we are right now just a final touch we'll update the say block to include each player's number as well as their name we can do this with a couple of joins like so all that remains is to add a way of choosing which player we're controlling when we play the game let's just continue to do it manually for now by pressing the number keys 1 2 4 so duplicate the when key pressed hat blocked for players 2 3 & 4 and while we're at it let's include a zero key to drop out of the game that's by setting my player to 0 and that my friends is it are you ready to see what we have created let's go fullscreen ahh looks like a scratcher I'm an or is it I'm an popular youtuber has been hit before me right I'm pressing the one key now and there I am with patch tutor has appeared and pressing number two and now I'm controlling cat number 2 you'll notice that each time I've changed to be a new player the previous player stops moving where I left it pressing three yep all looks good and four great now I can stop the project all the clones will be deleted clones do not exist once the project is stopped but if I click the green flag again our scripts recreate the clones read in the Cloud Data and reposition the players exactly where they left off neat but in reality when a player leaves a game you don't want them to hang around on the screen forever so how do you tell a player has left the game well you simply need to detect when they have stopped changing their players cloud variable to demonstrate this will make the player fade out when their cloud data stops changing add a new variable named last value for this sprite only we will use this to keep track of when the players cloud variable has changed place an if/else block here just after the value equals cloud number custom block and compare the variable named value with last value now we must be careful this really caught me out before I understood what was going wrong we must join value with a letter I'll use 30 a when a comparison is done on values made up of just digits scratch thinks it must be a number and attempts a numerical comparison but this can go wrong scratch can't accurately represent long numbers technically this is called a loss of precision the result of this is that scratch will say that the two numbers are the same even if they are slightly different this is not a problem when comparing long string values so adding a letter fixes the problem so when the current cloud value is the same as the previous value we change the ghost effect by one fading the player slowly away but when the cloud variable has changed we set the goat effect to zero bringing the player back into view we also set the last value variable to reflect the change remember to also join it with the letter A as before okay it's green flag time and look at this we see all four cats appear and then because no one is changing the cloud variables they slowly fade away but if I press one to take control of the first player then the change in the first place variables causes the goat effect to be removed and it pops into view so I am playing from the UK but my friend crystal keeper 7 has kindly connected from the US to help demonstrate this project in action and you can see as we switch players using a number keys the unused players are fading away and here we have three players at once this is working excellently well that's it for this video but I hope you're as excited as me as to where we will be going next with this series things are really coming together I'm especially looking forward to covering the subject of Auto game joining and ways of getting smoother movements from the other cloud players if you enjoyed watching this video please like the video and subscribe to my channel thanks for watching and I hope you will join us again soon for part 4 bye for now
Original Description
This is part 3 of how to create a multiplayer cloud game in Scratch 3. In this episode we expand the cloud engine to support 4 players at once. We also learn a little more about clones, custom blocks, run without screen refresh and numeric vs. string comparisons with loss of precision.
I have shared the scripts from part 2 here if you want to remix: https://scratch.mit.edu/projects/379298634/
Part 1 - The Basics - https://www.youtube.com/watch?v=-KoHaz7ZPvo
Part 2 - Encoding and Decoding - https://www.youtube.com/watch?v=nNv9iehpGRQ
Part 3 - Multiplayer - https://youtu.be/4acaa5y_jSU
🚀 *Boost Your Creativity with Griffpatch*
The Griffpatch Academy will take you from "Gamer to Game Creator"
Learn more at 👉 https://griffpatch.academy 👈
😺 Scratch was developed by the Lifelong Kindergarten Group at the MIT Media Lab. See http://scratch.mit.edu
In further videos we will look at smoothing things out, player interactions, lag, lobbies and game joining, etc.
Other multiplayer cloud games I have recreated in scratch are slither.io, agar.io, diep.io, Terraria, bomber man, and a few off my own including multiplayer platformer fun, and fireflies, and tanks.
Hi, I'm griffpatch from #Scratch (https://scratch.mit.edu/users/griffpatch/). I create and program games in the online Scratch coding website / language often used in schools to teach the basics of learning to code. In this #tutorial series I will be teaching the basics of how to create #multiplayer #games in Scratch using #cloud variables. In this episode we will create a small project that communicates the mouse position from one web browser to another. We also see the immediate limitations of how cloud variables which we will then learn how to overcome in subsequent videos.
Also in this video you get to see my Scratch port of slither.io which you can find here: https://scratch.mit.edu/projects/108566337/
Playlist
Uploads from griffpatch · griffpatch · 23 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
▶
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Burrow Blitz - A game by Griffpatch
griffpatch
Burrow Blitz Level 1 Speed Run!
griffpatch
Scratch Messaging Extension (for chrome & firefox)
griffpatch
Scrolling Platformer Tutorial | Part 1 | Get Scrolling
griffpatch
Scrolling Platformer Tutorial | Part 2 | Gravity
griffpatch
Scrolling Platformer Tutorial | Part 3 | Platform Detection
griffpatch
Scrolling Platformer Tutorial | Part 4 | Off Screen Sprites and Player Death
griffpatch
Scrolling Platformer Tutorial | Part 5 | Collectables
griffpatch
Scrolling Platformer Tutorial | Part 6 | Danger Sprites (Bug fix)
griffpatch
Scrolling Platformer Tutorial | Part 7 | Exit Portal
griffpatch
Scratch 3 | Box2d | Physics Extension
griffpatch
Scrolling Platformer Tutorial | Part 8 | Vertical Scrolling
griffpatch
Zombie Cube | Speedrun challenge
griffpatch
Scratch 3 | Physics Extension | Version 2
griffpatch
Top Down Scroller | E1 - Scrolling | Scratch Tutorial
griffpatch
Top Down Scroller | Part 2 | Enemy AI
griffpatch
Top Down Scroller | Part 3 | Enemy Clones & List
griffpatch
Top Down Scroller | Part 4 | Live Die Repeat (Game Over)
griffpatch
Top Down Scroller | Part 5 | You've Won!
griffpatch
Cloud Game Tutorial | Part 1 | The Basics
griffpatch
Cloud Game Tutorial | Part 2 | Encoding and Decoding
griffpatch
Scratch 3 Dev Tools Browser Extension by griffpatch
griffpatch
Cloud Game Tutorial | Part 3 | Multiplayer
griffpatch
Scrolling Platformer Tutorial | Part 9 | Momentum
griffpatch
Scrolling Platformer Tutorial | Part 10 | Wall Jumping
griffpatch
Cloud Game Tutorial | Part 4 | Auto Game Joining
griffpatch
Game Preview - "Getting Over It" with Griffpatch - A fan recreation in Scratch
griffpatch
Speed Run with Outtakes | Getting Over It | 4 minutes 30 seconds
griffpatch
Griffpatch's Live Stream
griffpatch
MMO Platformer Speedrun Challenge
griffpatch
Appel Platformer | Scratch Game Blog
griffpatch
Cloud Game Tutorial | Part 5 | Smooth Movement
griffpatch
How to Design a Level for Appel
griffpatch
Fastest Appel Speedruns | Jan 2021
griffpatch
Tile Scrolling Platformer (Mario) | 1. Setup
griffpatch
Tile Scrolling Platformer | 2. Grid List
griffpatch
Tile Scrolling Platformer | 3. Tile Collisions
griffpatch
Tile Scrolling Platformer | 4. Platforming Scripts
griffpatch
Tile Scrolling Platformer | 5. The Level Editor
griffpatch
Tile Scrolling Platformer | 6. Level Codes
griffpatch
Tile Scrolling Platformer | 7. Drop Through Platforms
griffpatch
How to make Physics in Scratch | Full Tutorial
griffpatch
Tile Scrolling Platformer | 8. Enemy Clones
griffpatch
Simple Pathfinding Tutorial
griffpatch
Tile Scrolling Platformer | 9. Enemies in Level Editor
griffpatch
Simple Maze Generation | Scratch Tutorial
griffpatch
Tile Scrolling Platformer | 10. Mystery Blocks
griffpatch
Simple Car Steering Simulation | Scratch Tutorial
griffpatch
Simple Background Scrolling Tutorial
griffpatch
Tile Scrolling Platformer | 11. Death, Background & Sound
griffpatch
Mid Week Update & Channel Membership
griffpatch
Simple Grid List Tutorial with Image Scanning
griffpatch
Tile Scrolling Platformer | 12. Auto Arranging Tiles
griffpatch
Code a Fun Space Shooter Game 🚀 | 1. Move & Shoot | Scratch Tutorial
griffpatch
Space Shooter 2 - Enemy Collisions
griffpatch
Tile Scrolling Platformer | 13. Level Progression & Editor Improvements
griffpatch
Space Shooter 3 - Camera Shake & FX
griffpatch
Tile Scrolling Platformer | 14. Sloping Tiles
griffpatch
Pathfinding a Maze using Lists
griffpatch
Tile Scrolling Platformer | 15. Crouch & Slide
griffpatch
More on: AI Productivity Tools
View skill →
🎓
Tutor Explanation
DeepCamp AI