Working with variables!

Variables in Coblocks

Variables are the bread and butter of programs. Without them, it’s hard to make sense of the countless of words and numbers we use to make things fly, run, speak, fall, bounce and dance :dancer:. Here we explain how variables are used in CoBlocks, and the magic you can create with them!

1. Declaring (creating) variables

To create a variable in CoBlocks, drag & drop the set variable - block found in the Data->Variables category from the toolbox into your workspace. The default variable name is “myVar”, which you can change to one that suits your needs!

2. Assign a value

Now that you’ve defined your first variable, let’s count some numbers. In the Values - category you can find expression-blocks that we can feed our variable with.

3. Using variable values

We stored a number in our variable. To use it, we take the variable expression block and place it into a statement block

4. Changing variables

You can update the value a variable block holds by using the set variable - block again. Let’s have our item count how many times it was clicked.

For changing number-values, we can also use the change variable by - block to get the same result!
29

5. Tutorial example: Open sesame!

Let’s make a locked door. To unlock it, you need to collect an item first.

1. First, we create a door and a key

2. Let’s open the coding window

Here we define the variable “isLocked” and set it to true.

3. Now we create click - events for each item. Let’s start with the key.

When the key is clicked, we set the variable isLocked to false and remove the key.

4. Let’s add the event for the door

When the door is clicked, we check the isLocked variable. If it is still true (because we did not pick up the key yet) the door won’t open for us. If it is false (because we picked up the key), the door will open!

5. Here is our example in action

6. Wrapping up

Hope you enjoyed this dive into variables. You will see variables a lot, especially when making lists and functions in CoBlocks. Stay tuned for more updates and how-to’s :slight_smile:

4 Likes

Can a variable be used in two different scripts in CoBlocks? It doesn’t seem to work.

3 Likes

Hi @pross67,

variables are currently only usable in the script tab they were defined in. We’re looking into the concept of global variables across several coding tab :slight_smile:

6 Likes

Hi @Stefan, I’m searching the forum for information about variables and came upon this old thread. Wondering if anything has changed? I have a student who would like to remember a variable as she switches scenes. Even more, can the state of the whole scene be remembered? She placed some hidden objects in scene 1. When the user clicks each object, they are deleted and she changes her variable by 1. If she goes to scene 2, then returns to scene 1, her variable resets itself and all of the previously deleted items are back. Is there any way to have a global memory of these actions?

2 Likes

I just found “Set CoSpace Property” This should be what I need! Thank you!

3 Likes

Wondering if it’s still not possible to cary a variable from one script to another? I’m trying to do this now but it doesn’t seem possible.

2 Likes

In CoBlocks, CoSpace Properties are global variables, available across scripts and scenes.

Or you talking about Typescript/Python?

4 Likes

I mean in CoBlocks. When I create a variable in one CoBlocks Workspace, it doesn’t appear in another CoBlocks workspace. How can I carry a variable from one workspace to another?

3 Likes

By using CoSpaces Properties (global variables). Variables are scoped, and the CoBlocks variables have local scope to the workspace.

You can use both in harmony, but you need to be clear about when you want a variable to local or global.

See Understanding Variables, Scope, and Hoisting in JavaScript | DigitalOcean for further reading on scope.

2 Likes

Thanks for your help, @techleapnz! I think this one is over my head! :laughing:

But I’ll see what I can figure out!

2 Likes

Hi Stefan,
Regarding the question @pross67 asked in September 2018, are can variables now be used across multiple CoBlocks tabs?
Thanks!

2 Likes

Hi @MBridges,

CoBlocks variables are local only, but (as @techleapnz mentioned) one can use CoSpaces property to store strings and numbers globally. That way is it possible to access store data not only from another tab, but also from another scene.

2 Likes

Thank you for your quick reply. I found this sample space which passes the variable between scenes correctly. What I’m trying to do is modify the same variable from more than one CoBlocks tab. For example, tab one adds 1 to a counter when the girl is clicked, tab 2 adds 1 when the boy is clicked. One counter represents total clicks - boy or girl. Is this possible? Might there be a gallery example that I’ve missed? Thank you!

2 Likes

I updated the sample space.
On the third scene there are two examples now:

  1. How to access local variable defined in one tab from another tab.
    This is basically emulation of variable shared between tabs with help of global function feature. Almost the same thing that you have asked before.
    Please, note that you cannot directly update or access local variable defined in one tab from another tab, this is possible only though a global function. This solution will work only if you need to access variable within the same scene. If you plan to share data between multiple scenes, then only the approach #2 will work.

  2. How to read and update CoSpace property using global function.
    The usage of global function is not mandatory in this case, but it will save you from accidental mistakes, so I encourage you to use this approach.

Hope this helps

4 Likes

Hi @CoSpaces_Edu we are creating an escape room and I wanted to try out the door/ key function.
Something isn’t work as in your video. I don’t know why I can’t program condition if = true.
Here is a short video from what’s going wron. Maybe in the last two years somethings are changed?


g

3 Likes

I think you have to move the variable just a bit more to the right? Is not always easy to move the variable into the right hole

4 Likes

Можно ли сделать так, чтобы при значении какой-либо переменной что-то происходило? К примеру, отдельный блок. Допустим, если переменная равна true, то тогда открыть дверь будет легче.

1 Like

hi, when I click the boy, instead of making a sequence of numbers, can i make the boy say a sequence of letters? If yes, how do you do it?

1 Like