Delete unselected items in an array and the game

Godot Version

4.6

Question

This is my code for choosing an item, my goal is to remove or delete the items in the array/game that aren’t chosen. Any ideas?

@export var array: Array[Area2D]
@onready var rng = RandomNumberGenerator.new()
var item

func _ready() -> void:
    pick_item()

func pick_item():
    item = array[rng.randi_range(0, array.size() - 1)]
    print(str(item))

Call queue_free() on all array elements that are not picked and then clear the array.

1 Like
@export var array: Array[Area2D]
@onready var rng = RandomNumberGenerator.new()
var item: Area2D

func _ready() -> void:
    pick_item()

func pick_item():
    var selected_idx: int = rng.randi_range(0, array.size() - 1)
    item = array[selected_idx]
    array.pop_at(selected_idx)
1 Like

Thank you for responding, but this code is only deleting the chosen item in the array.

Does this satisfy?

func pick_item():
    var selected_idx: int = rng.randi_range(0, array.size() - 1)
    item = array[selected_idx]
    array.clear()
    array.append(item)
1 Like

Thank you for your assistance!