Godot 3 -> 4 | AStar3D Issues

Godot Version

4.2.2

Question

Sorry if these are simple issues. I’m new to Godot and still struggling to wrap my head around the documentation and documentation changes from v3 to v4.

I’ve been following Miziziziz’s tutorial on using AStar3D with gridmaps for Godot 3 but ran into some issues when translating it to Godot 4, namely with get_path, move_and_slide, and .intersect_ray.

Code snippets with their corresponding errors are attached below:

extends Node3D

var all_points = {}
var astar = null
@onready var gridmap = $GridMap
func _ready():
	astar = AStar3D.new()
	var cells = gridmap.get_used_cells()
	for cell in cells:
		var ind = astar.get_available_point_id()
		astar.add_point(ind, gridmap.map_to_world(cell.x, cell.y, cell.z))
		all_points[v3_to_index(cell)] = ind
	for cell in cells:
		for x in [-1, 0, 1]:
			for y in [-1, 0, 1]:
				for z in [-1, 0, 1]:
					var v3 = Vector3(x, y, z)
					if v3 == Vector3(0, 0, 0):
						continue
					if v3_to_index(v3 + cell) in all_points:
						var ind1 = all_points[v3_to_index(cell)]
						var ind2 = all_points[v3_to_index(cell + v3)]
						if !astar.are_points_connected(ind1, ind2):
							astar.connect_points(ind1, ind2, true)

func v3_to_index(v3):
	return str(int(round(v3.x))) + "," + str(int(round(v3.y))) + "," + str(int(round(v3.z)))

func get_path(start, end):
	var gm_start = v3_to_index(gridmap.world_to_map(start))
	var gm_end = v3_to_index(gridmap.world_to_map(end))
	var start_id = 0
	var end_id = 0
	if gm_start in all_points:
		start_id = all_points[gm_start]
	else:
		start_id = astar.get_closest_point(start)
	if gm_end in all_points:
		end_id = all_points[gm_end]
	else:
		end_id = astar.get_closest_point(end)
	return astar.get_point_path(start_id, end_id)

Error at (29, 1): The function signature doesn’t match the parent. Parent signature is “get_path() → NodePath”.

extends CharacterBody3D

var path = []
var path_ind = 0
const move_speed = 5
@onready var amap = get_parent()

func _ready():
	add_to_group("Units")

func _physics_process(delta):
	if path_ind < path.size():
		var move_vec = (path[path_ind] - global_transform.origin)
		if move_vec.length() < 0.1:
			path_ind += 1
		else:
			move_and_slide(move_vec.normalized() * move_speed, Vector3(0, 1, 0))

func move_to(target_pos):
	path = amap.get_path(global_transform.origin, target_pos)
	path_ind = 0

Error at (17, 28): Too many arguments for “Move_and_slide()” call. Expected at most 0 but received 2.

Error at (20, 26): Too many arguments for “get_path()” call. Expected at most 0 but received 2.

extends Camera3D

const ray_length = 1000

func _input(event):
	if event is InputEventMouseButton and event.pressed and event.button_index == 1:
		var from = project_ray_origin(event.position)
		var to = from + project_ray_origin(event.position) * ray_length
		var space_state = get_world_3d().direct_space_state
		var result = space_state.intersect_ray(from, to, [], 1)
		if result:
			get_tree().call_group("Units", "move_to", result.position)

Error at (10, 54): Too many arguments for “intersect_ray()” call. Expected at most 1 but received 4.

From the errors, I’m guessing it’s mostly an issue with arguments, but can anyone explain why these are occurring and how to fix them?

“Error at (29, 1): The function signature doesn’t match the parent. Parent signature is “get_path() → NodePath”.”

When you extend another class, it comes with a lot of functions you can use. One of those functions is apparently get_path(node path). Basically, you can’t use a function which has the same signature but a different argument. All you need to do is rename it to something else and it should be all right like get_astarpath(start, end) or if it’s an astar function, you name astar.get_path(start, end) after you name an astar variable (see docs)

Error at (17, 28): Too many arguments for “Move_and_slide()” call. Expected at most 0 but received 2.

Move and slide no longer takes arguments. You now name a variable called velocity and adjust it as you wish, then call move_and_slide().

You should figure out the rest, just look at the latest docs concerning the function.

2 Likes

Thanks for the explanation!