UpdateGraphsNoBlock not working?

Hey there,

I’m trying to get UpdateGraphsNoBlock to work properly and it isn’t for some reason.

Here’s the code:

`

public bool valid = true;

void OnMouseDown ()
{
	Node StartNode;
	Node EndNode;
	GUO = new GraphUpdateObject(gameObject.collider.bounds);

	StartNode = AstarPath.active.GetNearest(StartPosition.transform.position).node;
	EndNode = AstarPath.active.GetNearest(EndPosition.transform.position).node;

        valid = GraphUpdateUtilities.UpdateGraphsNoBlock(GUO, StartNode, EndNode, true))

       if(valid == true )
       {
         //do stuff
       }
       else
          //do nothing
} 
`

The problem seems to be that the if statement is working as though the value is true, and THEN the bool turns to false after the check is made. (which is why I made the bool public so i could see if it was being changed at all)

So I suppose I need some type of delay on the if(valid == true ) first? a “wait for valid = GraphUpdateUtilities.UpdateGraphsNoBlock(GUO, StartNode, EndNode, true)) THEN if(valid == true )”?

Hi

You are using the return value of a method, the method cannot change the return value after it has completed. So the valid field is definitely set directly when you call the UpdateGraphsNoBlock method.

Ok, so after some testing I think there’s a different problem.
Even if valid is set to false by default, the if statement still is thrown as if it’s true.

So valid goes from false (at default), to true (when the if statement is called, back to false (after the method has finished) when this method is called. I’m not sure why.

EDIT:

Ok, apparently when the mouse is clicked this method is running twice. the first time it is running the UpdateGraphsNoBlock is returning true (when it should be false) the second time it runs it correctly runs false.

Ok, fixed the method running twice problem. But the first time around UpdateGraphsNoBlock is still returning true.

Hm, are you possibly creating a collider at the same time? Colliders take until the next fixed update to initialize themselves I believe.

Also, I think it shouldn’t affect anything if you are using one of the later versions, but try to set A* Inspector -> Settings -> Max Update Frequency to zero (0).

Hm, are you possibly creating a collider at the same time? Colliders take until the next fixed update to initialize themselves I believe.

I think this might be the issue. I created a box collider off screen and have it move to the gameObject and act as my “collider block”:

`
		BoxColliderCube.transform.position = gameObject.transform.position;
		GUO = new GraphUpdateObject(BoxColliderCube.collider.bounds); `

On first click it moves the box to the area, on second click it actually applies to the graph.

Even when I don’t do this and just use the gameObjects own collider, it still doesn’t work though:

GUO = new GraphUpdateObject(gameObject.collider.bounds);

The only time that GraphUpdateUtilities.UpdateGraphsNoBlock() is returning false is when the path is already blocked.

Somewhat unrelated question: Does the collider in GUO = new GraphUpdateObject(collider) need to be in the same layer as the collision test mask? Currently it IS in the same layer but I wasn’t sure if it was required or not.

ok,

I setup a debug that displays the bounds for the GUO GraphUpdateObject.bounds. The Center bounds is correct and the Extents seems to be correct for the gameObject collider.

Sooo if the GUO is “correct” (or at least its bounds is), is there a Debug print I can make out for the GraphUpdateUtilities.UpdateGraphsNoBlock (other than true/false) to see if it’s correct? Again, it’s only throwing false AFTER the path is blocked.

Somewhat unrelated question: Does the collider in GUO = new GraphUpdateObject(collider) need to be in the same layer as the collision test mask? Currently it IS in the same layer but I wasn't sure if it was required or not.
That is required if you use "updatePhysics" which will do a full recalculation of all nodes inside, and then it will only block the path if there actually is an obstacle there. But not if it is set to "modifyWalkability = true; setWalkability = false;" (i.e set all nodes to be unwalkable).

“After the path is blocked” do you mean the graph already reflects the change (i.e the nodes there are not walkable) or just after there has been a collider there for some time.

You can try adding something like
yield return 0 or yield return WaitForFixedUpdate (must be in a coroutine) to make sure the collider has time to initialize itself.

"After the path is blocked" do you mean the graph already reflects the change (i.e the nodes there are not walkable) or just after there has been a collider there for some time.

What happens is that GraphUpdateUtilities.UpdateGraphsNoBlock returns true when the path is blocked (when it should be false, part of the grid graph turns red) and on the second click it returns false.

Ok, Here’s what I’ve tried:

`
void OnMouseDown ()
{
         StartCoroutine(testthisthing());
}

IEnumerator testthisthing()
{
        yield return new WaitForFixedUpdate();
	Debug.Log("AT METHOD START: " + valid);

	GraphUpdateObject GUO;
	Node StartNode;
	Node EndNode;

	GUO = new GraphUpdateObject(gameObject.collider.bounds); 

	StartNode = AstarPath.active.GetNearest(EnemySpawn.transform.position).node;
	EndNode = AstarPath.active.GetNearest(enemyGoal.transform.position).node;
	
	valid= GraphUpdateUtilities.UpdateGraphsNoBlock(GUO, StartNode, EndNode, true);
	Debug.Log("AFTER UPDATEGRAPHS: " + valid);

	if(valid == true)
	{
		Debug.Log("it's true! It's still open!");
	}
	if(valid == false)
	{
		Debug.Log("it's false! It's blocked!");
	}
	Debug.Log("AT METHOD END: " + valid);
}
`

Still doesn’t work.

Holy crap I think I found the issue… This is so stupid of me. The bounds of GUO = new GraphUpdateObject(gameObject.collider.bounds); wasn’t large enough on the Y to register apparently. So doubling the Y collision scale and adding:

`void OnMouseDown ()
{
         StartCoroutine(testthisthing());
}

IEnumerator testthisthing()
{
	gameObject.collider.enabled = true;
        yield return new WaitForFixedUpdate();
	Debug.Log("AT METHOD START: " + valid);

	GraphUpdateObject GUO;
	Node StartNode;
	Node EndNode;

	GUO = new GraphUpdateObject(gameObject.collider.bounds); 

	StartNode = AstarPath.active.GetNearest(EnemySpawn.transform.position).node;
	EndNode = AstarPath.active.GetNearest(enemyGoal.transform.position).node;
	
	valid= GraphUpdateUtilities.UpdateGraphsNoBlock(GUO, StartNode, EndNode, true);
	Debug.Log("AFTER UPDATEGRAPHS: " + valid);

	if(valid == true)
	{
		Debug.Log("it's true! It's still open!");
	}
	if(valid == false)
	{
		Debug.Log("it's false! It's blocked!");
	}
	Debug.Log("AT METHOD END: " + valid);
	gameObject.collider.enabled = false;
}
`

This seems to work so far.

Ah, simple thing to miss.

Great that you found it!