Error when spawning chunk

Godot Version

4.2.1

I decided to try to make an endless runner, but several problems arose when implementing chunks.

problem 1

for some reason, when creating chunk number 2, the file with it simply breaks, everything is fine with chunk 1, chunk 3, etc., but chunk two breaks

problem 2 decided

When creating a chunk, two errors appear
E 0:00:02:0557 Globals.gd:9 @ spawn_chunk(): 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()
Globals.gd:9 @ spawn_chunk()
ChunkBaseScript.gd:17 @_on_spawn_area_body_entered()

code

chunk base

extends Node2D

var p = false

func _on_del_area_area_entered(area):
	if area.is_in_group("worl_wall"):
		queue_free()

func _on_spawn_area_body_entered(body):
	print('fdfd')
	if body.is_in_group("player") and not p:
		p = true
		print('colide')
		
		var end = $TileMap.get_used_rect().end.x * $TileMap.tile_set.tile_size.x + global_position.x
		print(end)
		G.spawn_chunk(end)

singleton

extends Node

@onready var chunk_list := [preload("res://nodes/chunks/chunks_list/chunk_1.tscn"),preload("res://nodes/chunks/chunks_list/chunk_3.tscn")]

func spawn_chunk(chunk_position):
	var chunk_node = chunk_list.pick_random().instantiate()

	chunk_node.global_position.x = chunk_position
	get_parent().add_child(chunk_node)

try this instead?

@onready var chunk_list := [load("res://nodes/chunks/chunks_list/chunk_1.tscn"),load("res://nodes/chunks/chunks_list/chunk_3.tscn")]

nothing has changed

What’s the code inside chunk2? Is it trying to change size or state of a collision_shape? collision_shape properties should only be changed using set_deferred method, not directly.

try

get_parent().add_child.call_deferred(chunk_node)

why do you need to add child at root node?, shouldnt it targets the parent that’s holding all chunk ?

I have a chunck_base node to create a new chunk, I set this node as a parent, create a level and add it to the chunk_list, I’m just not very experienced and don’t understand where the node will be attached if I call the add_child() function in a singleton

you can send the node reference to the singleton too, so it can use it as parent to add the child of new chunk

example:
chunk base
G.spawn_chunk(end, self)
assuming the chunk base is the parent

singleton

func spawn_chunk(chunk_position,parent_node):
	var chunk_node = chunk_list.pick_random().instantiate()

	chunk_node.global_position.x = chunk_position
	parent_node.add_child(chunk_node)
1 Like

thank you very much it worked! I add this to the parent node because I don’t really understand where to attach the node if I call add_child() in the singleton

basically you can pass around the node as a parameter, parent is also a node, so you can throw it into spawn_chunk method, as you can see above edited

What he said, although instead of singleton I’d have a scene called Level that is responsible for spawning/freeing chunks.

Singletons aren’t part of Scene Tree and you should not be attaching children to them.

So have a Level scene. It has spawn_chunk() method. In it’s _ready(), call spawn_chunk(pos) where pos is the starting position.

func _ready():
	spawn_chunk(0)

func spawn_chunk(pos: float):
	var chunk_node = chunk_list.pick_random().instantiate()

	chunk_node.global_position.x = pos
	chunk_node.spawn_area_entered.connect(spawn_chunk)

	add_child(chunk_node)

then in your chunk class:

extends Node2D

signal spawn_area_entered(end: float)

var p = false

func _on_del_area_area_entered(area):
	if area.is_in_group("worl_wall"):
		queue_free()

func _on_spawn_area_body_entered(body):
	print('fdfd')
	if body.is_in_group("player") and not p:
		p = true
		print('colide')
		
		var end = $TileMap.get_used_rect().end.x * $TileMap.tile_set.tile_size.x + global_position.x
		print(end)
		spawn_area_entered.emit(end)
2 Likes

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