Dungeon generation, creating the paths with Astar2D from room to room

Godot 4.3

I have been following this tutorial on creating a dungeon and the paths that connect the rooms, however in godot 4.3 this algorithm seems to not work correctly, i have tested the same code in the version he uses in the video(godot 3.0) and it works fine, i have also tried changing values to see if anything sticks but nothing.

This is my code on “carving” the paths, this is the only part of the code that deals with these paths, however bellow it i have the section of the code that sends the positions to connect.

func carve_path(_pos1:Vector2,_pos2:Vector2,r):
    var x_diff = sign(_pos2.x-_pos1.x)
    var y_diff = sign(_pos2.y-_pos1.y)
    if x_diff == 0: x_diff = pow(-1.0, randi() % 2) # -1^(2 or 1) positve if 2 else negative
    if y_diff == 0: y_diff = pow(-1.0, randi() % 2)
    print(r.name,"\n",_pos1,"\n",_pos2)
    var x_y := _pos1
    var y_x := _pos2
    if (randi()%2)>0:
        x_y = _pos2
        y_x = _pos1
    
    for x in range(_pos1.x,_pos2.x,x_diff):
        await get_tree().process_frame
        Map.set_cell(Vector2(x,x_y.y),0,Vector2i(9,7))
    for y in range(_pos1.y,_pos2.y,y_diff):
        await get_tree().process_frame
        Map.set_cell(Vector2(y_x.x,y),0,Vector2i(9,7))

This part is independant from the code above it.

	var corridor := []
	
	for room:RigidBody2D in rooms_container.get_children():
		
		var s = floor(room.size / tile_size)
		var pos = Map.local_to_map(room.position)
		var ul = floor(room.position / tile_size) - s
		# Fill the rooms
		#for x in range(2,s.x - 1):
			#for y in range(2,s.y - 1):
				#Map.set_cell(Vector2(ul.x + x,ul.y + y),0,Vector2i(9,7))
		
        # Get the path connections and positions
		var p = paths.get_closest_point(Vector2(room.position.x,room.position.y))
		for conn in paths.get_point_connections(p):
			if not conn in corridor:
				var start = Map.local_to_map(Vector2(paths.get_point_position(p).x,paths.get_point_position(p).y))
				var end = Map.local_to_map(Vector2(paths.get_point_position(conn).x,paths.get_point_position(conn).y))
				carve_path(start,end,room)
		corridor.append(p)

i have also checked to see if the paths are not generating correctly with the _draw() function here is the code for that:

func _draw() -> void:
	for room in rooms_container.get_children():
		draw_rect(Rect2(room.position - room.size,room.size),Color(0,1,0),false)
	if paths:
		for p in paths.get_point_ids():
			for c in paths.get_point_connections(p):
				var pp := paths.get_point_position(p)
				var cp := paths.get_point_position(c)
				draw_line(Vector2(pp.x,pp.y),Vector2(cp.x,cp.y),Color(1,0,0,.5),1,true)

And the results:
image

Should be noted that this is not 100% consistent, I do get some generations with no errors, but i also get generations with errors this big. Any help would be great, Thank you in advance

Me and someone i know remade this, with the original tiles from the video and its working fine… so its sloved? it was a very obscure error to me

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.