Discuss Scratch

Gede_Booo
Scratcher
100+ posts

Cloud Multiplayer

I know how to code but there has always been one thing that i can kind of understand yet it almost never works for me. Cloud Multiplayer. Can somebody help me make a simple engine where each player has a X and a X and another number which is for cosmetic skins. Also environment things such as doors being open or closed and some other stuff. Now this might seem like im just too lazy to make something, it kinda is but still i cant make this work, please help me.
toadkid
Scratcher
17 posts

Cloud Multiplayer

Cloud can be tough, and I haven't worked with it in a while, but I'll try to help. Something important to note is that cloud variables only support numbers,, this means you can't have a-z or any special characters like ?.,~ etc. Another important note is that scratch can only transfer information from 1 cloud variable 10 times per second. Since cloud variables can be very very long, it's generally a good idea to try and get all of the data you need to transfer between players into 1 cloud variable, if you are looking for any level of smooth looking gameplay. I think for your first cloud project it's probably a good idea to stick with only 2 players at a time.

So, unless I'm misunderstanding, you want to store both players' X positions first of all. This is a good starting point, but there's a trick to it. Since cloud variables can only contain 1234567890, negative numbers are going to cause issues. In addition, when the other side is reading the data of the opposite player, the number of digits in the cloud variable need to stay the same, or it'll mess up the whole line of data. So here's my usual approach:

since X position can be anywhere from -240 to 240, first store it to a temporary variable and add +240 to it,
making it a range of 0 to 480, then check if the length of the variable is less than 2, and add a zero at the front if so, and repeat again if less than 3.

set [temp v] to ((x position) + (240))
if <(length of (temp)) < [2]> then
set [temp v] to (join [0] (temp))
end
if <(length of (temp)) < [3]> then
set [temp v] to (join [0] (temp))
end

so with this code block you'll have your x position as a 3 digit nonnegative number.
in this simple example, we're going for 2 players that communicate their x positions to the cloud. Since we can get the x position as 3 digits now, we just need a 6 digit long cloud variable, with 3 slots reserved for each players' x.

set [☁ data v] to [000000]

so to actually communicate, each player will have to be unique in some way. I usually make a normal variable called “id” for each player, which influences which slots of the cloud variable you take up, and which you read from to display the other player locally (can be done either with a clone or just making another sprite specifically to read cloud data and display the other player).

this script writes your 3 digit temp value to the cloud data while preserving the other player's half of the data:

set [id v] to [1]
forever
if <(id) = [1]> then
set [☁ data v] to (join (temp) (join (letter (4) of (☁ data)) (join (letter (5) of (☁ data)) (letter (6) of (☁ data)))))
end
if <(id) = [2]> then
set [☁ data v] to (join (join (letter (1) of (☁ data)) (join (letter (2) of (☁ data)) (letter (3) of (☁ data)))) (temp))
end
end

and then to read the data of the other player, in an otherwise blank sprite/clone, just read the correct letters from the data and subtract the 240 we added earlier:

forever
if <(id) = [1]> then
set x to ((join (letter (4) of (☁ data)) (join (letter (5) of (☁ data)) (letter (6) of (☁ data)))) - (240))
end
if <(id) = [2]> then
set x to ((join (letter (1) of (☁ data)) (join (letter (2) of (☁ data)) (letter (3) of (☁ data)))) - (240))
end
end

and unless I'm forgetting anything you should be able to move a character around horizontally with 1 friend now! I wrote all this from memory without testing it, so some details in the code or explanation might be incorrect. I think it's a good idea to trust your judgment if something in my writing doesn't end up working. Let me know how it goes!
Gede_Booo
Scratcher
100+ posts

Cloud Multiplayer

This goes over more simple things that I already understand most of, I'm thinking more how to know what id the player has and knowing when a player leaves. Also, I know that you probably cant help with this, but stored data for each player that saves things such as for example how many coins they have so if they reload the project or come back to the project later the data is still saved, I know that probably wont happen but other people have made it happen so it must be possible.
cubetube7
New Scratcher
59 posts

Cloud Multiplayer

toadkid wrote:

since X position can be anywhere from -240 to 240, first store it to a temporary variable and add +240 to it,
making it a range of 0 to 480, then check if the length of the variable is less than 2, and add a zero at the front if so, and repeat again if less than 3.
i would recommend adding +340 to it because that way the range is 100 to 580, meaning you don't have to worry about adding extra 0s at the start

also, the way that other games save your progress is by using save codes, which is a long line of text containing lots of letters and numbers and symbols that gets decoded into the progress information. although it is technically possible to save player codes into the cloud, since you are limited to only 256 digits per cloud variable, it is hard to store much

to detect when a player leaves, you can have one digit which increases every second, and if that digit doesn't increase for 9 seconds, then you can say that player left
toadkid
Scratcher
17 posts

Cloud Multiplayer

cubetube7 wrote:

i would recommend adding +340 to it because that way the range is 100 to 580, meaning you don't have to worry about adding extra 0s at the start
aw man why didn't I think of that?? thanks for the tip
dizzyboy42
Scratcher
500+ posts

Cloud Multiplayer

I would reccomend using griffpatchs tutorial here are the links incase youtbe is blocked for you:
episode 1
episode 2
episode 3
episode 4

Powered by DjangoBB