Grasshopper

algorithmic modeling for Rhino

C# -Initialize class objects in for loop - Persistent data

Hi guys,

I am wondering how to initialize n amount of class objects in an array and  keep persistent data. I think that I have the logic correctly in setting up my array, initializing all my class objects in a for loop, looping through them and calling all their methods (see below). The  problem is, that in every solution of the component, the data from the previous run is lost and the locations of the walkers are not updating properly. Attached is a simplified version of one of my random walker codes.

Any suggestions will be great.

Thank you

private void RunScript(bool run, int number, ref object A)
{

List<Point3d> walkerList = new List<Point3d>();

if(run)
{
rw = new RandomWalk[number];
for (int i = 0; i < rw.Length; i++)
{
rw[i] = new RandomWalk(Util.GetRandomPoint(0, 20, 0, 20, 0, 0), number);
rw[i].Move();
walkerList.Add(rw[i].Move());
}


Component.ExpireSolution(true);
}

else
{

Print("test");
}

A = walkerList;

}

// <Custom additional code>


public RandomWalk[] rw;


public class RandomWalk

Views: 1092

Attachments:

Replies to This Discussion

If I understand you correctly, maybe you should create the list in the additional code (static mode) , so it's not gonna be refreshed every iteration?

List<Point3d> walkerList = new List<Point3d>();

Hi Zuardin,

thanks for your reply, but your suggestion dosent work, when I test the output by only getting the last point int the list, its still jumping around in strange ways. Also I am not sure why you suggested to make the list static, if I am correct, making a variable static inside a class makes more sense than making it static as a stand alone varibale. I am actually trying to initialize multiple objects as you would do in processing, which is very straight foreword in my opinion. But I cant get it right here in C#

Nicholas

Hi Nicholas, 

Ahh I see..I think I misunderstood your explanation. Are you looking for something like this? However, I still didn't manage to maintain the random seed. Please see attached

Attachments:

I cleaned up a bit with the code, did some trick to keep constant random number and add some trails. Please see attached!

Best, 

Zuardin Akbar

Attachments:

Hi Zuardin, 

thank you for your reply, this is a very nice way to initialize any amount of objects within a loop and keeps the code very OOP in my opinion. Also for each object having its own random seed was an important pointer, otherwise they would be stuck together in all the simulation... lol.  I actually combined the 2 versions that you sent me, and cleaned it up a little more. You can see the attached file if you want.

Cheers,

Nicholas

Attachments:

Cool! Glad to see it runs properly now. I was thinking to put a boundary as well but then my wife already suspected my nerdiness so I had to leave the code as it is.. Lol. Can't wait to see the final stuff you're trying to achieve!

Cheers!

Actually my main goal with this was to understand how to initialize any amount of objects within a loop in the C# component in Grasshopper. It was important for me to get that step going right for efficient OOP in the next steps. So thank you for your help!

Cheers

Nicholas

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service