#A* Pathfinding (CodeMonkey) and Game Objects

1 messages · Page 1 of 1 (latest)

timber stag
#

Hi everyone!

I have followed the tutorial for the A* pathfinding algorithm from CodeMonkey and have got the script running. However, I went onto looking at the ECS and have seen that it is in experimental. I know that using ECS is more "efficient", however is there a way to pass gameObject data into the script or not? If there is, would anyone be so kind as to talk me through how to do it. I haven't found many resources on this. I will post the script below, sorry its long.

vital bear
#

You have to modify this (alot)

#

Right now it's literally just running 20 path calculations from a set start position to an end position every second

timber stag
#

yes I used that for testing the algorithm

vital bear
#

ok

#

Do you have a tilemap to act as the cells?

timber stag
#

not at the moment no

vital bear
#

or path nodes sry

timber stag
#

nope

vital bear
#

You have to divide your map into a grid and check which cells are walkable

timber stag
#

To make the cells not walkable I would have to manually do this is code though right?

#

by setting the bool

vital bear
#

Yes, but you can automate it using physics casts

#

so you basically run a collision check on every cell and see if there's another collider there

timber stag
#

Alright, so how would I do this with a tilemap then? Because do tilemaps have colliders?

vital bear
#

With a tilemap you can use GetTile too see which type of tile is at a specific position. Then you'd have a list of walkable and non-walkable tiles and compare it against that

#

You could also use the physics casts and attach a tilemap collider

timber stag
#

Perfect, okay. So I now know how to do that. What about the ECS then, should I just not use it and just use DOTS

vital bear
#

Don't use ECS

timber stag
#

Okay, I'll reframe from using it

vital bear
#

you can stick with Burst and Jobs since it wiil improve your performance

timber stag
#

Alright, I may be back with more questions soon lol

vital bear
#

its just a bit more complicated

#

alright

timber stag
timber stag
#

Imagine I have multiple of the same type of enemy in a scene. But each of them needs to path find from a different location to a different end location. How would I pass all these jobs into one scripts and then get them all to complete at once using multi threading?

vital bear
#

Just have the script on all enemies and start one job per. Complete them in LateUpdate or in a later frame to get them all to run in parallell

timber stag
#

So have a copy of the PathFinder script in every game object?

vital bear
#

Yes

#

You want to create the grid in some manager script though

#

And share it between all your pathfinders

timber stag
#

That seems a little inefficient?

#

having a copy on loads on gameobjects

vital bear
#

Why would it be? They barely contain any data

timber stag
#

true, good point

#

could I hold the pathfinder in the scene by itself like in a manager?

vital bear
#

You could

#

And each enemy sends in a "request"

timber stag
#

yes but I'm not sure how to go around it

vital bear
#

But that sounds like unnecessary complexity

#

Think about how many enemies you will want to have and the size and complexity of the map

#

Computers are fast

timber stag
#

I want maybe, 200+

vital bear
#

Oh thats actually a lot

timber stag
#

a few yeah

vital bear
#

Are they all pathfinding to one point?

timber stag
#

once they get in a certain range of the player then they start pathfinding to the player

#

and then I'm going to add a roam function

vital bear
#

Alright

#

I say try the performance of A*

#

but otherwise there is the flow field algorithm which works great when a lot of enemies move to one point

#

You could also do a mix of them

timber stag
#

I didn't want to limit myself with an algorithm that would only pathfind to 1 point which is why I went for A*

#

Anyway, how would I make my gameobject pass its position into the job and then spit out the path back to the hgameobject

vital bear
#

Make a float3 field in the job

timber stag
#

ive got this?

#

but I don't know how I'm going to retrieve the path

vital bear
#

Well you have to convert the position to a grid coordinatw

#

So yeah int2 sry

timber stag
#

thats fine I'll do a little rounding

vital bear
#

Then return a nativearray/list of int2

timber stag
#

so here can I change void in execute to a Native List of int2?

timber stag
#

Would this work?

timber stag
#

I still can't get this path outside of the job and its starting to annoy me lol

surreal hazel
#

See the example, you need to allocate a native container when setting up the job, pass it into the job, use it in the job and then you can use the result once the job completes

timber stag
#

I've got this and passed an array but idk what to do now. I feel like I've done everything

surreal hazel
#

you need to fill it with the paths

#

you are only setting the field to the new Temp one created in the job

#

don't do that

#

just send the results list into the functions and fill it, don't create a temp one

#

so use CalculatePathVoid() line 287

#

but don't do line 295 (or line 291) do results.Add(...