get_simple_path.........isn't working

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

In the create_path(): function. Its Telling me that get_simple_path isn’t working anymore and i can’t find a function to replace it.

extends CharacterBody2D

var SPEED = 30
var motion = Vector2.ZERO

var path: Array = []
var NavigationNode = null
var player = null

func _ready():
    if get_tree().has_group("Nav"):
	    NavigationNode = get_tree().get_nodes_in_group("Nav")[0]
    if get_tree().has_group("Player"):
	    player = get_tree().get_nodes_in_group("Player")[0]

func _physics_process(delta):
    create_path()
    goto()

func create_path():
    if NavigationNode != null and player != null:
	    path = NavigationNode.get_simple_path(global_position, player.position, false)

	
func goto():
    if path.size() > 0:
	    velocity = global_position.direction_to(path[1]) * SPEED
	    move_and_slide()
:bust_in_silhouette: Reply From: Adrasteon

You can check the doc to verify if a function is deprecated or not: https://docs.godotengine.org/fr/stable/classes/class_navigation2d.html#class-navigation2d-method-get-simple-path

It seems that get_simple_path is obsolete and should be replaced by Navigation2DServer.map_get_path

It’s Tellin me that map get path() is a nonexistent function:

func create_path():
    if NavigationNode != null and player != null:
	    path = NavigationNode.map_get_path(global_position, player.position, false)

javrocks | 2023-04-20 06:36

It seems that this line of code is the one you need: NavigationServer2D.map_get_path(get_world_2d().get_navigation_map(), global_position, player.position, true)

Can you confirm that it works ?

Adrasteon | 2023-04-20 16:04

No nothing seems to be happening.
To give u some additional information: I have my Navigation Link2D in my World scene, and a tile map as the child of Navigation Link2D. In the enemy scene is where im writing the script. After Enemy and player are instanced into World scene I need the Enemy to navigate around the tiles towards the player

extends CharacterBody2D

var SPEED = 30

var path: Array = []
var NavigationNode = null
var player = null
var Map = null

func _ready():
    if get_tree().has_group("Nav"):
	    NavigationNode = get_tree().get_nodes_in_group("Nav")[0]
    if get_tree().has_group("Player"):
	    player = get_tree().get_nodes_in_group("Player")[0]
    if get_tree().has_group("Map"):
	    Map = get_tree().get_nodes_in_group("TileMap")[0]

func _physics_process(delta):
    create_path()
    goto()

func create_path():
    if NavigationNode != null and player != null:
	    path = 
NavigationServer2D.map_get_path(get_world_2d().get_navigation_map(), global_position, player.position, true)
	
	
func goto():
    if path.size() > 0:
	    velocity = global_position.direction_to(path[1]) * SPEED
	    move_and_slide()
    pass

javrocks | 2023-04-20 20:39

There was a typing error in my previous answer, you can try with a “false” parameter instead of “true”.

If the scene can run, have you tried to add breakpoint on these lines of code ? :

  • path =
    NavigationServer2D.map_get_path …
  • velocity = global_position.direction_to(path[1]) * SPEED

If you never reach the breakpoints, your problem may be a node that was not found in the ready step

If you reach them, what is returned by map_get_path ?

Adrasteon | 2023-04-21 03:48

When i run, the code reaches the breakpoints. I’ve also changed false to true, but the enemy still isn’t moving. When i print (path) i get " " being printed over and over…

javrocks | 2023-04-21 05:08

I created a small project to test the behavior.

You need to make these changes:

  • You need a Navigation2DRegion (maybe you can just right click the Navigation2DLink node and select “Change type”)
  • Click on the Navigation2DRegion node, and in the inspector, create a navigation polygon
  • Click on the Navigation2DRegion node. A green icon on the top of the scene will allow you to define the navigation region.
  • Use this line of code: path = NavigationServer2D.map_get_path(get_world_2d().get_navigation_map(), global_position, player.position, false)
  • The “path” variable will now have a value (for a reason I don’t know, with my test, the first call of the function returns an empty array, but it is not the case after that)

You can check Navigation agents, it seems it is easier to handle navigation. It seems that you can find navigation tutorials on Youtube if needed (like this one).

Adrasteon | 2023-04-21 16:51

I made all the changes but it isn’t working. My navigation polygons are placed on the tiles and the first call of the function also returns an empty array, but it is also not the case after that. However when i run the game the enemy just moves up a little then sticks in that position.

javrocks | 2023-04-21 20:30

I made a sample project here that is working. You can try to take inspiration from it.

There is one thing that may be the cause of your problem:

  • Be sure that the sprite of a character has a local position equal to (0,0)
  • When you want to move the character, don’t move the sprite node (you need to click on the character node and move it)
  • When you click on the CharacterBody node and then in the sprite node, the position mark on the scene should be in the same position for the two nodes

I hope you will be able to resolve your problem with that. I don’t think I can help you more…

For information I will probably remove the repository from gitlab in one or two weeks, so be sure to download the project on your computer before the deletion if you need it.

Adrasteon | 2023-04-22 11:44

Ok thanks for ur help. I will download the project

javrocks | 2023-04-22 16:18

I can’t find the project anymore. Can you upload it again?

AngryGeri | 2023-05-01 13:37

I uploaded it again

Adrasteon | 2023-05-01 19:22