How to count within loop


I was fiddling around with VL for a while, when I thought about giving my Objects an unique ID each. Thought it would be straight forward, but for me it wasn’t. I played around with accumulators, + nodes, increment nodes but without a satisfying result.

What I’m trying to achieve:
A counter or variable counts up once for each time a loop is executed.
Looking at the particle patch from Node it would be similar to this:

I managed to count up within the loop with index and/or accumulators, but then the index resets whenever I manually execute the loop again later.

Don’t know if my concept is completely wrong, I thought:
Create Counter -> store an inital value -> count that value up each time the loop is executed -> as the “create mycounter” is assigned to create in the parent patch it will only get executed once, thus no value resetting.

In my patch right now it seems as if the value is resetted each time the loop is iterated. Where am I going wrong?

P.S: Couldn’t upload .vl files :( (2.9 kB)

This is THE pitfall of immutable objects like your counter, you have to store the new counter back into a field. what you did is creating one and then using that same instance every time without remembering the last state of it.

so you have two options, either check the box “mutable” in the patch tab of your counter, which might lead to some complications in conjunction with immutable types, or store the counter into a field and put it in as an accumulator and get the modified counter back after the loop is finished to store it back into the field.

here is a quick modification of your patch demonstrating the second option.

ID (4.7 kB)

perfect! thanks for clarifying, I didn’t entirely grasp that…