My Pathfinding Looks Weird, how I align PoolVector2Array() arrays into a gridcellsize?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By The_Black_Chess_King

Reference: Godot V3.0.6

So I got my pathfinding from Navigation2D node working properly, however I would like to center the path points generated from the functionget_simple_path(startPosition, targetPosition, true). how I would do that?

Here’s the code I have so far:

for pathPointsNumber in pathPoints:
		var newPos = Vector2()
		newPos = pathPointsNumber.snapped(TilesetNav.cell_size)
		pathPoints.set(pathPointsNumber,newPos)

Gives an error: Invalid type in function 'set' in base 'PoolVector2Array'. Cannot convert argument 1 from Vector2 to int. I don’t understand, where’s the integer?

Also tried:

for pathPointsNumber in pathPoints:
            var newPos = Vector2()
            newPos = TilesetNav.world_to_map(pathPointsNumber)
    	    newPos = ((newPos * TilesetNav.cell_size.x) + Vector2(40,60))
            pathPoints.set(pathPointsNumber,newPos)

-I tried other methods but always ended up unable to assign the new position to the path points. How I edit all the indexes position of a PoolVector2Array generated with get_simple_path(startPosition, targetPosition, true) using a for code?

So let me get this straight. You have a PoolVector2Array generated by get_simple_path(), and you would like to center all of the elements in the PoolVector2Array to grid cells?

Ertain | 2018-10-15 00:09

Precisely, offset the coordinates of the path points to fit the center of a grid.

The_Black_Chess_King | 2018-10-15 02:53

For anyone following this issue, upon further experimentation the code that worked ended up like this:

- don’t know if this is the optimal way, but it worked so far

CustomFuncUpdatePath():

	pathPoints = navigationNode.get_simple_path(self.position, goal, true)
	var x = 0

	while x < pathPoints.size():
	
		for pathPointVector2 in pathPoints:
		
			var newPos = Vector2()
			newPos = TilesetNav.world_to_map(pathPointVector2)
			newPos = newPos * TilesetNav.cell_size.x #It's a square grid, so I only need to multiply by one value.

		
			pathPointVector2 = newPos
			pathPoints[x] = pathPointVector2 
		
			x += 1

Now I only need to handle some details in the calculations as the Navigation2D pathfinding is calculated by point. Issue related here: https://forum.godotengine.org/23519/

The_Black_Chess_King | 2018-10-15 12:48

:bust_in_silhouette: Reply From: SIsilicon

Here’s the code I have so far:

for pathPointsNumber in pathPoints:
var newPos = Vector2()
newPos = pathPointsNumber.snapped(TilesetNav.cell_size)
pathPoints.set(pathPointsNumber,newPos)
Gives an error: Invalid type in function ‘set’ in base ‘PoolVector2Array’. Cannot convert argument 1 from Vector2 to int. I don’t understand, where’s the integer?

That’s not the problem. The first argument of set is supposed to be an integer. You passed in pathPointsNumber(a Vector2) instead. You need to pass the current index of the array you are handling.

#In this case, ppn is actually an integer, rather than a value from the array itself.
for ppn in range(pathPoints.size()):
    var pathPoint = pathPoints[ppn] #so we'll have to get the point ourselves inside the loop.
    var newPos = pathPoint.snapped(TilesetNav.cell_size)
    pathPoints.set(ppn, newPos)