godotengine / godot-demo-projects

Demonstration and Template Projects
https://godotengine.org
MIT License
5.44k stars 1.52k forks source link

Wait until `_ready` function to init previous_position to prevent errors #1042

Closed chrisl8 closed 2 months ago

chrisl8 commented 2 months ago

Upon starting the project and selecting a truck, this error is always emitted:

E 0:00:22:0996   follow_camera.gd:27 @ @implicit_new(): Condition "!is_inside_tree()" is true. Returning: Transform3D()
  <C++ Source>   scene\3d\node_3d.cpp:346 @ Node3D::get_global_transform()
  <Stack Trace>  follow_camera.gd:27 @ @implicit_new()
                 car_select.gd:15 @ _load_scene()
                 car_select.gd:39 @ _on_mini_van_pressed()

This also happens if you go back and pick a different truck.

I believe this is caused by attempting to assign global_position to a variable before the scene has initialized.

Waiting until the _ready() function to assign the variable for the first time appears to solve this issue.

Other fixes I tried fixed the initial error, but the error persisted if you went "Back" and selected a different truck. This fix covers that case too.

AThousandShips commented 2 months ago

@onready doesn't help?

chrisl8 commented 2 months ago

@AThousandShips @onready prevents the error the first time a truck is selected, but if I use the "Back" button (upper left corner" and select a different truck, the error still happens then on each subsequent truck selection.

chrisl8 commented 2 months ago

Never mind, I should have re-tested before replying. I'm sure that was the case yesterday, but today @onready fixes it entirely. I will do that instead.