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;
}
}