Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Azimuth7 | |
Old Version | Published before Godot 3 was released. |
Hi ! I´m pretty new to this game engine and I´m trying to do a very simple procedure…
I created two buttons (texture buttons) and they are big like deck cards.
I´m struggling trying to do a procedure like, when someone press the button the Godot will pick a random image from my loaded images and load as the image texture of the pressed button.
Are there any way to do this? Or should I try a different approach?
Thank you in advance
You can store/preload the images first in a constant like this:
const images = [preload("res://image1.png"),
preload("res://image2.png"),
preload("res://image3.png")]
Then, in a function that will be called when the button is pressed, just add this:
func _onButtonPressed():
set_texture(images[round(rand_range(0,2))])
The rand_range()
method will choose a float from zero to two, so we must round it to the nearest integer. However, the rand_range method will always choose the same random number, so you can use the randomize()
method before the true random number (someone said like that) but I don’t know how to use it :v
tam0705 | 2017-06-28 02:02
Super thanks!
Azimuth7 | 2017-06-29 22:06