get Path in callback function
« on: August 14, 2015, 09:19:38 AM »
Hi.

I'm trying to work out how to access the Path once I have used a callback function. The new function won't have scope of the Tween created so I'd like to know how to access it. I would like to be able to set a waypoint dynamically to give a random look using the gotowaypoint function but obviously need a handle on the Tween first.

Your help is appreciated.


// taken from the supplied sample code

using UnityEngine;
using System.Collections;
using DG.Tweening;

public class Paths : MonoBehaviour
{
   public Transform target;
   public PathType pathType = PathType.CatmullRom;

   Vector3[] waypoints = new[] {
      new Vector3(-3.48f,-6.358611f,0f),
      new Vector3(-3.02f,-6.14f,0f),
      new Vector3(-2.348492f,-6.003549f,0f),
      new Vector3(-1.919789f,-5.918438f,0f),
      new Vector3(-1.533964f,-5.918437f,0f),
      new Vector3(-1.276615f,-5.807039f,0f),
      new Vector3(-0.7315713f,-5.847458f,0f),
      new Vector3(-0.2636646f,-5.965012f,0f),
      new Vector3(0.2385418f,-6.102207f,0f),
      new Vector3(0.5094132f,-6.184325f,0f),
      new Vector3(0.8023798f,-6.183298f,0f)
   };

void Start()
   {

      Tween t = target.DOPath(waypoints, 4, pathType)
         .SetOptions(true)
         .SetLookAt(0.001f)
         .SetId("supertween")
         .OnWaypointChange(MyCallback);
             t.SetEase(Ease.Linear).SetLoops(-1);
   }

void MyCallback(int a) {
 // how to access the Tween/Path in this function?
}

*

Daniele

  • Dr. Admin, I presume
  • *****
  • 378
    • View Profile
    • Demigiant
Re: get Path in callback function
« Reply #1 on: August 14, 2015, 01:12:23 PM »
Hi,

In this specific case it might be a little complicated, unless you can store a reference to the tween in the class. Is that something you can do? Like:
Code: [Select]
public class Paths : MonoBehaviour
{
   public Transform target;
   public PathType pathType = PathType.CatmullRom;
   Tween t;

Re: get Path in callback function
« Reply #2 on: August 14, 2015, 06:59:14 PM »
Thanks Daniele for your reply.

Would there be another purpose of using the callback other than doing something with the Tween? I'm wondering if I'm looking at things the wrong way?


*

Daniele

  • Dr. Admin, I presume
  • *****
  • 378
    • View Profile
    • Demigiant
Re: get Path in callback function
« Reply #3 on: August 15, 2015, 12:54:48 AM »
Most often that callback is used to do other stuff on waypoints. Like change your target's color when a given waypoint is reached, or warn some other player.

By the way, I realized that you can pass your tween's reference, but you'll have to use a lambda/anonymous-function, so that you can access the tween reference that was created inside the method closure:
Code: [Select]
myTween.OnWaypointChange(()=> {
   // DO something with waypoints and myTween
});
Remember that in this case you'll have to stop the chaining before adding the OnWaypointChange callback, so that the myTween reference is fully assigned.

Re: get Path in callback function
« Reply #4 on: August 15, 2015, 01:58:45 PM »
Thanks again Daniele. I'll look into the lambda function. I've got it working at the moment using your suggestion of a class variables to hold the tween instance.