This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

Game freezing permanently for unknown reasons

CyberJellyCyberJelly Posts: 3Member
edited June 2020 in 2D

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.


Tags :

Best Answer

  • bitshift-rbitshift-r Posts: 54
    Accepted Answer

    In _on_NextLevel_body_entered you could try world.call_deferred("_newLevel"). Godot doesn't like you changing things during a physics update.

Answers

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.