path from Navigation2D

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

I followed a tutorial from GDQuest regarding path description from Navigation 2D node.
I declared a variable path as a PoolVector2Array as given in the tutorial.
I also declared an onready variable character which is supposed to move along
a defined Navigation tilemap. This was also as per the tutorial guideline.
I then assigned the path of the character through this assignment:

var new_path = nav_2d.get_simple_path(character.global_position, event.global_position)
****
character.path = new_path

nav_2d is the refers to the node Navigation2D. I get the following error, however.
Invalid set index ‘path’ (on base: ‘Sprite’) with value of type ‘PoolVector2Array’.

The diversions from the tutorial are these:
-in the tutorial only the _unhandled_input(event: InputEvent) → void is in the script that extends a Node. The _process function and the user defined movement function is in the script that extends a Sprite
-in my code, I have inserted the _process function , the user defined movement function, and the unhandled_input(event: InputEvent) → void function in a script that extends a Node.

Are the diversions from the script the reason that I am getting the error? I have followed the tutorial code exactly in a test project and it is working fine there.

The reason I have deviated from the tutorial code is that I want more than one character to move along Navigation2D paths. The way I want to achieve this is through two touchscreen buttons where each makes a boolean true and then a particular character is made to move along the Navigation path.
Please help.

:bust_in_silhouette: Reply From: Fgico

I’m not a Godot expert so I might be mistaken, but the problem might be that in the line:
character.path = new_path
it tries to place a reference to a PoolVector2Array into a variable that’s declared to only directly store PoolVector2Array
you could try typing:

charachter.path = PoolVector2Array(new_path)

to clone the new_path array into charachter.path
again, I might be absolutely wrong, so take this with a grain of salt,
also posting the script of the sprite node might help people more expert than me to help you

Thanks for your answer. Have tried your suggestion. It has given the same error.
I am inserting my code here. Will be grateful if anyone can help out on this.

As I said earlier, I get this error:

Invalid set index ‘path’ (on base: ‘Sprite’) with value of type ‘PoolVector2Array’.

func _unhandled_input(event: InputEvent) -> void:
if not event is InputEventMouseButton:
	return
if not event.is_pressed():
	return
if bear1:
	
	var new_path = nav_2d.get_simple_path(character.global_position, event.global_position)
	line_2d.points = new_path
	character.path = new_path
if bear2:
	var new_path = nav_2d.get_simple_path(character1.global_position, event.global_position)
	line_2d.points = new_path
	character.path = new_path

func _process(delta):
var move_distance = speed * delta
move_along_path(move_distance)

func move_along_path(distance: float) -> void:
	var start_point = $Bear1/Character.position
	print(path.size())
	for i in range(path.size()):
		var distance_to_next = start_point.distance_to(path[0])
		if distance <= distance_to_next and distance > 0.0:
			$Bear1/Character.position = start_point.linear_interpolate(path[0], distance / distance_to_next)
			break

	elif distance < 0.0:
		$Bear1/Character.position = path[0]
		set_process(false)
		break
	distance -= distance_to_next
	start_point = path[0]
	path.remove(0)

func set_path(value : PoolVector2Array) -> void:
path = value
if value.size() == 0:
	return
set_process(true)

func _on_TouchScreenButton_released():
bear1 = true

ashish | 2020-12-21 11:39

Hasn’t the sprite you are trying to move a script of his own? I meant to ask for that script, sorry if I have’nt been clear

Fgico | 2020-12-21 21:15