C# modifying a List<T> within a loop?

Hello! This is some test code for and agent based script. The problem is I can’t .Add () to the list of agents after I first declare and populate it. I don’t get an error, but no output is generated. The problematic line (73 in the editor) is commented out. What am I doing wrong here?

Agentes.gh (9.3 KB)

---------------------------------- code: -----------------------------------------------

private void RunScript(List<Point3d> AgLoc, List<Vector3d> AgDir, int steps, ref object A) {
    List<Agent> agents = new List<Agent>();
    List<Polyline> trails = new List<Polyline>();
    Random rnd = new Random();
    Point3d pt = new Point3d();

    for (int i = 0; i < AgLoc.Count; i++){
      agents.Add(Agent.Spawn(AgLoc[i], AgDir[i]));
    }

    for (int i = 0; i < steps; i++){
      foreach (Agent ag in agents){
        ag.Move(Vector3d.ZAxis);
      }

      //spawn a radom agent each step (not working)
      pt = new Point3d(rnd.NextDouble(), rnd.NextDouble(), 0);
      //agents.Add(Agent.Spawn(pt, Vector3d.ZAxis));
    }

    foreach(Agent ag in agents){
      trails.Add(ag.Trail());
    }

    A = trails;
  }
public class Agent {
    //fields
    public Point3d Location;
    public Vector3d Direction;
    public List<Point3d> Hystory = new List<Point3d>();

    //constructors
    public Agent(){
      this.Location = new Point3d();
      this.Direction = new Vector3d();
    }

    public Agent(Point3d loc){
      this.Location = loc;
      this.Direction = new Vector3d();
    }

    public Agent(Point3d loc, Vector3d dir){
      this.Location = loc;
      this.Direction = dir;
    }

    //methods
    public static Agent Spawn(Point3d loc){
      Agent ag = new Agent(loc);
      return ag;
    }

    public static Agent Spawn(Point3d loc, Vector3d dir){
      Agent ag = new Agent(loc, dir);
      return ag;
    }

    public void Move(Vector3d dir){
      this.Hystory.Add(this.Location);
      this.Location = this.Location + dir;
    }

    public Polyline Trail(){
      Polyline poly = new Polyline(this.Hystory);
      return poly;
    }
  }

Doesn’t seem like you can in a foreach loop

True, but the addition to the list happens outside the foreach loop. For some reason .Remove() works fine…

It seems C# component can’t handle a list of polylines that contains empty polyline, You need to use a List<Curve> or a List<PolylineCurve> here.

private void RunScript(List<Point3d> AgLoc, List<Vector3d> AgDir, int steps, ref object A)
{
  var agents = new List<Agent>();
  var trails = new List<Curve>();
  var rnd = new Random();
  for (var i = 0; i < AgLoc.Count; i++)
    agents.Add(new Agent(AgLoc[i], AgDir[i]));
  for (var i = 0; i < steps; i++)
  {
    foreach (var ag in agents)
      ag.Move(Vector3d.ZAxis);
    var pt = new Point3d(rnd.NextDouble(), rnd.NextDouble(), 0);
    agents.Add(new Agent(pt, Vector3d.ZAxis));
  }
  foreach(var ag in agents)
    trails.Add(ag.History.ToNurbsCurve());
  A = trails;
}
//<Custom additional code> 
public class Agent
{
  public Point3d Location {get; private set;}
  public Vector3d Direction {get; private set;}
  public Polyline History {get; private set;}
  public Agent(Point3d loc, Vector3d dir)
  {
    Location = loc;
    Direction = dir;
    History = new Polyline(){loc};
  }
  public void Move(Vector3d dir)
  {
    Location = Location + dir;
    History.Add(Location);
  }
}

Agents.gh (6.0 KB)

1 Like

Thank you! That was it. Also, thanks for the help making the code cleaner. :raised_hands: