extends Area2D
var nav_box = true
export var start = false
export var end = false
var step_count = 0
var total_path = []
var next_steps = []
var nearby_nodes = []
func _ready():
yield(get_tree().create_timer(0.5) , "timeout")
connect_nearby_nodes()
yield(get_tree().create_timer(0.5) , "timeout")
if start:
find_path()
func connect_nearby_nodes(): #The node is an area2D node which detects other node like itself and connects them.
if nearby_nodes == []:
return
for node in nearby_nodes:
var sight = get_world_2d().direct_space_state.intersect_ray(global_position , node.global_position , [self] , node.collision_mask)
if !sight and not next_steps.has(node):
next_steps.append(node)
func find_path():
if next_steps == []:
return
step_count += 1
total_path.append(self)
for next_step in next_steps:
next_step.pass_parameters(step_count , self , total_path)
func pass_parameters(previous_step_count , previous_step , previous_total_path):
step_count = previous_step_count
total_path = previous_total_path
next_steps.erase(previous_step)
check_if_end()
func check_if_end():
if end:
total_path.append(self)
print(self , step_count , total_path)
else:
total_path.append(self)
print(self , step_count , total_path)
func _on_NavBox_area_entered(area):
if area.get("nav_box") != null and area.nav_box:
nearby_nodes.append(area)
This is a simple navigation system I wrote.
The "else" in check_if_end() is supposed to be find_path() but I was just testing so it looked like what we currently see.
I arranged the system in triangle , each area intersects with the other two.
Area 1 is set to be start point
Area 3 is set to be the end
According to the code this should print
Area 2 , 1 , [area 1 , area2]
Area 3 , 1 , [area1 , area3]
But instead we get this
Area 2 , 1 , [area 1 , area2]
Area 3 , 1 , [area1 , area2 , area3]
Seems like the if-else in check_if_end() is not working properly.
When I detete total_path.append(self) in the else section, everything works fine.
we get what we expected like
Area 2 , 1 , [area 1]
Area 3 , 1 , [area1 , area3]
I don't know why this is happening plz help me :(