Search code examples
c#listgrasshopperrhino3d

Resetting List to new quantity of objects


I'm having a problem with an accumulation of Point3ds in List. When I change the number of int agents (via a gui slider in grasshopper) the quantity keeps increasing rather than resetting to whatever the new quantity should be. I'm guessing that somewhere I should be re-initializing the list or clearing it everytime the value is changed? What would be the correct to do this?

        protected override void SolveInstance(IGH_DataAccess DA)
        {
            BoundingBox box = new BoundingBox(0.0, 0.0, 0.0, boundx, boundy, boundz);
            DA.SetData("Bounding Box", box);
            DA.SetData("Start", "The current trigger is set to " + started.ToString());

            // Initialize Agents
            for (int i = 0; i < agents; i++)
            {
                double xPos = RandomfromDouble(0.0, boundx);
                double yPos = RandomfromDouble(0.0, boundy);
                double zPos = RandomfromDouble(0.0, boundz);

                Point3d pos = new Point3d(xPos, yPos, zPos);        // Create Agent Start Position
                Vector3d vec = new Vector3d(xPos + RandomfromDouble(-360.00, 360.00), yPos + RandomfromDouble(-360.00, 360.00), zPos + RandomfromDouble(-360.00, 360.00));  // Create Agent Start Vector

                Agent agent = new Agent(pos, vec, alignment, separation, cohesion, neighborRadius);
                allAgents.Add(agent);

                DA.SetData("Debug", "# of Agents Created: " + allAgents.Count);
            }

            // Get agent positions
            List<Point3d> agentPositions = new List<Point3d>();
            List<Vector3d> agentVectors = new List<Vector3d>();

            agentPositions = allAgents.Select(agent => agent.Pos).ToList();
            agentVectors = allAgents.Select(agent => agent.Vec).ToList();

            DA.SetData("Agent Count", allAgents.Count);
            DA.SetDataList("Agent Points", agentPositions);
            DA.SetDataList("Agent Vectors", agentVectors);

            if (started)
            {
                DA.SetData("Start", "The current trigger is set to " + started.ToString());

                for (int i = 0; i < generations; i++)
                {
                    DA.SetData("Debug", "# of Generations: " + i);

                    foreach (Agent agent in allAgents)
                    {
                        DA.SetData("Debug", "# of Agents: " + i);

                        agent.UpdateAgent(allAgents);
                    }
                }
            }
            else if (!started)
            {
                DA.SetData("Start", "The current trigger is set to " + started.ToString());
                //return;
            }
        }

        public double RandomfromDouble(double from, double to)
        {
            double diff = Math.Abs(from - to);
            return rnd.NextDouble() * diff + from ;
        }

Solution

  • If I'm reading your code correctly, your issue is that the allAgents list keeps getting longer. As you guessed, that's because you're creating the list once, at the top, and then you only ever add to it, within the for loop that says // Initialize Agents.

    If your intent is to reset the list at this point, then before you enter the for loop I think you need to do allAgents.Clear(). This will empty the list, and then you loop through and add the new Agents within the loop.