Variables#

To put a variable in the Monitor Console, drag and drop its Reporter block to the Monitor Icon on the right side of the screen.

Animation showing a user dragging the 'drive is done?' block from the workspace to the Monitor Console, located at the top right of the screen. The block is moved over the small monitor icon in the top right corner of the coding environment.

Numeric variable#

The Numeric variable block is used to report the numeric value assigned to the variable. Variables are created with global scope by default.

a VEXcode stack of code blocks containing a Numberic variable block#
    (myVariable)

Use the Set numeric variable block to set or update the numeric value of a variable.

The Numeric Variable reporter block is used in blocks with circular spaces.

Variables are created using the Make a Variable button.

Rectangular button with the text 'Make a Variable'.

In this example, the variable driveLength is set to 500 and used in the Drive for block. This makes the Drive for block move forward for 500 mm.

A VEXcode blocks stack of code containing a “when started” hat block that sets [driveLength v] to 500 and drives forward for the value of [driveLength] in millimeters.#
    when started :: hat events
    set [driveLength v] to [500]
    drive [forward v] for (driveLength) [mm v] ▶

Set numeric variable#

The Set numeric variable block is used to set a numeric variable to a given value. Variables are created with global scope by default.

A VEXcode blocks stack of code containing a block that sets [myVariable v] to 0.#
    set [myVariable v] to [0]

Choose which numeric variable to use. Numeric variables can also be renamed or deleted.

An orange notched coding block with a dropdown menu open, displaying options to select 'myVariable,' rename the variable, or delete the 'myVariable' variable.

The Set numeric variable block can accept decimals, integers, or numeric blocks.

In this example, the variable driveLength is set to 500 and used in the Drive for block. This makes the Drive for block move forward for 500 mm.

A VEXcode blocks stack of code containing a “when started” hat block that sets [driveLength v] to 500 and drives forward for the value of [driveLength] in millimeters.#
    when started :: hat events
    set [driveLength v] to [500]
    drive [forward v] for (driveLength) [mm v] ▶

Change numeric variable#

The Change numeric variable block is used to change a variable by the given value.

A VEXcode blocks stack of code containing a block that changes [myVariable v] by 1.#
    change [myVariable v] by (1)

Choose which numeric variable to use - the selected variable can also be renamed or deleted.

An orange notched coding block with the text 'change myVariable by 1' and a dropdown menu expanded below it, showing options to select 'myVariable', rename the variable, or delete the 'myVariable' variable.

The Change numeric variable block can accept decimals, integers, or numeric blocks.

In this example, the variable driveLength is changed by 500 and used in the Drive for block. This makes the Drive for block move forward for 500 mm.

A VEXcode blocks stack of code containing a “when started” hat block that changes [driveLength v] by 500 and drives forward for the value of [driveLength] in millimeters.#
    when started :: hat events
    change [driveLength v] by [500]
    drive [forward v] for (driveLength) [mm v] ▶

Boolean variable#

The Boolean variable block is used to report the value of a Boolean variable. Variables are created with global scope by default.

A VEXcode stack of blocks containing a boolean variable block#
    <myBoolean>

Use the Set boolean variable block to set or update the value of a boolean variable.

The Boolean variable block reports a true or false value and is used in blocks with hexagonal (six-sided) spaces.

Boolean variables are created using the Make a Boolean button.

A rectangular button with the text 'Make a Boolean' centered inside.

In this example, the driveNow Boolean variable is set to True at the start of the project. This makes the Drivetrain drive forward until the driveNow variable returns False.

A VEXcode blocks stack of code containing a “when started” hat block that sets [driveNow v] to true, checks if [driveNow] is true, drives forward if the condition is met, and then stops driving.#
    when started :: hat events
    set [driveNow v] to <true>
    if <driveNow> then
    drive [forward v]
    end
    stop driving

Set Boolean variable#

The Set Boolean variable block is used to set a boolean variable to a given value. Variables are created with global scope by default.

A VEXcode stack of code blocks containing a set Boolean variable block#
    set [myBoolean v] to <true>

Choose which boolean variable to use. Boolean variables can also be renamed or deleted.

A notched coding block with the text 'set myBoolean to true' with dropdown menus for the variable name and Boolean value, and an open dropdown menu displaying options to rename or delete the variable.

Choose a Boolean value.

A notched coding block with the text 'set myBoolean to true' with dropdown menus for the variable name and Boolean value, and an open dropdown menu showing options 'true' (selected) and 'false' for the Boolean value.

The Set Boolean variable block can accept hexagonal (six-sided) shaped blocks.

In this example, the driveNow Boolean variable is set to True at the start of the project. This makes the Drivetrain drive forward until the driveNow variable returns False.

A VEXcode blocks stack of code containing a “when started” hat block that sets [driveNow v] to true, uses an “if” block to check if [driveNow] is true, drives forward if the condition is true, and then stops driving.#
    when started :: hat events
    set [driveNow v] to <true>
    if <driveNow> then
    drive [forward v]
    end
    stop driving

List item#

The List item block is used to report the value of an item in a list.

a VEXcode stack of code blocks containing a list item block#
    (item (1) of [myList v])

Choose which list to use. The list can also be renamed or deleted.

A rounded coding block labeled 'item 1 of myList,' with '1' in a white bubble and 'myList' in a dropdown menu. Below the block, an orange dropdown menu is expanded, showing options: 'myList' (selected), 'Rename variable,' and 'Delete the "myList" variable.'"

Enter the number of the item’s position within the list. Here, the second item in list (item 2) is being reported.

The List item block can accept decimals, integers, or numeric blocks.

In this example, the cardinalHeadings list stores the degrees for north, east, south, and west. The Drivetrain turns to face item 3, which is set to 180 degrees.

A VEXcode blocks stack of code containing a “when started” hat block that sets [cardinalHeadings v] to a list of values (0, 90, 180, 270) and turns to the heading of the third item (180 degrees) in [cardinalHeadings v].#
    when started :: hat events
    set [cardinalHeadings v] to (0) (90) (180) (270)
    turn to heading (item (3) of [cardinalHeadings v]) degrees ▶

Replace List item#

The Replace List item block is used to update an item in the list to a new value.

a VEXcode stack of code blocks that contains a replace list item block#
    replace item (1) of [myList v] to (0)

Choose which list to use. The list can also be renamed or deleted.

A coding block labeled 'replace item 1 of myList to 0' with a dropdown menu expanded below, showing options to select 'myList', 'Rename variable', or 'Delete the "myList" variable'.

The Replace List item block can accept decimals, integers, or numeric blocks.

In this example, only item 2 of the cardinalHeadings list is replaced with 90. The Drivetrain will then turn to a heading of 90 degrees.

A VEXcode blocks stack of code containing a “when started” hat block that replaces the second item of [cardinalHeadings v] with 90 and then turns to the heading of the second item (90 degrees) in [cardinalHeadings v].#
    when started :: hat events
    replace item (2) of [cardinalHeadings v] to (90)
    turn to heading (item (2) of [cardinalHeadings v]) degrees ▶

Set List item#

The Set List item block is used to set every item in the list to the entered values. Variables are created with global scope by default.

a VEXcode stack of blocks containing a set list item block.#
    set [myList v] to (0) (0) (0) (0) (0)

Choose which 2D list to use. The 2D list can also be renamed or deleted.

A coding block that reads 'set myList to 0, 0, 0, 0, 0' with a dropdown menu open showing options: myList, Rename variable, Delete the 'myList' variable.

The Set List item block can accept decimals, integers, or numeric blocks.

In this example, the cardinalHeadings list stores the degrees for north, east, south, and west. The Drivetrain turns to face item 3, which is set to 180 degrees.

A VEXcode blocks stack of code containing a “when started” hat block that sets [cardinalHeadings v] to a list of values (0, 90, 180, 270) and turns to the heading of the third item in the list (180 degrees).#
    when started :: hat events
    set [cardinalHeadings v] to (0) (90) (180) (270)
    turn to heading (item (3) of [cardinalHeadings v]) degrees ▶

Length of List#

The Length of List block is used to report the number of items in a list.

a VEXcode stack of code blocks containing a length of list block#
    (length of [myList v])

Choose which list to use. The 2D list can also be renamed or deleted.

A rounded coding block displaying 'length of myList' in orange, with a dropdown menu open showing options to select, rename, or delete the 'myList' variable.

The Length of List reporter block fits in blocks that accept circular spaces.

In this example, the length of the cardinalHeadings list will be printed to the Console.

A VEXcode blocks stack of code containing a “when started” hat block that prints the length of the list [cardinalHeadings].#
    when started :: hat events
    print (length of [cardinalHeadings v]) ▶

2D List item#

The 2D List item block is used to report the value of an item in a 2D list.

a VEXcode stack of code blocks containing a 2D list item block#
    (item (1) (1) of [my2Dlist v])

Choose which 2D list to use. The 2D list can also be renamed or deleted.

An orange coding block labeled 'item 1 1 of my2DList,' with a dropdown menu open showing options to select 'my2DList,' rename the variable, or delete the 'my2DList' variable. The block has two white circular inputs, both set to '1,' and the 'my2DList' variable is in a notched shape.

Enter the item’s row (first number) and column (second number) position within the list to be reported. Here, the item in position row 2, column 3 in the 2D list is being reported.

A VEXcode blocks stack of code containing a block that retrieves the item at row 2, column 3 of [my2Dlist].#
    (item (2) (3) of [my2Dlist v])

The 2D List item block can accept decimals, integers, or numeric blocks.

In this example, the moveCoordinates 2D List is used to store numeric values that relate to positions on the playground. Assuming that the Robot starts at the coordinate (0, 0), the Drivetrain uses the items from (5,1) and (5,2) in the 2D List to move to the coordinate (300, 200).

A VEXcode blocks stack of code containing a “when started” hat block that sets [move_coordinates v] to a 2D list with values ((300)(150)) and ((500)(200)). It drives forward for the value in row 1, column 1 (300 mm), turns right for 90 degrees, and then drives forward for the value in row 1, column 2 (150 mm).#
    when started :: hat events
    set [move_coordinates v] to ((300)(150)) ((500)(200))    
    drive [forward v] for (item (1)(1) of [move_coordinates v]) [mm v] ▶
    turn [right v] for (90) degrees ▶
    drive [forward v] for (item (1)(2) of [move_coordinates v]) [mm v] ▶

Replace 2D List item#

The Replace 2D List item block is used to update an item in the 2D list to a new value.

a VEXcode blocks stack of code containing a replace 2d list item block#
    replace item (1) (1) of [my2Dlist v] to (0)

Choose which 2D list to use. The 2D list can also be renamed or deleted.

An orange coding block with the text 'replace item 1 1 of my2Dlist to 0,' with a dropdown menu open showing options for the variable 'my2Dlist,' as well as options to rename or delete the variable.

The Replace 2D List item block can accept decimals, integers, or numeric blocks.

In this example, the Drivetrain will drive forward for 2 seconds. It will record the Robot’s current Y coordinate in the second column of the first row in its moveCoordinates 2D list before stopping.

A VEXcode blocks stack of code containing a “when started” hat block that drives forward, waits for 2 seconds, replaces the value in row 1, column 2 of [moveCoordinates v] with the robot’s current Y position in millimeters, and then stops driving.#
    when started :: hat events
    drive [forward v]
    wait (2) seconds
    replace item (1) (2) of [moveCordinates v] to (position [Y v] in [mm v])
    stop driving

Set 2D List item#

The Set 2D List item block is used to set every item in the 2D list to the entered values. Variables are created with global scope by default.

a VEXcode stack of code blocks containing a set 2d list item block#
    set [move_coordinates v] to ((0)(0)) ((0)(0))

Choose which 2D list to use. The 2D list can also be renamed or deleted.

An orange coding block labeled 'set move_coordinates to' with a dropdown menu open below. The dropdown menu shows the options: 'move_coordinates' (selected), 'my2Dlist,' 'Rename variable,' and 'Delete the "move_coordinates" variable.'"

The Set 2D List item block can accept decimals, integers, or numeric blocks.

In this example, the move_coordinates 2D List is used to store numeric values that relate to positions on the playground. Assuming that the Robot starts at the coordinate (0, 0), the Drivetrain uses the items from (1,1) and (1,2) in the 2D List to move to the coordinate (300, 150).

A VEXcode blocks stack of code containing a “when started” hat block that sets [move_coordinates v] to a 2D list with values ((300)(150)) and ((500)(200)). It drives forward for the value in row 1, column 1 (300 mm), turns right for 90 degrees, and then drives forward for the value in row 1, column 2 (150 mm).#
    when started :: hat events
    set [move_coordinates v] to ((300)(150)) ((500)(200))    
    drive [forward v] for (item (1)(1) of [move_coordinates v]) [mm v] ▶
    turn [right v] for (90) degrees ▶
    drive [forward v] for (item (1)(2) of [move_coordinates v]) [mm v] ▶

Length of 2D List#

The Length of 2D List block is used to report the number of rows or columns in a 2D list.

a VEXcode stack of code blocks containing a length of 2d list block#
    length of [my2Dlist v] [rows v]

Choose which 2D list to use. The 2D list can also be renamed or deleted.

A rounded, orange coding block labeled 'length of my2Dlist rows' with a dropdown menu expanded, showing options 'my2Dlist,' 'Rename variable,' and 'Delete the "my2Dlist" variable.'"

Choose which value to report. The 2D list can report either rows or columns.

A rounded, orange coding block labeled 'length of my2Dlist rows' with the 'rows' dropdown menu expanded, showing options 'rows' and 'columns,' with 'rows' selected.

The Length of 2D List reporter block fits in blocks that accept circular spaces.

In this example, the Robot will print how many rows there are in the moveCoordinates 2D List.

a VEXcode stack of code blocks containing a when started block and a print block that prints the length of moveCoordinates.#
    when started :: hat events
    print (length of [moveCoordinates v] [rows v]) ▶