Skip to content
Hunter Close edited this page Aug 15, 2018 · 11 revisions

Overview of Variables

The Variables blocks in GlowScript blocks are in some cases very similar to those in the original Google Blockly library.

Google Blockly help for Variables blocks

Using Variables blocks in GlowScript Blocks is a little more complicated than it is in Google Blockly, for two reasons:

  1. Some variables in GlowScript Blocks are vectors. Vectors have three components, and sometimes you want to use or edit only one of the components.
  2. GlowScript Blocks has shapes, and shapes have attributes. Sometimes you want to use or edit the data in only one attribute of a shape. Furthermore, sometimes this attribute is a vector. See number 1.

The help offered here focuses on the issues that users need to understand to manage variables specifically in GlowScript Blocks.

Vector variables

Set a variable to a vector

To set a variable equal to a vector, start with the set item block. The block is gray at first because no data is assigned to it.

set item

Click on item to reveal the menu below.

set item menu

The menu reveals all of the variables that have been assigned so far in the program. In this example, no variables have been assigned yet.

Choose a name for the new variable. In this case, the user has named the new variable "MyVector".

set with new name

Attach a vector to the set variable block to assign the data to the variable.

attach data

Get data from a vector variable

To retrieve, or "get", data from a variable, drag the item block into the workspace. The block is gray at first because the data is not yet selected, so the type of data (which is shown by the color of the block) is not known yet.

get item

Click on item to reveal the menu, which includes the names of all assigned variables so far.

get item menu

The name "MyVector" appears in the menu because this variable was assigned in the steps above. Select "MyVector" from the menu.

select MyVector

The block turns blue, which is the vector color, because the variable MyVector has been assigned to vector data. Now, you might want to get the whole vector or only one component. Click on the menu that says "vector".

MyVector change selection

Select the exact data that you want. In this example, the user wants only the x component.

get MyVector x

The block changes color to turquoise, because now the block represents a single number: the x component of MyVector. To verify that the block represents this number, you can print this variable.

print MyVector x

which prints: 7

Set data in an existing vector variable

Select the set item to block, and choose "MyVector" from the menu.

set MyVector

Click on "vector" to see the component menu.

set MyVector menu

In this example, the user wants to edit only the y component of MyVector. Note that the block changes color from blue (vector) to turquoise (number).

set MyVector y

The user sets the y component to 2 by attaching a number block.

set MyVector new number

The user can then verify that the data has been changed by printing the vector.

print MyVector new

which prints: <7, 2, 9>

Shape variables

Set a variable to a shape

To set a variable equal to a shape, start with the set item block. The block is gray at first because no data is assigned to it.

set item

Click on item to reveal the menu below.

set item menu

The menu reveals all of the variables that have been assigned so far in the program. In this example, no variables have been assigned yet.

Choose a name for the new variable. In this case, the user has named the new variable "Ball".

set with new name Ball

Attach a sphere block to the set variable block to assign the shape to the variable.

attach shape

Get data from a shape variable

To retrieve, or "get", data from a variable, drag the item block into the workspace. The block is gray at first because the data is not yet selected, so the type of data (which is shown by the color of the block) is not known yet.

get item

Click on item to reveal the menu, which includes the names of all assigned variables so far.

get item menu

The name "Ball" appears in the menu because this variable was assigned in the steps above. Select "Ball" from the menu.

select MyVector

The block turns yellow, which is the shape color, because the variable Ball has been assigned to a shape.

select Ball

The shape itself is not useful, but its attributes are useful. Click on the menu that says "sphere".

Ball change selection

All of the attribute data for this shape is now available to use. If the user clicks on "pos" (position), the block will turn blue, because position is a vector.

get Ball pos vector

You might also click on the "vector" to select only one component, as explained above.

get Ball pos menu

get Ball pos x

If the user clicks on mass instead of position, the block will turn turquoise, because mass is a number.

get Ball mass

There is also some text data (green) and some Boolean data (purple) associated with the shape.

Set data in an existing shape variable

Select the set item to block, and choose "Ball" from the menu.

set Ball

The shape itself cannot be edited, but the data stored as its attributes can be edited. Click on the menu that says "sphere".

set Ball menu

In this example, the user wants to edit only the y component of Ball's position. The user selects "pos" from the menu under "sphere",

set Ball pos

and then selects "y" from the menu under "vector".

set Ball pos y

Note that the block changes color from yellow (shape) to blue (vector) to turquoise (number).

The user sets the y component to 4 by attaching a number block.

set Ball pos y number

The user can then verify that the data has been changed by printing the position of the Ball.

print Ball pos

which prints: <0, 4, 0>