AnimationJobBinder Update not updating job values.

So I created my AnimationJobBinder, and I do some calculations in the update function and update the job’s value.

I have a job like this:

public struct SampleJob : IWeightedAnimationJob
{
    public Vector3 somePos;

    public void ProcessAnimation(AnimationStream stream)
    {
        // Fancy stuff with somePos
    }

    public void ProcessRootMotion(AnimationStream stream)
    {
    }

    public FloatProperty jobWeight { get; set; }
}

And AnimationJobBinder like this:

    public class SampleJobBinder : AnimationJobBinder<SampleJob, SampleData>
    {
        public override SampleJob Create(Animator animator, ref SampleData data, Component component)
        {
            // Some init
            return new SampleJob
            {
                somePos = Vector3.zero
            };
        }

        public override void Update(SampleJob job, ref SampleData data)
        {
            job.somePos = Vector3.one * Random.Range(-6f, 6f);
        }

        public override void Destroy(SampleJob job)
        {
        }
    }

Whenever I run this job’s value is always zero. This means that somePos is always equal to the initial value. How can I update it in runtime?

Hi,

The Update function in AnimationJobBinder is called with a copy of your job (all C# job containers are structs). Therefore, changing somePos in this instance will only modify the copy and not the job evaluated in the PlayableGraph.

To change this value, you would need to register it in a NativeArray a little bit like so:

public struct SampleJob : IWeightedAnimationJob
{
    public NativeArray<Vector3> somePos;
    public void ProcessAnimation(AnimationStream stream)
    {
        // Fancy stuff with somePos[0]
    }
    public void ProcessRootMotion(AnimationStream stream)
    {
    }
    public FloatProperty jobWeight { get; set; }
}
    public class SampleJobBinder : AnimationJobBinder<SampleJob, SampleData>
    {
        public override SampleJob Create(Animator animator, ref SampleData data, Component component)
        {
            // Some init
            return new SampleJob
            {
                somePos = new NativeArray<Vector3>(new Vector3[] {Vector3.zero}, Allocator.Persistent);
            };
        }
        public override void Update(SampleJob job, ref SampleData data)
        {
            job.somePos[0] = Vector3.one * Random.Range(-6f, 6f);
        }
        public override void Destroy(SampleJob job)
        {
        }
    }
1 Like

Is there a better way to do this, besides using NativeArray? NativeArray isn’t very readable. Shouldn’t FloatProperty and similar work the same way?