So the basic setup of the error is that two levels are added immediately. Then, whenever the player reaches a certain point and collides with an Area2D node it should add a new level (levels stored in separate scene).
The code is split up into code for the main Node2D (World) and an Area2D (NextLevel) with a CollisionShape2D. The Area2D is in the level1 scene while the Node2D is in the main scene.
Relevant Code for World Node2D
extends Node2D
onready var level1 = preload("res://level1.tscn")
onready var level2 = preload("res://level2.tscn")
var levels = 1
func _ready():
var level1i = level1.instance()
var level2i = level2.instance()
add_child(level1i)
level2i.position.y += -2048
add_child(level2i)
func _newLevel():
print("new level")
levels+=1
var level1i = level1.instance()
level1i.position.y += -2048*levels
add_child(level1i)`
Code for NextLevel Area2D
`extends Area2D
onready var world = get_node("/root/World")
func _on_NextLevel_body_entered(body):
print(body)
world._newLevel()
get_parent().queue_free()
So, to get to the point, for some reason, whenever the player collides with the Area2D and triggers _on_NextLevel_body_entered
, the game completely freezes. No errors, it just freezes.
What is even more strange is when it does freeze, the variable (body) is not printed, and neither is "new level", yet when I remove the line "add_child(level1i)" both are printed, although now the game doesn't function properly.
Sorry for spaghetti code, pretty new to Godot.
Also, I just realised that when it is frozen, this message comes up in the command prompt window that opens with Godot:
At: servers/physics_2d/physics_2d_server_sw.cpp:739
ERROR: Can't change this state while flushing queries. Use call_deferred() or set_deferred() to change monitoring state instead.