-
Notifications
You must be signed in to change notification settings - Fork 3
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:
- Some variables in GlowScript Blocks are vectors. Vectors have three components, and sometimes you want to use or edit only one of the components.
- 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.
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.
Click on item to reveal the menu below.
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".
Attach a vector to the set variable block to assign the data to the 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.
Click on item to reveal the menu, which includes the names of all assigned variables so far.
The name "MyVector" appears in the menu because this variable was assigned in the steps above. Select "MyVector" from the menu.
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".
Select the exact data that you want. In this example, the user wants only the x component.
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.
which prints: 7
Select the set item to block, and choose "MyVector" from the menu.
Click on "vector" to see the component 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).
The user sets the y component to 2 by attaching a number block.
The user can then verify that the data has been changed by printing the vector.
which prints: <7, 2, 9>
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.
Click on item to reveal the menu below.
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".
Attach a sphere block to the set variable block to assign the shape to the 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.
Click on item to reveal the menu, which includes the names of all assigned variables so far.
The name "Ball" appears in the menu because this variable was assigned in the steps above. Select "Ball" from the menu.
The block turns yellow, which is the shape color, because the variable Ball has been assigned to a shape.
The shape itself is not useful, but its attributes are useful. Click on the menu that says "sphere".
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.
You might also click on the "vector" to select only one component, as explained above.
If the user clicks on mass instead of position, the block will turn turquoise, because mass is a number.
There is also some text data (green) and some Boolean data (purple) associated with the shape.
Select the set item to block, and choose "Ball" from the menu.
The shape itself cannot be edited, but the data stored as its attributes can be edited. Click on the menu that says "sphere".
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",
and then selects "y" from the menu under "vector".
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.
The user can then verify that the data has been changed by printing the position of the Ball.
which prints: <0, 4, 0>