ramokz / phantom-camera

👻🎥 Control the movement and dynamically tween 2D & 3D cameras. Built for Godot 4. Inspired by Cinemachine.
https://phantom-camera.dev/
MIT License
1.92k stars 62 forks source link

Error when switching from a 3D scene with a PCamHost to one with only a PCam #297

Closed jogly closed 1 month ago

jogly commented 1 month ago

Issue description

When switching scenes the Phantom Camera view finder dock tries to make the new scene's PCam3D active, but uses the old scene's Camera3D (via the old scene's PCamHost) if the new scene does not have a PCamHost. This means calling get_global_transform on Camera3D when it is not in the current scene tree which results in the following editor error:

scene/3d/node_3d.cpp:346 - Condition "!is_inside_tree()" is true. Returning: Transform3D()

I was able to remove the error by checking if the camera3d is inside the tree before considering it in addons/phantom_camera/scripts/phantom_camera_host/phantom_camera_host.gd#128

func _assign_new_active_pcam(pcam: Node) -> void:
    var no_previous_pcam: bool

    if _active_pcam:
        if _is_2D:
            _prev_active_pcam_2d_transform = camera_2d.get_global_transform()
            _active_pcam.queue_redraw()
-       else: 
+       elif camera_3d.is_inside_tree():
            _prev_active_pcam_3d_transform = camera_3d.get_global_transform()
            _prev_camera_fov = camera_3d.get_fov()
            _prev_camera_h_offset = camera_3d.get_h_offset()
            _prev_camera_v_offset = camera_3d.get_v_offset()

but the real fix should patch the viewfinder logic to clean up on scene switching so it doesnt even try to update the old PCamHost.

Steps to reproduce

Two scenes:

  1. With a Camera3D, PCamHost, and PCam3D. The viewfinder should work.
  2. With a PCam3D only.

Switching from (1) to (2) should print the erorr.

(Optional) Minimal reproduction project

No response

ramokz commented 1 month ago

This looks like a case where logic in the _exit_tree() function is called, which tries to communicate with PCamHost. Which it really shouldn't, given everything is exiting the tree when changing scene.

Think there's a more systemic checker that could be put in place to guard against this.

ramokz commented 1 month ago

Appears to be solved from changes in #267