A Few Gotchas in VEX

A Few Gotchas When Writing VEX

Since VEX is all the rage these days, I thought I would pass on a few gotchas you might encounter when writing VEX. If you have a bit of experience with code and are accustomed to following certain conventions, small changes of syntax might drive you mad. I thought for a while there that I was losing it but I got the reassurance from my buddy @jonathanGranskg that indeed this is the way it is.

Using Constants

First gotcha is that you can’t use constants when writing VEX in a Wrangle. At least not that I’m aware of. For those unfamiliar with constants, they are variables but once initialized they cannot be changed during the execution of the program. They are locked. Any attempt to change the constant will produce an error. Not even Mcgyver will save you on this one so you would use these types of variables for values you know will not change. Think PI,  Avogadro’s number or the speed of light in case you are planning to travel to Mars. Constants look like this:

const float PI = 3.14159;  // This is your constant. It will never change. 

float radius = 6.0;
float circle = 2 * PI * radius

If you try this in a Wrangle node, you will get a syntax error. So what is the solution or alternative in VEX?

Macros

If you need to make use of a constant, you can use a macro definition. Macros are basically just text replacement. The way to define a macro is through the #define directive. Now, I’m glossing over a lot of technical detail here so to keep things simple, just knowing how to define one is sufficient for our purposes. Let’s say I want to create a spiral out of a line through an Attribute Wrangle. We first want to define our natural logarithm constant e. We type the following code in an Attribute Wrangle:

#define e 2.718281828

@P.x = .1 * pow(e, 5 * @ptnum * .001) * sin(@ptnum);
@P.z = .03 * pow(e, 50 * @ptnum * .001) * cos(@ptnum);

 

VEX-define-directive

Define Directive
Define Directive

Notice the lack of a semi-colon and the equals sign which you would use when defining regular variables. Do you have to do this? No, you can just as easily use a regular variable and be done but I thought I would be a nice guy and let you know about this alternative method.

Function Arguments

Now this one is a bit perplexing. Actually, I shouldn’t say that because the folks at Side Effects are way smarter than me so they may have a real good reason. Nevertheless, it caught me by surprise. Defining functions in VEX have a slight syntax difference from say, oh I don’t know, every other programming language? Here I define a simple function called randValue. Nothing fancy. All it does is return a random value and I assign it to the green component of @Cd. Here is an exercise to sharpen your debugging skills. See if you can spot the difference between the C style function and the VEX function respectively.

// C style function

float randValue(float seed, int pointNum) {
 float val = rand(seed * pointNum);
 return val;
}

float seed = 1.12345;
@Cd.y = randValue(seed, @ptnum);
// VEX function

float randValue(float seed; int pointNum) {
 float val = rand(seed * pointNum);
 return val;
}

float seed = 1.12345;
@Cd.y = randValue(seed, @ptnum);

This one might have slipped passed you but look at the difference in function parameters. Notice the comma in the C function and the semi-colon in the VEX function to separate the function parameters. Yes, my friends. You are seeing that correctly. Try it in a Wrangle for yourself. What is even crazier is that you use a comma to separate the arguments when you call the function! This actually had me Googling C functions because I started to question myself. 😀

Foreach

The last gotcha that suffers from the same VEX function syntax anomaly is the foreach statement. The foreach statement is used to loop over an array. Different languages have different ways of using it so this one is not too bad but if you come from another language, the syntax may be a bit annoying.

This is what a foreach looks like in C#:

int count = 0;
foreach (int element in cats) {
  count += 1;
  System.Console.WriteLine("Cat #{0}: {1} is awesome!", count, element);
}

This is what it looks like in VEX. Notice the semi-color after the int num value and the vertices array. This arbitrary piece of code just creates some funky geometry.

int vertices[] = pointvertices(0, 10);

foreach (int num; vertices) {
 @P %= num/10;
 @Cd = @P;
}

Foreach Statement

Foreach Statement
Foreach Statement

Nothing big and if you’ve written enough VEX then this shouldn’t be new to you. There may be others I have not come across yet. Just a few gotchas for those just jumping in.

Share:
990adjustments

990adjustments

I am a motion designer & developer based out of South Florida. When not designing or animating pixels, I wrangle some code. If all else fails, I watch Twilight Zone, I Love Lucy, or Three's Company reruns.