Before Variables: 3. Oversimplified Parameters — The Control Room 🟢

<aside>

$$ {\Large\text{A variable is a named place where Vex temporarily keeps a value.}} $$

</aside>

TL:DR

Type Stores Example Variable Attribute
int Whole numbers 10 int i = 10; i@i
float Decimal numbers 3.14 float f = 3.14; f@f
vector 3 values {1,0,0} vector v = {1,0,0}; v@v
string Text "hello" string s = "hello"; s@s
matrix A matrix ident() matrix m = ident(); 4@m

Fundamentals

Variables let us name information, reuse it, and change it easily.

float speed = 5.0;

Create a variable called speed and put 5.0 in it.

Then you can use that value:

@P.y += speed;

<aside> ⚠️

An important distinction

This creates a VEX variable.

float speed = 5.0;

This creates an attribute called speed.

f@speed = 5.0;

</aside>

sheet_variables_gerardojustel_re1mon.png

<aside>

$$ {\Large\begin{array}{c}\text{VARIABLE} \quad \text{temporary information used while your VEX code is running.}\\[8pt]\text{ATTRIBUTE} \quad \text{data stored as part of the geometry.}\end{array}} $$

</aside>

Why do we need variables?

Variables give values a name so we can refer to, reuse, and change them easily.

3 Things to Know About Variables

1. Variables can be changed

The name speed still refers to the same variable, but its value has changed.

float speed = 5.0;
speed = 10.0;

2. A variable has a type

A variable has a type that describes what kind of value it stores.

float speed = 5.0;
vector direction = {0,1,0};
int count = 10;

3. Variables are temporary

VARIABLE — A temporary piece of information used while your VEX code is running.

ATTRIBUTE — Data stored on the geometry and available to other nodes.

Types of Variables

Type Stores Example Variable Attribute
int Whole numbers 10 int i = 10; i@i
float Decimal numbers 3.14 float f = 3.14; f@f
vector 3 values {1,0,0} vector v = {1,0,0}; v@v
string Text "hello" string s = "hello"; s@s
matrix A matrix ident() matrix m = ident(); 4@m

The type tells VEX what kind of value it can hold.

The same type can be used for a variable or stored as a geometry attribute.

In Vex

// Create an integer variable from the "a" parameter
int i = chi("a");

// Store the integer as an integer attribute
i@i = i;

// Create a float variable from the "b" parameter
float f = chf("b");

// Store the float as a float attribute
f@f = f;

// Create a vector variable from the "c" parameter
vector v = chv("c");

// Store the vector as a vector attribute
v@v = v;

// Create a string variable from the "d" parameter
string s = chs("d");

// Store the string as a string attribute
s@s = s;

// Create a matrix variable from the "e" parameter
matrix m = ch4("e");

// Store the matrix as a 4×4 matrix attribute
4@m = m;

Operations with Variables

Variables can be used in calculations and expressions.

Arithmetic

Operation Meaning Example
+ Add speed + 3
- Subtract speed - 1
* Multiply speed * 2
/ Divide speed / 2

In Vex

float speed = 5.0;
float doubled = speed * 2;
float faster = speed + 3;

The same operations work with other numeric types, such as int:

int count = 5;
int doubled = count * 2;
int more = count + 3;