Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Connections - Can someone please explain how these work?
#4
Ok so after diving deep into the train example I finally isolated the code that figures out if the junction should be used or not.  I feel like I'm in the brain of a madman here but it's starting to make a bit of sense.  I'm still somewhat new to C# but I do get the hang of things quickly.  That said, perhaps you can help me make sense of this line of code that I found in the TrainCarManager.cs file:

Code:
       
void OnCPReached(CurvySplineMoveEventArgs e)
       {
           var jc = e.ControlPoint.GetMetadata<MDJunctionControl>();
           if (jc)
           {
               if (jc.UseJunction)
               {
                   e.Follow(e.ControlPoint.Connection.OtherControlPoints(e.ControlPoint)[0]);
                   // Set the controller to use the new spline
                   SplineController controller = (SplineController)e.Sender;
                   controller.Spline = e.Spline;
                   controller.RelativePosition = e.TF;
               }
           }
       }

So, I get that this is an event listener, and I notice that it's attached to the Spline controller of each car above via this method:

Code:
        void setController(SplineController c, CurvySpline spline, float speed)
        {
            c.Spline = spline;
            c.Speed = speed;
            c.OnControlPointReached.AddListenerOnce(OnCPReached); // RIGHT HERE
            c.OnEndReached.AddListenerOnce(CurvyDefaultEventHandler.UseFollowUpStatic);
        }

Cool.  So every control point is going to call "OnCPReached"..  Then I notice there's some metadata added on the junction which is just a bool toggle on or off, so let's not worry too much about that.  

What I want to know is what are each of these lines doing?  Especially that e.Follow one.  Of course, I know it's telling the connection to follow the "other" control point, and somehow e.ControlPoint will tell it to follow the other connection.  But how does it know that?!  I notice the [0] at the end there, is this an array?

                   e.Follow(e.ControlPoint.Connection.OtherControlPoints(e.ControlPoint)[0]);
                   // Set the controller to use the new spline
                   SplineController controller = (SplineController)e.Sender;
                   controller.Spline = e.Spline;
                   controller.RelativePosition = e.TF;

At a high level I get what's happening here, but I'd like to understand it more fundamentally since I plan to adapt this idea to my own purposes and I can't really do that until I look under the hood a bit more.

Thanks for the help!
Reply


Messages In This Thread
RE: Connections - Can someone please explain how these work? - by atomsinthevoid - 10-20-2017, 10:27 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Connections Problem Juton 3 16 03-06-2024, 10:41 AM
Last Post: _Aka_
  GUILayout.TextField:s dont work with CurveySplines Mike Danielsson 6 12 09-01-2023, 03:38 PM
Last Post: _Aka_
  Are connections pooled? Lupos 1 3 05-09-2023, 09:18 AM
Last Post: _Aka_
  How to merge intersecting meshes at spline connections? Reign_of_Light 2 6 03-20-2023, 08:36 AM
Last Post: Reign_of_Light

Forum Jump: