Enemy Mover not responding to Broadcast

Enemies are not following the recalculated path (path is being updated upon tower placement) and when the enemy wave has been completed the towers placed are ignored and the original path is reinstated. any idea on what i’m doing wrong here? I’ll do my best to be as clear and descriptive as I can.

1.) I’ve placed tower the path is recalculated but enemy mover does not listen
Relm Rush bs
2.) I place new tower and path is updated but ignores other towers placed
Screenshot 2021-07-08 155526
3.) When all enemies in the pool are looped through the recalculated path is set back to the first made and ignores all placed towers
Screenshot 2021-07-08 155642

here is a link to my code please excuse the mass changes I am still learning how to organize my backups properly

Try using some kind of
yourClass { … whatever code
public static UnityEngine.Events.UnityAction onTowerPlaced;

void Awake(){ onTowerPlaced += RecalculatePath; }
void OnDestroy() {onTowerPlaced -= RecalculatePath;}
… something}

in tower script then when you place tower use yourClass.onTowerPlaced();

1 Like

I don’t understand I’m sorry. Can you please take a look at my code and see what I might be doing wrong? I would really appreciate your help.

I think problem is here. every new placement you reset every node?
Maybe im wrong. And also when tower dies do you reset its node back to walkable? You can do it OnDisable.

PathFinder.cs

public List GetNewPath()
{
gridManager.ResetNodes();
BreadthFirstSearch();
BuildPath();
return BuildPath();
}

1 Like

It is not very handy to search errors without visual studio. Try look into where you could make error with isWalkable nodes. Find all reference and look if you exclude non walkable from path.

1 Like

Thank you for checking it out and for your input. No on ResetNodes(); I am doing the following
image
My towers aren’t dying it’s just right now when the algorithm is exploring the neighbors it’s not checking if there is a tower there and assigning the node ( isWalkable = false; ) I have tried to get the nodes talking to the tile to check if ( isplaceable == false ) inside of the tile and then assigning ( isWalkable = false ) in the foreach loop in the neighbors list

Debug the nodes which is added and on adding node change material color to see what is happening.
Make isWalkable be other color than non walkable. Problem is definitely there.

Check this part

““PathFinder.cs””

public bool WillBlockPath(Vector2Int Coordinates)
{
if (grid.ContainsKey(Coordinates))
{
bool previousState = grid[Coordinates].isWalkable;
grid[Coordinates].isWalkable = false;

        List<Node> newPath = GetNewPath();
        grid[Coordinates].isWalkable = previousState;

        if(newPath.Count <= 1)
        {
            GetNewPath();
            return true;
        }

    }

    return false;
}
1 Like

This is what I have going on it looks the same to me am I missing anything?
image

I really cant answer since i dont see what is happening there in your scene. Best for you is set material color for every node you search to that is walkable set to green and nonwalkable red and try place towers.

1 Like

Ye i mean check this part. Because there is you changing walkable to non walkable and then to walkable again. Could be case.

1 Like

RIght on man, I’ll do that thank you for your help :slight_smile:

1 Like

Found the issue and it’s working great now. I don’t understand completely why but when I built the new path I was resetting those nodes to walkable again by saying those coordinates were walkable again after the new path was retrieved. So I commented out the statement in case if I run into a problem down the road due to this I have it handy but as of now, it is recognizing where ballistas are previously placed and going around them now. Thank you for all your help!
image

1 Like

I have another question. When Broadcasting your message to the receivers how do you ensure that the pathfinder class is parented to the object pool class? I believed that may be my problem here is that my object pool class is actually parented to my pathfinder

Actually i have 2 year dev experience and never seen broadcast message used. You should really avoid it because its uncertain. Use Actions instead.
For example

public class MyClass{…
//Declare static action
public static UnityEngine.Events.UnityAction onPathShouldBeUpdated;

void Awake()
{
//subscribe method
onPathShouldBeUpdated += UpdatePath;
}
void OnDestroy()
{
//unsubscribe method
onPathShouldBeUpdated -= UpdatePath;
}

void UpdatePath(){

}

Then you can call this action from any class by calling MyClass.onPathShouldBeUpdated?.Invoke()

1 Like

Is this what you used instead for this functionality in your enemy mover? If so can I please see what it looks like in your code? DO you have a repo I could look at our maybe you can send me a screenshot?
This is the first time I have ever heard of an action.

I dont have this project anymore.

Awwww bummer ): thank you anyway I’ll do some research and see if I can figure it out. Thank you for pointing me in the right direction!

Look i edited previos post added example.

Just saw your input now thank you!

I just want to clarify, will this class inherit from MonoBehavior?

Also is UpdatePath in your code referring to the RecalculatePath method he shows in the project?

I really appreciate you helping me with this I apologize if these are elementary questions I still haven’t touched pure events in C#.

Yes it doesnt matter. You just need to be sure that you subscribed methods to action and unsubscribe them.
Action is like a variable that stores method. When you want to call those methods you invoke action.

For monobehavior it is better to subscribe in Start or Awake and unsub in OnDestroy

“Also is UpdatePath in your code referring to the RecalculatePath method he shows in the project?” -
as of this i cant tell you because its up to you.

2 Likes

Privacy & Terms