C#_ Random Walker

Hello,
I have made a script in which point moves in random direction, However I want to have a point at every place when it moves. Can somebody show me how it can be done?
Thanks!

if (reset)
{
  Point3d wlk = new Point3d(0, 0, 0);
  A = wlk;
}

else
{
  Random rand = new Random();
  int choice = rand.Next(4);
  if(choice == 0)
  {
    x++;
  }
  else if(choice == 1)
  {
    x--;
  }
  else if(choice == 2)
  {
    y++;
  }
  else
  {
    y--;
  }
  Point3d wlk = new Point3d(x, y, 0);
  A = wlk;
}

}

//
int x = 0;
int y = 0;

Capture

RandomWalker.gh (6.0 KB)

you can put a data recorder on the output nodeā€¦
Also - i always see this - Use a Boolean Button, not a Toggle - you save yourself a lot of clicks in the long runā€¦
And then you can get rid of the ā€˜elseā€™

Thank You for making it. I understood the code except the method that your created.
Is it possible for you to explain me what exactly it is doing?

Grasshopper.Kernel.Special.GH_Timer timer;

public void FindTimer(){
foreach(IGH_DocumentObject obj in GrasshopperDocument.Objects){
if(obj is Grasshopper.Kernel.Special.GH_Timer){
Grasshopper.Kernel.Special.GH_Timer tim = obj as Grasshopper.Kernel.Special.GH_Timer;
if(tim.Targets.Contains(this.Component.InstanceGuid)){
timer = tim;
return;
}
}
}
}

Thank You for the suggestions!

To mee it looks like heā€™s looking for the Timer which is connected to this component, and assigns it to an internal variable in

public void FindTimer()
  {
    // Look for a Timer among all the components in the GH document
    foreach(var obj in GrasshopperDocument.Objects)
    {
      // Check if this component is of Timer type
      if(obj is Grasshopper.Kernel.Special.GH_Timer)
      {
        // yes, it was, make sure we treat it like a Timer type:
        var tim = obj as Grasshopper.Kernel.Special.GH_Timer;
        
        // If this particular Timer ("tim") is connected to this 
        // component (where this code resides), then assign that
        // Timer to the timer variable, and then quit ("return").
        if (tim.Targets.Contains(this.Component.InstanceGuid))
        {
          timer = tim;
          return;
        }
      }
    }
  }

Edit: The variable named ā€œtimerā€ was declared here, and thus accessible from the code in the main method ā€œRunScriptā€:

// Rolf

Thank You! Now it is very much clear.

Yes, another question I always have( may be because I am beginner), that how one would know to get a ā€œGH_Timerā€ from Grasshopper.Kernel.Special

In RhinoPython I used to simply search the component I needed in rhino script syntax. Is there anyway to find such components in c# grasshopper ?

There is no official way. This part of RhinoCommon (the namespace ā€œspecialā€) isnā€™t published. it is reserved only for initiated top level developers belonging to the Grand Lodge Of Grasshoppers (aka as GLOGs. :wink:

Just joking.

Iā€™d like to see also the Special namespace being publicly published.

// Rolf

GH1 has been organically built on the fly for several years, so it is not too tidy. But you can start to know the Grasshopper.dll using the autocomplete function of the code editor (when you put a ā€œ.ā€ after the namespace, like ā€œGrasshopper.ā€), or using reflection like this:

A = GrasshopperDocument.GetType().Assembly 
      .GetTypes() 
      .Where(t => t.Namespace == "Grasshopper.Kernel.Special") 
      .ToList();

If you want to use the components from .NET code, you must use Rhino6ā€™s NodeInCode function. Components (actually parameters) such as Slider, Panel, Toogleā€¦ belong to the Grasshopper.Kernel.Special namespace. The (black hexagonal background) parameters are in Grasshopper.Kernel.Parameters and the data types in Grasshopper.Kernel.Types. Most of the components are in their own .dll (.gha). The graphics/interface/controls/forms part is in Grasshopper.GUI.

EDIT:
To see all namespaces:

A = GrasshopperDocument.GetType().Assembly
.GetTypes()
.Select(t => t.Namespace)
.Distinct()
.OrderBy(n => n)
.ToList();

1 Like