How to switch between two Camera2Ds in different scenes

Godot Version

4.2

Question

I’m using switching between scenes in my game, kind of like in Pokémon when you start a battle, and I used this method to swap over to the fight scene so that my main game scene doesn’t get reset like it would if I used the change_to_scene method. I made the fight scene work when the game is paused so that I can pause the main game and just run the fight scene.

func _on_enemy_area_body_entered(body):
	if(body.name == "Player"):
		swap_level()

func swap_level():
	add_sibling(fight_scene)
	get_tree().paused = true

The problem with this is that the camera I put on my player node is being used by the fight_scene so it doesn’t show it properly. Is there any way to swap over to a camera node I put in the fight_scene?

this is my bossfight camera switch linked to signal

Player enteret the fight scene scene

fight_scene_cam.make_current()
player_cam.clear_current()

End fight scene

fight_scene_cam.clear_current()
player_cam.make_current()

of course this is version 3.2.3 maybe diferent method in 4.x

and result is something like this

The cameras would be in different scenes how would I change them both in one scene?

docs 4.2 says:

bool enabled = true

  • void set_enabled ( bool value )
  • bool is_enabled ( )

Controls whether the camera can be active or not. If true, the Camera2D will become the main camera when it enters the scene tree and there is no active camera currently (see Viewport.get_camera_2d).

When the camera is currently active and enabled is set to false, the next enabled Camera2D in the scene tree will become active.

Thanks this worked for me I used a func in main to enable the cam in it when the scenes got swapped.

code from main.gd:

func swap_level():
	camera.set_enabled(false)
	add_sibling(fight_scene)
	get_tree().paused = true
	
func enable_cam():
	camera.set_enabled(true)

code from fight_scene.gd:

func swap_level():
	main.enable_cam()
	get_tree().paused = false
	get_parent().remove_child(self)

Thank you EX74!!

1 Like

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