#Pathfinding with jobs

1 messages · Page 1 of 1 (latest)

dense basin
#

I have a pathfinding script that uses the burst compiler and job system. However, I do not want to implement ECS and want to use regular game objects.

Inside my execute method

public void Execute() 
{
  // Calculate the path and save it as a native array path
  NativeList<int2> path = CalculatePath(pathNodeArray, endNode);

}

I want to make a game object move using this path. What's the best way to get the value of this path after scheduling the job and calling
JobHandle.Complete()

dapper sentinel
#

Create a nativearray on the main threads before you schedule your job and pass it in when scheduling. Write results to it inside the job and read from it after the job is done. Might be able to use transformaccessarray to move things inside of a job

dense basin
#

Could u give an example of passing code in and out of jobs? I'm pretty new to using jobs sorry

dapper sentinel
dense basin
# dapper sentinel https://docs.unity3d.com/ScriptReference/Unity.Jobs.IJobFor.html has a good exam...

am i understanding this right?
in the main thread I can create a ``` public NativeList<int2> test = new NativeList<int2>(Allocator.Temp);


and then when i construct the job:

var job = new FindPathJob
{
startPosition = new int2(1,1),
endPosition = new int2(2,2),
path = test
};
JobHandle pathFindJobHandle = job.Schedule();
pathFindJobHandle.Complete();
foreach(int2 x in job.path)
Debug.Log(x.ToString());

#

i keep getting a deallocation error when i do this

faint olive
#

You cannot use the Temp applicator to pass to jobs. Use TempJob

dense basin
faint olive
#

You need to dispose of the collection when you're done with it

dense basin
#

Does that not work? Do I manually need to call dispose

faint olive
#

How would you access the collection outside of the job if it was deallocated when the job completed?

dense basin
#

Oh xd I forgot about that

#

So after I print out the nativearray I dispose of it right?

faint olive
#

Yes

dense basin
#

Tysm!