How to prevent camera jumps when reparenting the player node into a different tilemap section

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By blubbers122

I have a series of tilemap sections that the player jumps between when traveling through the map. The game knows to move the player between sections when a specific area2d is entered by the player area.

The problem is, each time I cross section boundaries, the camera jumps forward and repositions back towards the player quickly. It’s especially noticeable when camera smoothing is on. The camera bug only happens for a frame or so probably, right when switching sections.

The function that responds to the area entered signal removes the player node and re-adds it to the tree as a child of the next section. I’m pretty sure I have to use call_deferred to add the player back into the scene tree because I get an error otherwise. (it would also infinitely trigger the section-cross area signal). I think there’s a frame or so where the remote transform I use to get the camera to follow the player isn’t available to the camera.

Is there any workarounds I could try to get around this camera bug? It causes a noticeable stutter when moving between sections

func add_player():
	  next_section.add_child(player)
	  prev_section = current_section
	  current_section = next_section

# handles "cross_sections" signal
func move_player_to_next_section():
	current_section.remove_child(player)
	call_deferred("add_player")

func cross_section(_area):
	if cross_threshold_area.is_queued_for_deletion(): return
	cross_threshold_area.queue_free()
	emit_signal("cross_sections")

Scene Tree:

  • Game
    • section1
      • player
        • remote transform2d
    • section2
    • camera

What is the error you get when you don’t use call deferred.

TRAILtheGREAT | 2023-04-29 22:31

E 0:00:07:0054 Game.gd:67 @ add_player(): Can’t change this state while flushing queries. Use call_deferred() or set_deferred() to change monitoring state instead.

blubbers122 | 2023-04-29 23:42

Well, I ended up moving the player up a level so I don’t have to worry about reparenting it to the sections and it seems to be working so far as a workaround

blubbers122 | 2023-04-30 00:19