Procedurally Generated Map Not Generating Properly

Godot Version

4.2.1

Question

I am trying to procedurally generate a map by loading in chunks.
Whenever I instantiate a copy of a scene from that scene (yes, probably bad ideas), if it tries to load from there, it generated from the original scene.
It is currently an Area2D shaped on each side, and I’m detecting which collision shape was hit in order to know where to generate.

var areas_entered = []
var scene = preload("res://map2.tscn")
@onready var world = $".."
@onready var this = $"."

func _on_area_2d_body_shape_entered(_body_rid, body, _body_shape_index, local_shape_index):
	if body.get_class() == "CharacterBody2D":
		match local_shape_index:
			0:
				if not areas_entered.has("Right"):
					areas_entered.append("Right")
			1:
				if not areas_entered.has("Down"):
					areas_entered.append("Down")
			2:
				if not areas_entered.has("Left"):
					areas_entered.append("Left")
			3: 
				if not areas_entered.has("Up"):
					areas_entered.append("Up")

func _on_area_2d_body_shape_exited(body_rid, body, body_shape_index, local_shape_index):
	var count = 0
	for location_id in areas_entered:
		for id in areas_entered:
			if id == location_id:
				count +=1
		for i in range(count-1):
			areas_entered.erase(location_id)
		# fix generation, then delete.
		var instance = scene.instantiate()
		instance.position = get_node(location_id).position
		instance.get_node("mapScreens").get_node("Area2D").body_shape_entered.connect(_on_area_2d_body_shape_entered)
		instance.get_node("mapScreens").get_node("Area2D").body_shape_exited.connect(_on_area_2d_body_shape_exited)
		world.add_child(instance)
		print(location_id)
	if not len(areas_entered) == 0:
		for i in range(len(areas_entered)):
			areas_entered.remove_at(i)

There is also this error, but I don’t really understand it:
E 0:00:15:0949 MapGen1.gd:53 @ _on_area_2d_body_shape_exited(): Can’t change this state while flushing queries. Use call_deferred() or set_deferred() to change monitoring state instead.
<C++ Error> Condition “area->get_space() && flushing_queries” is true.
<C++ Source> servers/physics_2d/godot_physics_server_2d.cpp:355 @ area_set_shape_disabled()
MapGen1.gd:53 @ _on_area_2d_body_shape_exited()

Any help would be appreciated!

1 Like