Trouble instantiating object

Godot Version

4.2.1

Question

I am making a game with a boss and one of the attacks im making is a laser shot. However it’s not working for some reason and i can’t find out why. Heres the code:

extends CharacterBody2D

@onready var player = get_node("/root/Main/Player")
@onready var attack_timer = %Attack_timer
@onready var delay_timer = %Delay_timer

var stamina = 100
var speed = 2000
var dashing = false
var direction = Vector2()
var rotation_speed = 5.0

var attacks = ["Shoot_laser"]

func _physics_process(delta):
	if dashing == false:
		direction = (player.global_position - global_position)
		var targetRotation = direction.angle()
		rotation = lerp_angle(rotation, targetRotation, rotation_speed * delta)
	else:
		rotation = direction.angle()
	
	var current_rotation = rotation
	
	velocity = (direction.normalized()) * speed
	
	move_and_slide()

func _on_attack_timer_timeout():
	var attack_index = randi() % attacks.size()
	var attack = attacks[attack_index]
	if attack == "Dash":
		speed = 6000
		dashing = true
		delay_timer.start()
		stamina -= 20
		%Stamina_bar.value = stamina
		attack_timer.wait_time = 0.5
	elif attack == "Shoot_laser":
#i think the problem is most likely somewhere here
#------------------------------------------------------------------------------
		const LASER = preload("res://boss_laser.tscn")
		var new_laser = LASER.instantiate()
		
		new_laser.global_position = global_position
		new_laser.global_rotation = global_rotation
		stamina -= 10
#-------------------------------------------------------------------------------		

func _on_delay_timer_timeout():
	speed = 2000
	dashing = false


func _on_stamina_regen_timeout():
	stamina += 2
	%Stamina_bar.value = stamina

The stamina does drain when the attack is called but the laser isn’t appearing for some reason.

Here is the laser’s code if it might help too

extends Area2D

var travelled_distance = 0

func _physics_process(delta):
	const SPEED = 1000
	const RANGE = 2000
	
	var direction = Vector2.RIGHT.rotated(rotation)
	position += direction * SPEED * delta
	
	travelled_distance += SPEED * delta
	if travelled_distance > RANGE:
		queue_free()

It seems you never add the new_laser instance to the scene using the function add_child.

1 Like

Well i tried it and it doesn’t seem to work unless i did something wrong which i probably did. Heres what i did.

func _on_attack_timer_timeout():
	var attack_index = randi() % attacks.size()
	var attack = attacks[attack_index]
	if attack == "Dash":
		speed = 6000
		dashing = true
		delay_timer.start()
		stamina -= 20
		%Stamina_bar.value = stamina
		attack_timer.wait_time = 0.5
	elif attack == "Shoot_laser":
		const LASER = preload("res://boss_laser.tscn")
		var new_laser = LASER.instantiate()
		
		new_laser.global_position = global_position
		new_laser.global_rotation = global_rotation
#-------------------------------------------
		add_child(new_laser)
#-------------------------------------------
		stamina -= 10
		

you should add const at code’s start(on top like

)

Apperantly i had to add it as a child to the main scene specificly