[SOLVED] I am getting errors using "get_tree().reload_current_scene()"
Asked Answered
S

5

0

Hello, I am admittedly very new to Godot (starting yesterday), and I have been trying to learn Godot (mainly for fun). Ever since, I've been stuck on this error for a while (though I dont think it breaks the system I've got but I haven't tested it enough)

Here are the errors:

E 0:00:02:0523   enemy.gd:12 @ _on_entity_collision_body_entered(): Removing a CollisionObject node during a physics callback is not allowed and will cause undesired behavior. Remove with call_deferred() instead.
  <C++ Source>   scene/2d/collision_object_2d.cpp:98 @ _notification()
  <Stack Trace>  enemy.gd:12 @ _on_entity_collision_body_entered()

E 0:00:02:0523   enemy.gd:12 @ _on_entity_collision_body_entered(): This function can't be used during the in/out signal.
  <C++ Error>    Condition "locked" is true.
  <C++ Source>   scene/2d/area_2d.cpp:328 @ _clear_monitoring()
  <Stack Trace>  enemy.gd:12 @ _on_entity_collision_body_entered()

here is the full GD script:

extends CharacterBody2D

@export var gravity = 300.0;

func _physics_process(delta):
	if not is_on_floor(): velocity.y += gravity * delta
	move_and_slide()

func _on_entity_collision_body_entered(body):
	if body.is_in_group("player"):
		queue_free()
		if position.y - 10 < body.position.y: get_tree().reload_current_scene()

the part we should be looking at is "_on_entity_collision_body_entered" function from what i can tell its the "get_tree().reload_current_scene()" call what's causing the 2 errors above

the goal of this function is to reload scene if colliding and below the enemy(well -10 of the enemy). this does happen but I get them errors and after doing some research i have no clue why it happens and would like to learn why just in case It causes problems in my future tests

Symposium answered 6/3 at 22:54 Comment(0)
C
0

The error message explains it exactly:

Removing a CollisionObject node during a physics callback is not allowed and will cause undesired behavior. Remove with call_deferred() instead.

The signal that is connected to _on_entity_collision_body_entered is a physics callback. And reloading a scene does remove it, before adding it again. Obviously your scene contains a CollisionObject.

The error message also offers a solution: call the reload_current_scene function deferred.

Cantaloupe answered 6/3 at 23:1 Comment(0)
S
0

Cantaloupe sorry im not aware of what deferred means in this case, ill check the docs to see if i can find out what it is, thanks

Symposium answered 7/3 at 18:32 Comment(0)
S
0

Symposium i see there is a call_deferred() and a set_deferred(). i assume i use call but i dont know what to put in the argument

Symposium answered 7/3 at 18:51 Comment(0)
S
0

Symposium okay after research i feel like "call_deferred("get_tree", "reload_current_scene")" should work but nope which means im missing something just dont know what yet

hmm error logs says that's cuz "reload_current_scene" is an argument of "get_tree"

Symposium answered 7/3 at 19:9 Comment(0)
S
0

okay problem solved it was get_tree().call_deferred("reload_current_scene"), thanks

Symposium answered 7/3 at 19:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.