How to use Navigation2D and NavigationPolygonInstance with instanced scenes.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Desertdweller
:warning: Old Version Published before Godot 3 was released.

I have been working on a small project with enemies who will be able to pathfind around objects, and etc. I plan to have multiple enemies on the screen at one time, so I have the enemy scene instanced. The problem I am running into, is the fact that I cannot (as far as I am aware), use the Navigation2D node outside of the enemy scene. However, I cannot place the NavigationPolygonInstance in the enemy scene, because I would not be able to create it inside of my game world. This of course conflicts with the need for the NavigationPolygonInstance to be a child of the Navigation2D.

Any help would be greatly appreciated!

:bust_in_silhouette: Reply From: YeOldeDM

Why can’t you have your Navigation/Polygon as a part of the world your enemies are pathfinding around? I’m not even sure how it would work to have the navigation on the enemies…

All you need is to have some function on your enemy that references their world (assume the enemy is a direct child of the world), and returns the path from their position to wherever they’re aiming for.

(on the world)

func find_path_to_player(from_pos, to_pos):
	return get_node('Navigation').get_simple_path(from_pos,to_pos)

(on the enemy)

func get_path(to_point):
	path = get_parent().find_path(self.get_pos(), to_point)

From there, the enemy gets its path and figures out what to do with it from there.

Hm… I’ll give it a shot, thanks!

Desertdweller | 2017-02-16 23:27