10-20-2017, 10:27 PM
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:
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:
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!
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!