Seeker check for shortcut path

Hey there!
I just started using A* pathfinding. It’s really great so far. I read a lot of the documents and still trying to learn some stuff.

Players and enemies can create tiles to extend the grid graph and create new walkable areas…
My question is, is there a way I can have the seeker check for a shorter path even if it’s blocked currently (IE no tiles). My tile creation code can check for empty spaces and create a new tile in that direction, but not sure how to have a seeker check for the path and possible choose that path over a longer one.
Here is quick screenshot explaining it
https://i.imgur.com/0qZPKfQ.png

Thanks in advance! Sorry If I missed something obvious here. I did a lot of searching already with no luck.

Off the top of my head, you may be able to accomplish this with Tags. Instead of making an explicit graph of connected nodes, the entire GridGraph would be filled in (Width * Height total nodes). Nodes the player can walk on would be tagged something like “PlayerWalkable”. In the Seeker component, make PlayerWalkable the only valid tag. This will force that object to find the longer (red) path.

Meanwhile, the enemy doesn’t care about PlayerWalkable tags, and can traverse the entire grid; that Seeker’s valid tags would be “Everything”. As it moves along, it could inspect the next node in Path.path[currentWaypoint + 1] and check if it has the PlayerWalkable tag. If the tag is missing, you could create the tile graphics, set that node as now PlayerWalkable for future pathfinding, and continue creeping up on the player!

You may be able to do the same approach but use Walkable/Unwalkable nodes instead of Tags, but if you want to extend it to different enemy types with different rules in the future, Tags seem like the more robust approach.

Disclaimer: Of course, I’m just another dev and this might not be the best solution! Just something to get ideas flowing :slight_smile:

2 Likes

Wow thanks for the in-depth explanation!
I was also thinking about tags, but was trying to find the best way to go about it. I was hoping to make enemies “dumb” or “smart” so some create a path and some just go all the way around. If the whole node is walkable how would that exactly work? I’m going to start playing around with it now. Thanks again!

I couldn’t have described it better myself. The approach that @torgie describes is what I would use.

Note that with tags you could also use a penalty for the tiles that are not yet constructed. So if it takes some extra time to construct a tile, you could use a higher cost for traversing that tile. The effect of this will be that the enemies will take existing routes unless the existing route is really long, then it will resort to building new tiles.

See also https://arongranberg.com/astar/docs/tags.php

2 Likes

I was going to say, it does slow down the enemy / player a bit to create tiles.
Awesome. Thanks so much for the help. :smiley:

1 Like

I ended up giving “everything” a cost and a tile cost of 0 and oh my gosh it works beautifully. Thanks so much. you guys are the best. I’m using the free version now for testing, but definitely going to buy this asset. :smiley: Thanks guys!

2 Likes

Awesome! I’m glad it works well :slight_smile:

1 Like

Quick question. How would I get the current waypoint. Im using AI lerp currently. I have everything else working but that.

Cheers!

Hi

The AILerp script navigates just by interpolating along the path, so it doesn’t really navigate “towards” something in the script.
You could use the closest node to the agent’s current position, that might work?

AstarPath.active.GetNearest(agent.position).node;

It is possible to get the end of the current segment it is moving on too, but that requires a little bit more code.

1 Like

Ah, I didn’t think about that! I was using currentWaypoint based off of the movement script I found here: https://arongranberg.com/astar/docs/custom_movement_script.html … I needed some pretty fine-grained control over movement in my game, so I went with a custom script. Good luck!