how do i load the var list numbers with images? example 0 = preload("image.png") or something like that

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

extends Area2D

var list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

func _on_Player_Laser_area_entered(area):

if area.is_in_group("enemies"): #ignore this
	area.take_damage(1). #ignore this
	queue_free() #ignore this

randomize()

var item

item = list[rand_range(0, list.size())]

print(round(rand_range(0, list.size())))
:bust_in_silhouette: Reply From: exuin

So, there’s not really a need to have an Array with the values 0, 1, 2, etc. since those are already the indices of the Array. Instead, the values should be the images themselves. So like:

var list = [preload("image0.png"), preload("image1.png"), preload("image2.png")...

Also how do i can show the different images in a random order in the screen when something is destroyed.

func onPlayerLaserarea_entered(area):

if area.is_in_group(“enemies”):
area.take_damage(1).
queue_free() #when it destroy to show a ramdom image in a random place

Mate_Dev | 2021-09-16 18:22

Use the rand_range() function @GDScript — Godot Engine (stable) documentation in English and also the randi() function.

exuin | 2021-09-16 18:35

:bust_in_silhouette: Reply From: abelgutierrez99

Also you can create a dictionary and use load to reduce the use of memory, but impacting performance. Check what fits better for you and read documentation about preload and load before trying this solution.