BFS Pathfinding

Why does the pathfinding script need to be attached to the object pooler? I want to understand so i can break this link. If i remove it and put it else where the broadcast and pathfind.notify dont seem to work any more.

Hi @Ashley_Rothery,
Unity’s “BroadcastMessage” only works with elements in the same parent/child structure (it sends a message to every component on the same object and all of its children).

Since we’re using this method to tell the EnemyMover to recalculate the path when the state of the world changes, the Pathfinder script needs to be attached to the parent of where we’re storing all of our enemies - i.e. the root of the object pool.

You can think of BroadcastMessage as a being a simplified, Unity specific, version of an event/delegate system, similar to how Invoke is a simplified version of a coroutine.

Events and Delegates are beyond the scope of this course but if you take the RPG Core Combat course then Sam provides a very nice introduction to them in the “Character Stats” section.

2 Likes

Ok thankyou for the reply.

I understand that a bit more now. Would it have the same effect if I applied it directly to the enemy prefab? Would that cause performance issues later on if I had a pathfinder script applied to every enemy prefab.

I will have a look at the RPG Core Combat section as I own that as well.

I can’t remember the specific details for the implementation I used during the course but I’ve got a feeling that having multiple pathfinders may bring up some issues due to the way that it interacts with the GridManager class.

You could certainly adapt the codebase to use multiple pathfinders but you really only want one in the scene anyway - Having any more would just be a waste of resources.
The pathfinder is generic enough that the enemy can just say “I’m currently here and I want to get over there”, meaning that every enemy can be given access to the same pathfinder to obtain their path.

If you let me know what functionality you’re trying to achieve I might be able to point you in the right direction.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms