Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assigning "OnControlPointReached" and "OnEndReached" values during runtime?
#1
Hi there, 


I'm instantiating a spawned object during runtime, then adding a Spline Controller script to it and assigning its Spline via code.  This part is all working great.  However, under the Events area of the Spline Controller script (viewed in the Inspector), I need to then assign new values for "On Control Point Reached" and "On End Reached", both the listener itself and the Event Handler designation.  This where I get stuck.

I know I'm close... it should be something like this:



Code:
public int numberOfVehiclesinFleet = 4;
public List<GameObject> newVehicleFleet = new List<GameObject>;

void Start(){
     CreateNewVehicles();
}

void CreateNewVehicles(){

     for (int i = 0; i < numberOfVehiclesInFleet, i++){
          GameObject newVehicle = Instantiate (Resources.Load("Vehicle", typeof(GameObject)) as GameObject);
          newVehicleFleet.Add (newVehicle);
     }

     for (int i = 0; i < numberOfVehiclesInFleet; i++){
          newVehiclefleet[i].AddComponent<SplineController>();
          newVehicleFleet[i].AddComponent<CurvyEventHandler>();
          newVehicleFleet[i].GetComponent<SplineController>().enabled = true;
          newVehicleFleet[i].GetComponent<SplineController>().OnControlPointReached(??????????)
     }
}

The question marks are where I get stuck.  How do I add and assign a new "OnControlPointReached" and "OnEndReached" here?

I've also attached a photo of what I WANT it to look like.  In the meantime, I've created a workaround where I assign these values to the actual Resource so they are already assigned when they are Instantiated.  But this is what I'm trying to achieve (attached).

Any advice?  I'm stuck!

Thank you!


Attached Files Thumbnail(s)
   
Reply
#2
TLDR
Code:
newVehicleFleet[i].GetComponent<SplineController>().OnControlPointReached.AddListenerOnce(yourEventListner)


Those events are of type CurvySplineMoveEvent, which inherits from UnityEvent, a events class provided by Unity. So you can treat them as any other Unity event, or use the functionalities provided by CurvySplineMoveEvent, like AddListenerOnce(...) that make sure that you don't have the same listner attached multiple times.

You have an example usage in one of the example scenes, TrainCarManager, in the setController method
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply
#3
I know this is an old post but if anyone is still having the issue I solved this problem using the following:


private SplineController _splineController;
    [SerializeField] private GameObject _yourGameObject;

private void Awake() {
        _splineController = _yourGameObject.GetComponent<SplineController>();
    }

private void Start() {
        _splineController.OnControlPointReached.AddListenerOnce(EndReached);
    }

private void EndReached(CurvySplineMoveEventArgs e) {
        Debug.Log("End Reached");
       //put your code here
    }


Hope that helps Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Avoiding runtime GC allocations on control point position change Ell223 8 18 02-24-2024, 10:43 AM
Last Post: _Aka_
Wink Modifying Splines at Runtime artsung 1 7 01-30-2024, 09:40 AM
Last Post: _Aka_
  Is it possible to create a road texture at runtime? artsung 1 4 01-30-2024, 09:30 AM
Last Post: _Aka_
  Cant Generate Meshes At Runtime alms94 5 22 01-26-2024, 11:27 AM
Last Post: _Aka_

Forum Jump: