Execution Error with Pathfinding

There were definitely a few errors when I first ran the game, which appear to be race condition based.
image

I took a look at the Script Execution order and compared it to the course SEO… you’ve only set 2 in there, but they’re reversed from the way Hugo has them


Hugo’s is on the right. UnitManager is after UnitActionSystem in Hugo’s project.

I don’t think that’s the only problem, so I’ll be exploring further.

Hmmm the more I fix, the more gets weird…

One thing I noticed is that in Unit, you’re getting a null reference in GetAction() because something is calling GetAction before the baseActionArray is set in Awake(). I inserted a poor man’s Lazy trap here, first checking that baseActionArray is valid, and getting it if it isn’t.

    public T GetAction<T>() where T : BaseAction
    {
        if (baseActionArray == null)
        {
            baseActionArray = GetComponents<BaseAction>();
        }
        foreach (BaseAction baseAction in baseActionArray)
        {
            
            if(baseAction is T)
            {
                return (T)baseAction;
            }
        }
        return null;
    }

Next, after logging in GridObject when a unit enters and exits the GridObject, I noticed that the characters were leaving the new tile (?) and then entering the new tile…
This was a simple fix, and easier to catch in my Editor (JetBrains Rider) because Rider actually pointed out that oldGridPosition was not being used.
If you look at the call to LevelGrid to the UnitGridMovedPosition method, you’re using gridPosition and newGridPosition, but at this point, we’ve stashed the gridPosition into oldGridPosition and set gridPosition to newGridPosition. Simple change to use oldGridPosition, newGridPosition fixed that.

    private void Update()
    {

        GridPosition newGridPosition = LevelGrid.Instance.GetGridPostion(transform.position);
        if(newGridPosition != gridPosition)
        {
            GridPosition oldGridPosition = gridPosition;
            //unit change grid postion
            gridPosition = newGridPosition;

            LevelGrid.Instance.UnitMovedGridPosition(this, oldGridPosition, newGridPosition);

        }
    }

What I haven’t got a handle on is that one of the locations simply isn’t landable no matter what I try, and the strangeness in the Pathfinder.
I’ll need to dive deeper into this tomorrow.

So the odd mystery I was having yesterday was that the unit wasn’t updating it’s GridPosition when moving from point A to point B (well, it was sometimes). I solved the problem of returning to the starting GridPosition in the path by changing Unit.GetGridPosition() from

public GridPosition GetGridPosition()
{
     return gridPosition)
}

to

public GridPosition GetGridPosition()
{
    return LevelGrid.Instance.GetGridPosition(transform.position);
}

But, this is only masking a symptom, that being that the GridPosition is not being updated.
So then I turned to GridPosition itself, and I found that the Equals was

    public bool Equals(GridPosition other)
    {
        return this == other;
    }

Now my first thought was that in Update,

if(newGridPosition != gridPosition)

should be triggering a change every frame regardless, but for some reason this was not the case. It was generally not firing at all, In 2 movements, it didn’t trigger once!
Now the actual Equals(GridPosition other) should read

return x == other.x && z == other.z

When I made this change, grid position changes were properly registered each frame, and characters could move into abandoned positions. Additionally, characters didn’t return to the false starting gridPosition on subsequent moves, because they were properly registered on the GridPosition they were standing on.

Holy crap! I did not know there were so many things adding up. Thank you for the thorough dive into the code! This is awesome. I made the changes you provided and now my game is actually working! Thank you for all the time and the help.

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

Privacy & Terms