Instances of an object dissapear when are out of the player's camera zone

Godot Version

4.5.1

Question

Thanks for reading, I am starting at Godot and I am having issues with object spawning, I have this frogger like game with cars (that I am being following from Clear Code tutorials on YT) and it seems to be that when my player moves and the cars are out of the camera zone they dissapear

ezgif-34c6e05eb43289

The cars should dissapear when they get at the border of the scene, but when moving my player and if they are out of the camera zone they are erased (with queue_free() my guess)

any advice on what am I doing wrong? these are the codes

car (or auto as I called them):

extends Area2D

var dir = Vector2.LEFT
var vel = 100
var colores = [
	preload("res://graphics/cars/green.png"),
	preload("res://graphics/cars/red.png"),
	preload("res://graphics/cars/yellow.png"),
	]

func _ready() -> void:
	if position.x < 0:
		dir.x = 1
		$AutoSprite.flip_h = true
	$AutoSprite.texture = colores.pick_random()

func _process(delta: float) -> void:
	position += dir*vel*delta

func _on_visible_on_screen_notifier_2d_screen_exited() -> void:
	queue_free()

game:

extends Node2D

var auto_scene: PackedScene = preload("res://objetos/auto.tscn")

func _on_llegada_body_entered(body: Node2D) -> void:
	print(body)
	print("Ha llegado a la meta")


func _on_auto_timer_timeout() -> void:
	var auto = auto_scene.instantiate() as Area2D
	var marcador = $PosicionesInicioAuto.get_children().pick_random() as Marker2D
	auto.position = marcador.position
	$objetos.add_child(auto)
	auto.connect("body_entered",iniciar)
	

func iniciar(body):
	print(body)
	print("inicio")

player:

extends CharacterBody2D

var dir: Vector2 = Vector2(1,1)
var vel: int = 200

func _physics_process(_delta: float) -> void:
	dir = Input.get_vector("izq","der","arr","aba")
	velocity = dir * vel
	
	move_and_slide()
	anim()
	
	if Input.is_action_just_pressed("sal"):
		print("Espacio")

func anim():
	if dir:
		$AnimatedSprite2D.flip_h = dir.x > 0
		if dir.x != 0:
			$AnimatedSprite2D.animation = 'horizontal'
		else:
			$AnimatedSprite2D.animation = 'arriba' if dir.y < 0 else 'abajo' 
	else:
		$AnimatedSprite2D.frame = 0

Well your code explicitly deletes them if they go out of screen here:

func _on_visible_on_screen_notifier_2d_screen_exited() -> void:
	queue_free()

If you don’t want that, don’t run that callback.

1 Like

But how do I delete the cars when they reach the end of the scene? Or else I would have running cars out of boundaries for ever (and instances of them consumming resources on the background which is bad for performance)

PD: I have borders with multiple CollisionShape2D

1 Like

Make a big rectangular area the size of the world, and use area_exited signal to get a notification when the car area went out of that big area.

1 Like

Indeed, area_exited worked just fine (after creating a big area2d that represents the space where the cars should be until they dissapear) thanks a lot!

func _on_car_area_area_exited(area: Area2D) -> void:
	area.queue_free()
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.