Storing Data#

Variables let you store data during a project, such as a number, a true/false value, or a list of items.

Create a Variable#

Go to the Logic - Variables category in the toolbox, and select Make a Variable.

Screenshot of the toolbox with the Logic - Variables category open, and the Make a Variable button highlighted.

Choose a Variable Type#

Select the type of variable you want to create.

Screenshot of the Create a Variable dialog, showing the Variable, Boolean, List, and 2D List type options.

Numeric variables can store text or a number.

A numeric variable.#
(my_variable)

Booleans store true or false values.

A Boolean variable.#
<my_boolean>

Lists store a sequence of text and/or numbers.

A list variable.#
设定 [my_list v] 为 (0) (0) (0)

2D Lists store a sequence of text and/or numbers, organized into rows and columns. These are useful when you want to store a lot of data at once and keep all the values organized.

A 2D list variable.#
设定 [my_2d_list v] 为 ([0] [0]) ([0] [0])

Name the Variable#

Type a name for your variable.

Screenshot of the Create a Variable dialog, with the New variable name field highlighted.

Create the Variable#

Select Create to add your variable to the toolbox.

Screenshot of the Create a Variable dialog, with the variable named drivingDistance and the Create button highlighted.

Use the Variable#

Along with your variable, there will also be blocks in the toolbox you can use to change its value in the middle of a project. For example, numeric variables include a reporter block, along with the set numeric variable and change numeric variable blocks.

The my_variable reporter block.#
(my_variable)
The set numeric variable block.#
设定 [my_variable v] 为 (1)
The change numeric variable block.#
将 [my_variable v] 改变 (1)

Editing a Variable#

Select a variable block’s dropdown to choose a different variable, or to rename or delete the currently selected variable.

Screenshot of the set numeric variable block's dropdown open, showing the my_variable option along with Rename the 'my_variable' variable and Delete the 'my_variable' variable options.

Example#

In this project, pressing the Brain’s Check button will add 1 to a variable named count and display it on the Console, while pressing the Brain’s Left button will reset it back to 0.

A project that counts Brain Check button presses, and resets the count when the Left button is pressed.#
当开始
设定 [count v] 为 [0]
永久循环
如果 <Brain [确认 v] 键按下了?> 那么
[Add 1 to the count.]
将 [count v] 改变 (1)
在控制台上打印 (count)◀ 并设定光标至下一行
否则如果 <Brain [◀ v] 键按下了?> 那么
[Reset the count back to 0.]
设定 [count v] 为 [0]
在控制台上打印 (count)◀ 并设定光标至下一行
结束
结束