Map_get_random_point() not working

I’m following a tutorial on state machines with navigation. However, the target position shown in print() is always 0,0, and as a result, all my characters move towards global when starting the game. I’m unsure of what went wrong as i have followed the tutorial code exactly, yet I’m not getting a randomised target_position as shown in the tutorial.

code:

func _ready() -> void:
	call_deferred("character_setup")
	
func character_setup() -> void:
	await get_tree().physics_frame
	set_movement_target()
	
func set_movement_target() -> void:
	var target_position: Vector2 = NavigationServer2D.map_get_random_point(navigation_agent_2d.get_navigation_map(), navigation_agent_2d.navigation_layers, false)
	navigation_agent_2d.target_position = target_position
	print(target_position)

There’s been a similartopic posted by someone following the exact same tutorial, but i’m pretty sure set_movement_target() has been called in my case, seeing as the print statement is working?

some help would be appreciated. T-T thanks!

Check if navigation_agent_2d.get_navigation_map() and navigation_agent_2d.navigation_layers return the expected values.

They seem to? navigation_agent_2d.navigation_layers returns 1, which is correct, and get_navigation_map() returns RID(1288490188800), which is probably correct…?

Any errors reported in the debugger?

nope….

Then something might be wrong with your navigation setup. Hard to tell without more information.

huh…

set_movement_target()" works just fine if i put the function in

func _on_physics_process(_delta : float) -> void:
	
	if navigation_agent_2d.is_navigation_finished():
		set_movement_target()
		return
	var target_position: Vector2 = navigation_agent_2d.get_next_path_position()
	var target_direction: Vector2 = character.global_position.direction_to(target_position)
	character.velocity = target_direction * stats.speed
	character.move_and_slide()
	print(target_position)

the npc is able to navigate normally after heading to 0,0 after starting the game. Im assuming that there’s something wrong with it being called initially, but at the same time, the values for navigation_agent_2d.get_navigation_map(), navigation_agent_2d.navigation_layers are already there, so there’s no reason for it to be not executed properly…

There is a comment on that tutorial:

I have an error: upon starting, all the chickens walk towards the same point and then move randomly.

Then he followed up with:

I finally fixed the issue: I wasn’t allowing the area to load first. The solution is to put await get_tree().physics_frame in the setup and set a random position just above.