[solved] Error: Assigning PackedVector2Array to Array[Vector2]

So I am following Mantequilla’s Colony Sim tutorial and the code has given me this error:

Trying to assign a value of type “PackedVector2Array” to a variable of type “Array[Vector2]”.

This error shows up on line 37 of the unit script:
path = pf.getPath(pos, clicked)

I am still fairly new to Godot, so I don’t really understand what this means. I have followed he tutorial closely and it didn’t mention this at all. Here’s the code that is related:

Pathfinding Script:

class_name Pathfinder
extends Node

var aStar = AStar2D.new()
@onready var main = get_tree().root.get_node("Main")
@onready var grid: Grid = main.get_node("Grid")

const DIRECTIONS = [Vector2.UP, Vector2.DOWN, Vector2.LEFT, Vector2.RIGHT]

func getPath(_pointA: Vector2, _pointB: Vector2):
	var aID = getPointID(_pointA)
	var bID = getPointID(_pointB)
	return aStar.get_point_path(aID, bID)

func initialize():
	addPoints()
	connectAllPoints()

func addPoints():
	var curID = 0
	for point in grid.grid:
		aStar.add_point(curID, grid.gridToWorld(point))
		curID += 1

func connectAllPoints():
	for point in grid.grid:
		connectPoint(point)

func connectPoint(_point: Vector2):
	var _pointID = getPointID(_point)
	for direction in DIRECTIONS:
		var neighbor = _point + direction
		var neighborID = getPointID(neighbor)
		if grid.grid.has(neighbor) and grid.grid[neighbor] == null: #change later to see if navigatable
			aStar.connect_points(_pointID, neighborID)

func disconnectPoint(_point: Vector2):
	var _pointID = getPointID(_point)
	for direction in DIRECTIONS:
		var neighbor = _point + direction
		var neighborID = getPointID(neighbor)
		aStar.disconnect_points(_pointID, neighborID)

func getPointID(gridPoint: Vector2) -> int:
	return aStar.get_closest_point(grid.gridToWorld(gridPoint))

func getWorldID(worldPoint: Vector2) -> int:
	return aStar.get_closest_point(worldPoint)

func getIDWorldPos(_id: int) -> Vector2:
	return aStar.get_point_position(_id)

func getIDGridPos(_id: int) -> Vector2:
	var worldPos = getIDWorldPos(_id)
	return grid.worldToGrid(worldPos)

Unit Script:

class_name Unit
extends Area2D

@onready var main = get_tree().root.get_node("Main")
@onready var grid: Grid = main.get_node("Grid")
@onready var pf: Pathfinder = grid.get_node("Pathfinding")

var data: UnitData
var speed = 100

var path: Array[Vector2]
var pos: Vector2 :
	get:
		return pos
	set(value):
		pos = value

func _ready():
	path = [Vector2(1,0), Vector2(1,1), Vector2(1,2), Vector2(2,2)]

func _process(delta):
	move(delta)

func move(delta):
	if path.size() > 0:
		if position.distance_to(grid.gridToWorld(path[0])) < 5:
			position = grid.gridToWorld(path[0])
			pos = path[0]
			path.pop_front()
		else:
			position += (grid.gridToWorld(path[0]) - position).normalized() * speed * delta

func _input(event):
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
		if event.pressed:
			var clicked = grid.worldToGrid(get_global_mouse_position())
			path = pf.getPath(pos, clicked)

Grid Script:

class_name Grid
extends Node2D

@export var width: int = 12
@export var height: int = 12
@export var cell_size: int = 128
@export var show_debug: bool = false

var grid: Dictionary = {}

func generateGrid():
	for x in width:
		for y in height:
			grid[Vector2(x,y)] = null
			if show_debug:
				var rect = ReferenceRect.new()
				rect.position = gridToWorld(Vector2(x,y))
				rect.size = Vector2(cell_size, cell_size)
				rect.editor_only = false
				$Debug.add_child(rect)
				var label = Label.new()
				label.position = gridToWorld(Vector2(x,y))
				label.text = str(Vector2(x,y))
				$Debug.add_child(label)

func gridToWorld(_pos: Vector2) -> Vector2:
	return _pos * cell_size

func worldToGrid(_pos: Vector2) -> Vector2:
	return floor(_pos / cell_size)

The error is self explanatory, you’re trying to assign a variable that’s different from the type you specified for this variable:

The : Array[Vector2] means this variable only can hold a normal array typed to accept only Vector2


But this function return a PackedVector2Array that’s a specialized array optimized to hold Vector2, both can only hold Vector2 but the packed version is different from the normal version and incompatible types.

You should either change the variable to be a PackedVector2Array (change var path: Array[Vector2] to var path: PackedVector2Array) or just untype the variable (remove the : Array[Vector2])

Thank you so much, I’ll fix it right away

No problem, just mark the question as solved