Idea of this code is that, few spheres(bees) would come to a common point from different location and will keep hovering over it. I am able to create behavior for one single sphere getting attracted towards the point, but when I am making array of an object, there is no error but output is not visible.

Why the output of c# component is giving "Script_Instance+(Mover)’’, it should give a list of surfaces(spheres). 01_Swarm bees.gh (15.2 KB)

Any help would be great! attaching GH file too.

// List<Mover> bees = new List<Mover>();
List<Mover> Allbees = new List<Mover>();
for(int i = 0; i < movers.Length; i++)
  movers[i] = new Mover();
  // bees.Add(movers[i]);
for(int i = 0;i < movers.Length;i++)
  movers[i].Display(radius, reset);
swarms = Allbees;



Mover movers = new Mover[5];

class PVector{
public double X;
public double Y;
//public double Z;

// constructor
public PVector(double x, double y)
  X = x;
  Y = y;
  //Z = z;

// create a method for vector addition
public void Vadd(PVector v)
  X = X + v.X;
  Y = Y + v.Y;
  //Z = Z + v.Z * zVelocity;
// create a method for vector subtraction
static public PVector Vsub(PVector v1, PVector v2)
  double X = v1.X - v2.X;
  double Y = v1.Y - v2.Y;
  // Z = Z - v.Z * zVelocity;
  return new PVector(X, Y);

// create a method for vector multiplication
public void Vmul(double scale)
  X = X * scale;
  Y = Y * scale;
  // Z = Z * scale;

// create a method for vector division
public void Vdiv(double scale)
  X = X / scale;
  Y = Y / scale;
  // Z = Z * scale;
// create a method for vector magnitude
public double Vmag()
  return Math.Pow(X * X + Y * Y, 1 / 2);

// create a method for vector length
public double VmagSq()
  return X * X + Y * Y;
// create a method for vector Normalization
public void Vnormalize()
  double m = Vmag();
  if(m != 0){
// create a method for vector magnitude limit
public void Vlimit(double max)
  if(VmagSq() > max * max ){
//Random Vector
static public PVector Random2d(){
  double vx; double vy;  PVector target;
  Random rnd = new Random();
  vx = rnd.NextDouble() * rnd.Next(-1, 2);
  vy = rnd.NextDouble() * rnd.Next(-1, 2);
  target = new PVector(vx, vy);
  return target;


// make another class “Mover” to oerate PVector dynamically

class Mover{
// accessible parameters
public PVector location;
public PVector velocity;
public PVector acceleration;
public double topSpeed;
//public Random rnd;

public Mover(){
  Random rnd = new Random();
  location = new PVector(rnd.Next(0, 50), rnd.Next(0, 50));
  velocity = new PVector(1.5, 3);
  acceleration = new PVector(-0.001, 0.01);
  topSpeed = 0.9;


//class METHODS (Update location Method)
public void Update(Point3d point){
  PVector attractor = new PVector(point.X, point.Y);
  PVector dir = PVector.Vsub(attractor, location);



  acceleration = dir;



// MEthod to display the object(swarm/sphere/circle/whatever)
public Sphere Display(double radius, bool reset){
  if(reset == false){
    return new Sphere(new Point3d(0, 0, 0), radius);
    return new Sphere(new Point3d(location.X, location.Y, 0), radius);

// define function for rectangle boundary
public Polyline Boundary(double width, double height)
  List<Point3d> rPts = new List<Point3d>();

  rPts.Add(new Point3d(0, 0, 0));
  rPts.Add(new Point3d(width, 0, 0));
  rPts.Add(new Point3d(width, height, 0));
  rPts.Add(new Point3d(0, height, 0));
  rPts.Add(new Point3d(0, 0, 0));

  Polyline pl = new Polyline(rPts);
  return pl;


I think it behaves just as expected, you are assigning an array of a custom class to your script output, which Grasshopper recognizes as Script_Instance+Mover. Grasshopper however does not know what to do with this object further.

I am not sure what you really want, if you want to output the spheres, add a method to your class which produces the sphere and call it for each Mover object, or construct it yourself by using the Mover classes properties.

If you want Grasshopper to understand the Mover object and know what to do with it, you need to create a custom parameter, which Grasshopper can handle. However this needs to be compiled in an .gha-File. There are quite many examples in this Forum how to do this (Create a custom type + gooWrapper + custom parameter).

1 Like

The core of the matter in these things is how the new objects are sampled in the (moving or static) collection.

Here’s a demo on that matter: Growing Collection shown is static (for clarity). A new Collection item is in fact a transforned (Plane To Plane) object with respect a new node added in an existed tetrahedron like topology List of nodes (Point3d). Is very very hard to guess the terahedron logic mind (unless you observe the connections [lines]).

This means:
(a) there’s no clash situations around
(b) the Collection looks 100% random > job done.

Note: When dealing with a growing Collection that should look random for each step try to deal with integers AND integer Lists bool ops AND connectivity Trees: that way the procedure is real-time no matter the amount of steps. For instance this Method is used for randomly adding nodes on that growing “Tetrahedron topology” goal mentioned:

1 Like