Support Forum

Tower Defense graph updates

Looking for some assistance on how to proceed with a tower defense game. What is the correct way to build towers on a grid graph, to keep UpdateGraphsNoBlock working?

Here is my test case. I’m doing a UpdateGraphsNoBlock check on my build cursor, that changes between blue and red for valid and invalid placement. UpdateGraphsNoBlock seems to be giving random results.

Here is the code I use to place a tower. Should I be using dynamic objects? I’m using physics colliders here with update physics set to true. Should I be modifying walkability directly?

    void PlaceTowerOnGround()
    {
        m_collider.enabled = true;
        m_towerGraphUpdate.updatePhysics = true;
        m_towerGraphUpdate.bounds = m_collider.bounds;
        AstarPath.active.UpdateGraphs(m_towerGraphUpdate);
    }

    public void RemoveTower()
    {
        m_towerGraphUpdate.updatePhysics = true;
        m_collider.enabled = false;
        AstarPath.active.UpdateGraphs(m_towerGraphUpdate);
    }

Here is the cursor build placement check.

        //Cursor graph update node, modify walkability during checks
        m_cursorUpdateBounds = new GraphUpdateObject(m_visualCursorCollider.bounds);
        m_cursorUpdateBounds.modifyWalkability = true;
        m_cursorUpdateBounds.setWalkability = false;

       ........

bool CheckValidBuildPlacement()
    {
        //Check if current location is blocked by another tower already
        if (Physics.CheckBox(m_visualCursorCollider.bounds.center, m_visualCursorCollider.bounds.extents / 2f))
        {
            return false;
        }
        m_buildCheckTime = Time.time + m_buildCheckCooldown;
        m_cursorUpdateBounds.bounds = m_visualCursorCollider.bounds;

        //Check if current location will block pathing between start and end nodes
        if (GraphUpdateUtilities.UpdateGraphsNoBlock(m_cursorUpdateBounds, m_buildArea.GetSpawnNode(), m_buildArea.GetGoalNode(), true))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

Let me know.

Hi

Just to double check, try to call Physics.SyncTransforms() after you set collider.enabled.

I put one after each collider enable change, and one before UpdateGraphsNoBlock method. Same issue.

What are you using this for?

m_cursorUpdateBounds = new GraphUpdateObject(m_visualCursorCollider.bounds);
        m_cursorUpdateBounds.modifyWalkability = true;
        m_cursorUpdateBounds.setWalkability = false;

The towers seem to affect the walkability using physics directly, so why do you need to modify the walkability manually like this?

Mainly because I didn’t want the units walking around to be blocked by my build cursor. So the collider is disabled on the cursor. So I am changing the walkability instead?

I suppose I could add a special layer for the build cursor, that doesn’t collide with the units layer, but blocks navigation?

So my build cursor, it shows the tower you are about build and you can drag your cursor around to see valid build placements.

I see. If I turn on the collider on my build cursor it properly works. So I can get rid of the walkability set?

So it starts breaking after I delete towers. Using the RemoveTower code above with sync transforms after enable is set to false.

I can place 3 towers to increase the maze, UpdateGraphsNoBlock correctly works. But removing towers, it randomly breaks and gives random results for UpdateGraphsNoBlock

I can provide my repo if it helps. I’m also open to updating the graph without physics if needed. Just looking for a way to update the graph with tower placements and removals. And be able to check if a build locations will block nodes using the UpdateGraphsNoBlock with revert set to true.

Let me know.

Sorry for a bunch of posts. But I’m kinda just winging it trying to figure out how to get things working. I ended up putting a AstarPath.active.FlushGraphUpdates() before my UpdateGraphsNoBlock call. Seems to have resolved my issues with it not working.

So I guess that’s it for now. If you have recommendations on the correct way to do this let me know.

Hi

Mainly because I didn’t want the units walking around to be blocked by my build cursor. So the collider is disabled on the cursor. So I am changing the walkability instead?

I think a better way is to just enable collision temporarily, run UpdateGraphsNoBlock and then disable collision again. That way it will be more accurate.

AstarPath.active.FlushGraphUpdates()

Hmm, that’s odd. Maybe some other graph update is interfering. Does anything change if you change A* Inspector -> Settings -> Max Graph Update Frequency?

I don’t see that option.

Here is my Pathfinder > Settings

SettingsPath

**EDIT
I see the value you are looking for is renamed to Batch Graph Updates. If I turn it on and set it to .2 seconds as the default. The issue still occurs, unless I have the FlushGraphUpdates in there.

And just to reiterate the issue since I have posted so much. When I delete towers the UpdateGraphsNoBlock method calls that follow are randomly incorrect. Calling AstarPath.active.FlushGraphUpdates prior to the no block call resolves the issue.