Godot Version
Godot 4
Question
Hello fellow Godot users! I’ve been working on my game, I made it so that when a timer ends a “starter_attack” scene gets instantiated and positioned to the player position (it will seem weird and no-sense in the video I’m uploading but it is because i’m gonna make it so that it queue_free()
s some istants after it spawned and then respawn, basically)
So everything was working fine until i noticed that if i go to the enemy sometime after the weapon spawned the enemy stops getting queue_free()
d by the starter_attack.
While if I instantly go to the enemy it will despawn as it is supposed to.
I even placed an attack on the floor, and even that one stops working, the enemy just goes through it.
The code I am using on the starter_attack is
extends Node2D
@export var attack_resource: Resource
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
func _on_area_2d_body_entered(body: Node2D) -> void:
if body.is_in_group("enemies"):
body.health -= attack_resource.damage
Note: attack_resource it’s just
extends Resource
class_name weapon_resource
@export var damage: int = 5
@export var weapon_level: int = 1
It is not anything very useful for now.
The code i am using for the weapon spawn (and for the character as a whole):
extends CharacterBody2D
const SPEED = 200.0
const JUMP_VELOCITY = -400.0
@export var weapon1: PackedScene
var new_weapon
func _physics_process(delta: float) -> void:
# Add the gravity.
# Handle jump.
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction_horizontal := Input.get_axis("ui_left", "ui_right")
if direction_horizontal:
velocity.x = direction_horizontal * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
var direction_vertical := Input.get_axis("ui_up", "ui_down")
if direction_vertical:
velocity.y = direction_vertical * SPEED
else:
velocity.y = move_toward(velocity.y, 0, SPEED)
move_and_slide()
func _on_timer_timeout() -> void:
var weapon_pos = global_position + Vector2(30, 0)
new_weapon = weapon1.instantiate()
add_child(new_weapon)
new_weapon.global_position = weapon_pos
The enemy code:
extends CharacterBody2D
@onready var adventurer = get_node("/root/TestingFields/Starter_character")
@export var enemy_resource: Resource
var health
func _ready() -> void:
health = 5
func _physics_process(delta: float) -> void:
var direction = global_position.direction_to(adventurer.global_position)
velocity = direction * 250.00
move_and_slide()
if (health == 0):
queue_free()
Btw basically all assets are temporary (just saying even though it is irrelevant to the problem.