Help in code optimization

Godot Version

4.2.1

Question

Hi everyone, how to optimize this code? I tried to optimize it by merging it into one node, but the game just crashes on startup.

extends CharacterBody2D

@onready var sprite_2d = $Sprite2D

const SPEED = 500.0
const JUMP_VELOCITY = -900.0

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")


func _physics_process(delta):
	#Animations
	if(velocity.x > 1 || velocity.x <-1):
		sprite_2d.animation = "running"
	else:
		sprite_2d.animation = "default"
	
	# Add the gravity.
	if not is_on_floor():
		velocity.y += gravity * delta
		sprite_2d.animation = "jumping"

	# Handle jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var direction = Input.get_axis("ui_left", "ui_right")
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, 35)

	move_and_slide()
	
	var isLeft = velocity.x < 0
	sprite_2d.flip_h = isLeft
  


func _on_death_arena_body_entered(body):
	print("Player Death")
	get_tree().reload_current_scene()




func _on_death_spike_body_entered(body):
	print("Player Death")
	get_tree().reload_current_scene()


func _on_death_spike_2_body_entered(body):
	print("Player Death")
	get_tree().reload_current_scene()


func _on_death_spike_4_body_entered(body):
	print("Player Death")
	get_tree().reload_current_scene()


func _on_death_spike_5_body_entered(body):
	print("Player Death")
	get_tree().reload_current_scene()


func _on_death_spike_6_body_entered(body):
	print("Player Death")
	get_tree().reload_current_scene()


func _on_death_spike_3_body_entered(body):
	print("Player Death")
	get_tree().reload_current_scene()


func _on_death_spike_7_body_entered(body):
	print("Player Death")
	get_tree().reload_current_scene()


func _on_death_spike_8_body_entered(body):
	print("Player Death")
	get_tree().reload_current_scene()


func _on_death_spike_9_body_entered(body):
	print("Player Death")
	get_tree().reload_current_scene()


func _on_death_spike_10_body_entered(body):
	print("Player Death")
	get_tree().reload_current_scene()


func _on_death_spike_11_body_entered(body):
	print("Player Death")
	get_tree().reload_current_scene()


func _on_death_spike_12_body_entered(body):
	print("Player Death")
	get_tree().reload_current_scene()


func _on_death_spike_13_body_entered(body):
	print("Player Death")
	get_tree().reload_current_scene()


func _on_death_spike_14_body_entered(body):
	print("Player Death")
	get_tree().reload_current_scene()


func _on_death_spike_22_body_entered(body):
	print("Player Death")
	get_tree().reload_current_scene()


func _on_death_spike_16_body_entered(body):
	print("Player Death")
	get_tree().reload_current_scene()


func _on_death_spike_18_body_entered(body):
	print("Player Death")
	get_tree().reload_current_scene()


func _on_death_spike_19_body_entered(body):
	print("Player Death")
	get_tree().reload_current_scene()


func _on_death_spike_15_body_entered(body):
	print("Player Death")
	get_tree().reload_current_scene()


func _on_death_spike_17_body_entered(body):
	print("Player Death")
	get_tree().reload_current_scene()


func _on_death_spike_20_body_entered(body):
	print("Player Death")
	get_tree().reload_current_scene()


func _on_death_spike_21_body_entered(body):
	print("Player Death")
	get_tree().reload_current_scene()


func _on_test_tree_entered():
	get_tree().reload_current_scene()

My attempt to optimize:

func _on_test_tree_entered():
	get_tree().reload_current_scene()

I’m not too sure it’s optimization you’re after, as optimization usually refers to performance optimization, and your code needs to be refactored. I think you’ve already got the first refactoring down, which is to remove all those almost identical signal handler functions, and replace them with a single function.

After that, there’s not a lot of code left, and probably not enough to suggest other refactorings.

Finally, you mention the game crashes, but whether its related to the code you supplied, or something else, it’s difficult to help without an error message.

You can start by connecting the same callback function to the body_entered event of all spikes. There is no need for copying and pasting the same line over and over.

You could give your player a collider that only listens to the mask where the spikes are on.

Give your spikes a script with a class_name, for instance “class_name Spike” and then connect the onBodyEntered from the player collider and check for said class with if body is Spike: