Character freezes after shooting

Godot Version

Question

Hi! I’m new to Godot, so I don’t know the language very well. I made a character class for a 2D platformer. It can jump, move and is animated.
I followed this tutorial for the shooting.
My problem is that, whenever I press the shooting key (F), my character completely stops. I can’t move it, enemies don’t detect it, it’s animations stops too. I noticed this happens when “add_child(arrow_instance)” runs. I know cause I deleted that line of code and the character works. What is happening? Why is creating the child node doing this? I’ll leave my character class script here:

extends CharacterBody2D

const SPEED = 130.0
const JUMP_VELOCITY = -300.0

var baston_equipado = true
var arrow = preload(“res://Scenes/arrow.tscn”)

Get the gravity from the project settings to be synced with RigidBody nodes.

var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)

#Llamadas
@onready var animated_sprite = $AnimatedSprite2D
@onready var timer = $Timer

func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta

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

# Get the input direction -1, 0 ó 1
var direction = Input.get_axis("move_left", "move_right")

#Cambia el sprite de lado
if direction > 0:
	animated_sprite.flip_h = false
elif direction < 0:
	animated_sprite.flip_h = true

#Animaciones
if is_on_floor():
	if direction == 0:
		animated_sprite.play("Idle")
	else:
		animated_sprite.play("run")
else:
	animated_sprite.play("jump")

if direction:
	velocity.x = direction * SPEED
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()

var mouse_pos = get_global_mouse_position()
$Marker2D.look_at(mouse_pos)

#Llama a Disparar
if Input.is_action_just_pressed("Shoot") && timer.is_stopped() && baston_equipado:
	print("Tiro") 
	shoot()
	var arrow_instance = arrow.instantiate()
	arrow_instance.rotation = $Marker2D.rotation
	arrow_instance.global_position = $Marker2D.global_position
	add_child(arrow_instance)

#Disparar
func shoot():
timer.start()

func _on_timer_timeout():
timer.stop()

Thank you for reading. This is my first post, I am sorry if I didn’t do things in the proper order.

After a bit of troubleshooting with your code, I couldn’t seem to replicate your problem. I would try changing “add_child” to “add_sibling”. It might fix your issue.

I will admit I’m a bit stumped. Do you have any other scripts affecting the player?

Hi! Thank you so much for answering and sorry for the delay. If I change it to add_sibling the whole game freezes. HOWEVER, after trying random stuff I discovered that if I change the arrow’s initial position to a Vector2(0,0) instead of using the marker, the arrow is shot without issue… until it hits the border of the screen, then the character freezes again. I discovered that if I then put the game on fullscreen, the arrow (and my character) keeps moving without issue until it collides again with something. If my character dies while the arrow is moving, then both die and my character respawns without the arrow perfectly. This is the code of the arrow, although is very basic and I don’t think it’s the problem. Maybe the arrow needs to be on screen all the time?

extends Area2D

var speed = 100

func ready():
set_as_top_level(true)

func _physics_process(delta):
position += (Vector2.RIGHT*speed).rotated(rotation) * delta

What do you mean by “freezes”? this could be the debugger telling you a very important stack trace, which will help you get to the root of the issue, tab back into the editor when it freezes next time and post what error message shows up.

Hi, thanks for answering! Yes, by “freeze” I mean my character stops doing their animation, doesn’t move nor it reacts when enemies touch them (it should kill them). The rest of the game works normally. Enemies and platforms move and the music keeps playing. The game doesn’t show me any error messages, so I don’t know why this could be. I also learned that when my character shoots an arrow and jumps, the arrow jumps too.