I need help with procedural generation

Godot Version

Godot 4.7.1

Question

I am struggling to get my buildings to turn correctly, I have them spawning in the right place but sometimes my roads and buildings rotate 180 degrees for seemingly no reason, I have tried a lot of stuff but I need another perspective on it, I’ll share my generation script and my street script

extends Node3D

const STREET_TEMPLATE1 = preload("res://Streets/street_template.tscn") # Road 1
const STREET_TEMPLATE2 = preload("res://Streets/street_template2.tscn") # Road 2
const BUILDING1 = preload("res://Streets/Building.tscn")
const crystal = preload("res://Collectibles/crystal.tscn")
const BOOST = preload("res://Collectibles/boost.tscn")
var roads_to_spawn: Array = [STREET_TEMPLATE1, STREET_TEMPLATE2, BUILDING1, BUILDING1, BUILDING1] # Possible roads to spawn
var collectibles_to_spawn: Array = [crystal, BOOST]
@onready var starting_street: street = $Roads/StreetTemplate # The starting street
@onready var roads: Node3D = $Roads # The node all roads are under
@onready var collectibles: Node3D = $Collectibles
var recently_summoned_collectibles: Array = []
var player
var collectible_spawned: bool = false

func _ready() -> void:
	starting_street.entered.connect(enter, CONNECT_ONE_SHOT)

func enter(street_node: street):
	for child in street_node.get_links(street_node.links):
		var road = roads_to_spawn.pick_random()
		var road_spawn = road.instantiate()
		roads.add_child(road_spawn)
		road_spawn.global_transform = child.global_transform
		if not road_spawn.is_in_group("Building"):
			road_spawn.entered.connect(disable, CONNECT_ONE_SHOT)
			road_spawn.set_enter_potential(true)
	for child in street_node.get_links(street_node.collectibles_links):
		if not collectible_spawned and randi_range(1, 5) == 1:
			collectible_spawned = true
			var collectible_node = collectibles_to_spawn.pick_random()
			var collectible_spawn = collectible_node.instantiate()
			collectibles.add_child(collectible_spawn)
			collectible_spawn.global_position = child.global_position
			recently_summoned_collectibles.append(collectible_spawn)
			if find_player():
				player.connect_collectible(collectible_spawn)
	collectible_spawned = false

func disable(not_disable: street):
	not_disable.set_enter_potential(false)
	for child in roads.get_children():
		if child != not_disable:
			child.queue_free()
	for child in collectibles.get_children():
		if child not in recently_summoned_collectibles:
			child.queue_free()
	await get_tree().process_frame
	recently_summoned_collectibles.clear()
	if is_instance_valid(not_disable):
		enter(not_disable)

func find_player():
	for child in get_tree().get_first_node_in_group("Player").get_children():
		if child.name == "CharacterBody3D":
			player = child
			return true
	return false



extends StaticBody3D
class_name street

var has_been_entered: bool = false
@onready var links: Node3D = $Links
@onready var collectibles_links: Node3D = $CollectiblesLinks
signal entered(node_to_keep: street)
@onready var area: Area3D = $Area3D
@export var starting_street_node: bool = false
var collectibles: Array = []

func _ready() -> void:
	if starting_street_node:
		area.monitoring = true

func find_link_loc(Link: Node3D):
	for child in links.get_children():
		if child == Link:
			return child.global_transform

func get_links(link: Node3D):
	return link.get_children()

func _on_area_3d_body_entered(body: Node3D) -> void:
	if body.is_in_group("Player") and not has_been_entered:
		has_been_entered = true
		entered.emit(self)

func set_enter_potential(value: bool):
	if not is_inside_tree():
		return
	area.set_deferred("monitoring", value)

Can you post a video that shows the problem?

I can’t upload attachments as I just recently created my account, I have the links turned correctly, but sometimes it just randomly turns the road/building in a random direction, it should be turning them relative to the street node. It only happens sometimes, so it’s not a problem with the direction of the link nodes. It’s something with this code

Try getting rid of that await.

didn’t work

Find a way to link a video and/or screenshots.

Not sure where that 180 deg rotation is actually seen.
I’d suggest implementing a controllable third person debug camera for a better oversight of the situation.

the wall that spawns in front of me is the wall being in the wrong direction

How did you determine it’s rotated 180 degrees from what is expected.

You’ll have to present the problem better and provide more context.

It usually is rotated so that the building it right beside the edge of the road, but in this case, the scene instance is rotated 180 degrees, causing the building to connect to the opposite side of the link and cover the middle of the road

The way it works is i have a road, on this road are a bunch of links under a single parent node, each link is rotated to the correct rotation so that the road will spawn at the rotation and position to be put on the link. It SHOULD (and most of the time does) put the rotation and the position equal to that of the link to the new road/building. But sometimes, the rotation flips it 180 degrees and places the building in the middle of the road, it only seems to happen with buildings and im not sure why.

Ended up using an area3D to detect if the building is inside another road and if it is, rotate 180 degrees. It works now, I just had to make sure it only did it once per instance of a building