Como eliminar correctamente una escena instanciada?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Player_0

Hola : 3 estoy intentando hacer un videojuego con la temática de tanques en 2d, ya tengo algunas bases planteadas pero me surgió el siguiente problema:

Lo que quiero lograr hacer es eliminar la instancia bala y a la vez eliminar al jugador si es que se da la situación de que los dos colisionen, logre hacerlo pero surgió el problema de que al momento de disparar dos balas me sale el siguiente error, pero al disparar una sola funciona correctamente:

-Esto sucede cuando disparo dos o mas proyectiles:

-Cuando disparo solo un proyectil funciona como lo tenia pensado

-En la pestaña de salida imprimi "uwu " como indicador de que se ha eliminado correctamente el JUGADOR y la BALA

este es el codigo de las dos escenas:

Jugador:

extends KinematicBody2D

var movimiento = Vector2()
export(int) var velocidad 
var move

const BALA= preload("res://Escenas/bala.tscn")
const MISIL= preload("res://Escenas/Misil.tscn")

var seleccion = 1
var bala

func _ready():
pass



func muerte():
 if bala.colision.collider.name == "p1" :
	print("uwu")
	bala.queue_free()
	queue_free()
	



func fuego():
  if Input.is_key_pressed(KEY_1):
	seleccion = 1
  if Input.is_key_pressed(KEY_2):
	seleccion = 2
  if Input.is_action_just_pressed("ui_disparar"):
	if seleccion == 1:
		bala = BALA.instance()
		get_parent().add_child(bala)
		bala.position = get_node("Core/salida").global_position
		bala.rotation_degrees = $Core.global_rotation_degrees
		bala.connect("death_p1", self, "muerte")         
	  elif seleccion == 2:
		var misil = MISIL.instance()
		get_parent().add_child(misil)
		misil.position = get_node("Core/salida").global_position
		misil.rotation_degrees = $Core.global_rotation_degrees

 func _physics_process(delta):
    _mover()
    mirar_a()
    fuego()

Bala:

extends KinematicBody2D

var colision
var alt 
var movimiento
var count = 0

signal death_p1
signal death_p2

func _ready():
   alt = true              




 func muerte_por_colision():
   if colision:
	   if colision.collider.is_in_group("players"):              
		   emit_signal("death_p1")


  func _physics_process(delta):
     if colision:
	      movimiento = movimiento.bounce(colision.normal)              
	      alt = false
     if alt == true:
	   movimiento = Vector2(4,0).rotated(rotation)       
	   colision = move_and_collide(movimiento)           
    else:
	   colision = move_and_collide(movimiento)   
    count += 1 * delta                               
    if count > 16:
	   queue_free()

    muerte_por_colision()

que error te sale?

Zelta | 2021-02-23 18:50

No se puede ver el erro, podrías resubirlo?

MarshaLee | 2021-02-25 05:09

muchas gracias por comentar ya lo resolvi :3

Player_0 | 2021-02-26 01:55

Igualmente muchas gracias ya lo resolvi <3

Player_0 | 2021-02-26 01:56

:bust_in_silhouette: Reply From: Surtarso

I’m not sure I fully understand the question, but Ill try to help with my bullet code (the part that matters) Y me desculpe por no hablar espanol =)

func _on_body_entered(body): #if hits an enemy
	if body.is_in_group("enemies"):
        body._got_hit()
        queue_free()

and on my enemy _hit(): func

func _got_hit(): #gets hit by Player Bullet
   queue_free()

and my player shoot:

func _shoot():
    owner.add_child(bullet) #add bullet

on my enemy bullets it goes like this:

func _on_body_entered(body):
	if body.is_in_group("player"):
		body._got_hit()
	queue_free()

so the bullet free’s itself if hits ANYTHING, but also trigger the player _got_hit() function that could queue_free() my player if that was the intention

Surtarso | 2021-02-26 00:10

Yes, bro I was thinking about the solution for a while and I saw a post that tried to do the same thing that I wanted to apply in my code but with an area2d and the object parameter, and I managed to create a parameter in the signal that I created which I was sending the specific object that collided, thanks for your contribution: 3

Player_0 | 2021-02-26 01:59