system
January 7, 2021, 6:42pm
1
Attention
Topic was automatically imported from the old Question2Answer platform.
Asked By
toxicspeed03
For example, I have an Array of values 1 to 15
And I also have 15 sprites in my resource folder, each named a number of 1 to 15
If I selected a number between 1 and 15 from the array, how could I have a matching sprite appear based on the array value?
Thanks
system
January 7, 2021, 11:09pm
2
Reply From:
shubhamr2511
Example 1:
res:// folder have : 1.tscn, 2.tscn, 3.tscn … 14.tscn, 15.tscn
Selected number = 9
arr = [1, 2, 3, ... 13, 14, 15]
var sel_num = 9
var path = "res://"+String(9)+".tscn"
var sprite_scn = load(path)
var sprite = sprite_scn.instance()
add_child(sprite)
or
if you have .png,.jpg etc file for sprites
Example 2:
res:// folder have : 1.png, 2.png … 14.png, 15.png
arr = [1, 2, 3, ... 13, 14, 15]
var sel_num = 9
var path = "res://"+String(9)+".png"
sprite = Sprite.new()
sprite.texture = load(path)
add_child(sprite)
system
January 8, 2021, 12:54pm
3
Reply From:
Andrea
are you using an array to store the number from 1 to 15?
it’s not very efficient, asarr[i]==i
, you can simply use i
you can however use an array to store the texture path directly, similarly to what shubhamr2511 suggested.
var arr=["res://Picture.jpg", "res://whatever_path", .... ]
var img=Image.new()
$Sprite.texture=load(arr[i])