Navigation

    CreatiCode Scratch Forum

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • CreatiCode

    Newly Enhanced Cloud Variables

    2D Blocks
    1
    1
    61
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • info-creaticode
      CreatiCode last edited by info-creaticode

      Introduction

       

      Cloud variables are a powerful tool in MIT Scratch for creating multiplayer programs. CreatiCode has enhanced them further, making them even more capable. In this tutorial, you’ll learn how cloud variables work in the CreatiCode playground.

       
       

      Basics of Cloud Variables

       

      When creating a new variable, you can check the “cloud variable” box:

      f702479b-a596-4ae5-b146-caf71bb4fce8-image.png

      If the project is shared and opened by other users, the value of that cloud variable will be synchronized across all their computers automatically.

      For example, imagine 10 users are running the same project at the same time. If one user changes the value of a cloud variable, the other 9 users will instantly see the updated value.

      Here’s what happens behind the scenes:

      1. User A changes a cloud variable using a “set variable to” or “change variable by” block.
      2. User A’s computer sends the new value to the CreatiCode server.
      3. The server broadcasts the update to the other 9 users running the same project.
      4. Each of those computers receives the message and updates the cloud variable’s value.

       
       

      A Simple Example

       

      Here is an example project: play.creaticode.com/projects/68473cea3d952dd83a76d160

      It has one dog sprite with this code:

      3306fac4-3758-4577-8c80-213ef1781d5b-image.png

      When the program starts, the sprite keeps saying the value of the cloud variable “counter”.

      Whenever any user clicks the sprite, the value of “counter” increases by 1, and all other users running this program will instantly see the new value.

      You can test this by opening the project in two browser windows (one normal mode and one incognito mode). Make sure you’re logged in, as cloud variables only work when you’re signed in. Of course, you can also try this with a friend on 2 computers.

      Here’s what it looks like across two browsers:

      cloudcounter.gif

       
       
       
       

      The “When Variable Changes” Block

       

      To make working with variables easier, especially cloud variables, CreatiCode has added a new event block:

      005ee1af-8126-487d-ba3c-e0ef2949e773-image.png

       

      This block is triggered whenever the selected variable changes, i.e., when its value becomes different. Note that if you set a variable to its current value (hence no change), then this block won’t be triggered.

      It works with both regular and cloud variables. This means you no longer need a forever loop to constantly check the variable — you can run code only when the value actually changes.

      Here’s how we can rewrite the earlier example using this block:

      122a4880-f6c8-43f7-9021-7a186fdf8f6c-image.png

      1. When the sprite is clicked, increase the counter as before.
      2. When the counter changes, the sprite says the new value.

      You can try it here: play.creaticode.com/projects/684747163d952dd83a76d79c

       
       
       
       

      Number of Cloud Variables

       

      MIT Scratch limits you to 10 cloud variables per project. In CreatiCode, you can use up to 100. This allows you to build much more advanced multiplayer projects.

       
       
       
       

      Accepted Values and Update Frequency

       

      In MIT Scratch, cloud variables:

      • Can only store numbers
      • Can only update any variable at most 10 times per second

      For example, suppose your project has 2 cloud variables, then if within any second, your code updated them more than 10 times, then the extra updates will be ignored.

      These limits were put in place to block users from building chat rooms, but they also make cloud variables harder to use.

      In CreatiCode, cloud variables support any type of value, with some important rules:

      • If the value contains any letters (a–z), it must pass a moderation check to filter inappropriate content. Also, such values can only be updated once per second per user. That’s still fast enough for chat room projects.

      • If the value does not contain letters, and only contains numbers or symbols, like 120 or "1_23_44", it bypasses moderation and can be updated up to 20 times per second per user. This is useful when you want to send multiple numbers in a single update. For example, to send X = 50 and Y = 80, you can combine them into one value of "50_80". Also, CreatiCode does not drop any updates. Instead, these updates are queued and sent out at intervals of 50ms.

       
       
       
       

      Cloud Sessions

       

      In MIT Scratch, everyone running the same shared project receives the same cloud variable updates. For example, suppose you share a multiplayer game, then all users who run this game at the same time will receive the same updates. To isolate groups, you’d have to make separate copies of the project.

      CreatiCode introduces cloud sessions, which limit cloud updates to just the users in the same session. If a user hasn’t joined a session, they’re placed in the default session.

      To use sessions, one user has to create one with a unique name first, then other users can join that session with the same name. Here are the2 blocks from the “Variables” category for doing so:

      6c299aae-5c60-4df2-9b81-9dc492d5d1fe-image.png

       
      The session name is just a string that users agree to use. Only users in the same session will receive each other’s cloud variable updates. These blocks are boolean blocks, since they will return true or false depending on whether the action is successful.

      Here’s an example program that shows how to use these blocks:

      b64c75e1-465d-4b95-ad9d-c68284db34db-image.png

       
      To run this program:

      • First, one user needs to create a session named “579”
      • All other users can then join the same session using the same name.
      • After that, only these users will see each other’s updates to the “counter” variable.

      The project is shared here: play.creaticode.com/projects/6847509b3d952dd83a76dbfb

       
       
       
       

      Private Cloud Variables for Clones (Advanced)

       

      In MIT Scratch, cloud variables are always global: shared across all sprites and clones. This creates problems when you’re working with clones.

      For instance, suppose a car sprite creates 2 clones: Clone 1 and Clone 2. If you use a global cloud variable like “facing direction,” then changing it to 90 will affect both clones, even if you only meant to rotate one.

      To fix this, CreatiCode lets you create private cloud variables for each sprite or clone:

      86ec028f-9c73-4591-8462-e4d99ee35191-image.png
       
      Now, each clone will have its own “direction” variable, separate from the others. For example, when one user updates the direction variable for “Clone 1”, then the direction will be updated for “Clone 1” running on all other computers, yet “Clone 2” will not be affected on any computer.

       
       
       
       

      Example for Private Cloud Variables

       

      Here is an example project: play.creaticode.com/projects/6847575f1c49cae45091d685

      When the green flag is clicked, the original dog sprite hides and creates two clones, with IDs 1 and 2:

      421699f2-f4bc-45e6-9b02-854ecfab8946-image.png

       
      When born, each clone shows itself, says its clone ID, and moves to one side of the stage:

      884f5fda-d4cd-4209-8005-176279dc4986-image.png

       
      Now, when a clone is clicked, it increases its own private cloud variable called “direction” by 15. This triggers only that clone to rotate:

      dcf57267-c6f6-4929-ac99-c27ede0e9f1c-image.png

       
      As a result, when any user clicks on a dog clone, that specific clone rotates on every computer — but the other clone is unaffected:

      cloudcounter.gif





      We hope you enjoy these new features, and we can’t wait to see the amazing multiplayer projects you’ll build with them!

      1 Reply Last reply Reply Quote 1
      • Pinned by  info-creaticode info-creaticode 
      • First post
        Last post