Jump to content
fabsberry

VEX: for i

Recommended Posts

Hello, I have a problem understanding "for i" in vex.
 

for (int i=0; i<npoints(0); i++) {

    float val = point(0, "value", i);
    float preval = point(0, "value", i-1);
    
    float calc = val+preval;
    
    setpointattrib(0, "value", i, calc, "set");
}

 

In the picture you can at first see my initial values.
Now i want to add the sum of the previous values to my current value . 
 
I wrote the values i expected to get in red. 
 
What is wrong with my "for i"? 

values.jpg

Share this post


Link to post
Share on other sites

Check out this post on the topic. Basically the variables can't be written to because all new point values are read from the first time input. Your setpointattrib won't actually run until the VEX node exits the code and updates it's initial input with the final value.

Share this post


Link to post
Share on other sites

You need to do it in different way, like this:

float accum = 0;

for (int i = 0; i < @numpt; i++)
{
    float val = point(0, "value", i);
    accum += val;
    setpointattrib(0, "value", i, accum);
}

Input doesn't changed during the code execution, we can't change and retrieve changed attribute values within same wrangle using geometry functions. If you set input point #1 @value from 1.0 to 2.0, when you query input point #1 value again using point() function, you still get unchanged value of 1.0. If you operate on points/vertices/primitives, you can use @-bindings, they are ordinary variables internally, and will work.

// Point Wrangle.

// Works.
@foo = 5.0;
@foo = @foo * 2.0;

// Doesn't work.
setpointattrib(0, "bar", @ptnum, 5.0);
setpointattrib(0, "bar", @ptnum, float(point(0, "bar", @ptnum)) * 2.0);

 

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×