Is there a way that you can change the background of your game by importing an image from your gallery?

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

Is there a way to add some function so that, for example, you can change the background of your game by importing an image from your gallery?

I have to do that since I am implementing an interface where you can put your name and a photo, but also if you can, I want the code to have specification in some way, such as the image being 500px x 500px, help pls…

:bust_in_silhouette: Reply From: exuin

Changing the background is simple since it’s just changing the texture property. In order to import and image, you need a FileDialog. Set its access to ACCESS_FILESYSTEM. After the user selects an image, you must load the image as an Image and use its methods to validate the size of it. Then use ImageTexture.create_from_image() (doc) to turn theImage into a texture that can be displayed in the game.

Oh thanks, but would you be so kind to give me a reference code pls.

RubRub | 2021-10-26 18:06

I need help with the code since this importing images is new for me

RubRub | 2021-10-26 18:16

not tested

func _on_FileDialog_file_selected(path: String) -> void:
	var i = Image.new()
	i.load(path)
	if i.get_width() != 500 and i.get_height() != 500:
		return
	var it := ImageTexture.new()
	it.create_from_image(i)

exuin | 2021-10-26 18:21

let me see if it worked, thank you very much

RubRub | 2021-10-26 18:23