@@ -313,8 +313,12 @@ func _build_planet_triangles(segs: int, uvs: Array[Vector2]) -> void:
313313 # Create and apply lunar material
314314 var material = create_planet_material ()
315315
316- mesh_instance .mesh = mesh
317- mesh_instance .material_override = material
316+ # Ensure mesh_instance is valid before assigning
317+ if mesh_instance != null :
318+ mesh_instance .mesh = mesh
319+ mesh_instance .material_override = material
320+ else :
321+ push_error ("mesh_instance is null in _build_planet_triangles" )
318322
319323func _add_triangle (i0 : int , i1 : int , i2 : int , uvs : Array [Vector2 ]) -> void :
320324 # Add triangle with reduced function calls
@@ -557,18 +561,19 @@ func _physics_process(delta: float) -> void:
557561 _update_atmosphere_params ()
558562
559563 # Apply gravitational pull only to bodies that aren't too far
560- for body in get_tree ().get_nodes_in_group ("affected_by_gravity" ):
561- if body is RigidBody3D :
562- if not is_instance_valid (body ) or body .freeze :
563- continue
564-
565- var direction = global_position - body .global_position
566- var distance = direction .length ()
564+ if get_tree () != null :
565+ for body in get_tree ().get_nodes_in_group ("affected_by_gravity" ):
566+ if body is RigidBody3D :
567+ if not is_instance_valid (body ) or body .freeze :
568+ continue
569+
570+ var direction = global_position - body .global_position
571+ var distance = direction .length ()
572+
573+ var force_magnitude = (gravitational_pull * body .mass ) / (distance * distance )
574+ var force = direction .normalized () * force_magnitude
567575
568- var force_magnitude = (gravitational_pull * body .mass ) / (distance * distance )
569- var force = direction .normalized () * force_magnitude
570-
571- body .apply_central_force (force )
576+ body .apply_central_force (force )
572577
573578 # Update local scatter near camera
574579 if enable_scatter :
@@ -611,15 +616,16 @@ func _get_directional_light() -> DirectionalLight3D:
611616 if l is DirectionalLight3D :
612617 return l
613618 # Fallback: search the tree (first one)
614- for node in get_tree ().get_nodes_in_group ("" ):
615- # no dedicated group; skip
616- pass
617- # Broad fallback: scan root children
618- var root = get_tree ().current_scene
619- if root :
620- for c in root .get_children ():
621- if c is DirectionalLight3D :
622- return c
619+ if get_tree () != null :
620+ for node in get_tree ().get_nodes_in_group ("" ):
621+ # no dedicated group; skip
622+ pass
623+ # Broad fallback: scan root children
624+ var root = get_tree ().current_scene
625+ if root :
626+ for c in root .get_children ():
627+ if c is DirectionalLight3D :
628+ return c
623629 return null
624630
625631func _update_atmosphere_params () -> void :
@@ -642,10 +648,12 @@ func _update_atmosphere_params() -> void:
642648 var sun_dir_obj : Vector3 = (global_transform .basis .inverse () * sun_dir_world ).normalized ()
643649 atmosphere_material .set_shader_parameter ("sun_dir_object" , sun_dir_obj )
644650 # Camera position in planet object space
645- var _cam := get_viewport ().get_camera_3d ()
646- if _cam :
647- var cam_obj : Vector3 = to_local (_cam .global_transform .origin )
648- atmosphere_material .set_shader_parameter ("camera_pos_object" , cam_obj )
651+ var viewport = get_viewport ()
652+ if viewport != null :
653+ var _cam := viewport .get_camera_3d ()
654+ if _cam :
655+ var cam_obj : Vector3 = to_local (_cam .global_transform .origin )
656+ atmosphere_material .set_shader_parameter ("camera_pos_object" , cam_obj )
649657
650658func _update_surface_atmo_params () -> void :
651659 var smat := mesh_instance .material_override as ShaderMaterial
0 commit comments