What am I doing wrong with lists in Plugins?

So, I’m finally trying to learn a little of programming plugins.
I found some pseudo code for a boids routine, I understand the math so thought I’d try and make it into a plugin.
I’ve been using connectall plugin to help me and so I tried to use lists as it does and things broke…
If anyone could have a quick look and tell me why its broken that would be lovely :)
(its in the keep away section, I’m trying to keep it all separate while I work out whats going on!)

help me fix this… (49.1 kB)

the error was that you were adding slices from the input to the list in the outer loop while the inner loop was already using elements of the list, which you didn’t yet add to it. (j>i)

either changing SpreadMax to j in line 46

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

or the same evaluate function but shorter and without a list but reusing both ispreads:

public void Evaluate(int SpreadMax)
{
	FOutput.AssignFrom(FInput);
	for (int i = 0; i < SpreadMax; i++) 
	{
		for (int j = 0; j < SpreadMax; j++) 
		{
			if (i != j) 
				if ( VMath.Dist(FOutput[i](i),FInput[j](j))<FMaxRadius[0](0) )									
					FOutput[i](i)= 0-(FOutput[i](i)-FInput[j](j));
		}
	}
}

fixes the error.

but i guess then the behaviour is still not what you want to achieve, but that’s another topic

hi cat,

the error comes from the inner loop (the one over j). in the inner loop you access elements from 0 to SpreadMax-1 but you do that every iteration of the outer loop.
now think of the first iteration of the outer loop: it adds one element into the list, but then starts the inner loop which assumes that the list is already full.

so, fill the list before the outer loop, or just use FInput. and you can use FOutput directly as buffer to write to. this would be the c variable in the move away code.

so it could look like:

code(lang=csharp):
public void Evaluate(int SpreadMax)
{
FOutput.SliceCount = SpreadMax;

  	for (int i = 0; i < SpreadMax; i++)
  	{
  		FOutput[i](i) = new Vector3D();
  		
  		for (int j = 0; j < SpreadMax; j++) 
  		{
  			if (i != j) 
  			{
  				//then
  				if ( VMath.Dist(FInput[i](i),FInput[j](j))<FMaxRadius[0](0) )
  					//then
  					FOutput[i](i) = FOutput[i](i) + (FInput[i](i)-FInput[j](j))*0.01;
  			}
  		}
  	}
  }

i should probably reload the page sometimes… ;)

as woei, i was a bit puzzeled by the rule. but it think it works like this. it needed some scaling for the repulsion vector to look smooth and c in the pseudcode could change its value in the inner loop, so you have to reuse it.

and here is a handy shortcut for you: select a block of code, including some lines above it from the template code and press ctrl+i, it indents the code. ctrl+# is also cool…

Thanks guys :)
I shall carry on scratching my head at this …

So heres where I’ve got to, it works, kind of, a little mechanical looking, but MyFirstPlugins :)

A basic Boids (81.0 kB)