How to change background with push a key on keyboard ?

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

Hi
Can you say which part of my code is wrong that its not working ?
func _input(ev):
If Input.is_key_pressed(Key_8):
$BackGame.texture = preload (the adress of the background i want to upload and the adress is right. I have checked it before)
Thanks

:bust_in_silhouette: Reply From: Zylo_X

Hello
First of all please describe your problem in more specific way
I think some parts of your code are incorrect

And If you are using TextureRect then you can use set_texture()
Like this example :

@onready var Back= $BackGame 

var New_Background= preload(address)

func input(ev): 
   if Input.is_key_pressed(Key_8):
       Back.set_texture(New_Background)

_process(delta) :
   input(ev)

Using the _input(event): function would be better because you dont have to call it in _process(delta):

For example :

@onready var Back= $BackGame 
var New_Background= preload(address)

func _input(event): # No need to call this in _process
    if Input.is_key_pressed(Key_8):
        Back.set_texture(New_Background)

Enfyna | 2023-03-26 09:28

I didn’t know about this function
It seems a pretty good way to handle inputs
Thanks !!!

Zylo_X | 2023-03-26 09:40

Thanks
I wrote it but it still don`t work when I run all the project but it work when I run just the scene.
and could you tell me what @onready do , please?
This is all my code in this scene:
The other keys script work in both run but the key 8 script just work in the scene project.
@onready var Back = $BackGame
var new_background = preload (I wrote the address)
func _input(event):
if Input.is_key_pressed(KEY_8):
Back.set_texture (New_Background)

Elina | 2023-03-26 14:44

Instead of typing $node every time where $ sign mean get_node()
You can type

 @onready var name = $node

Now you can use variable instead of typing $node everytime

Can you provide a screenshot of your main scene ?
You should instance the node which have the background to the main scene

See this image you should click the chain icon § and select the scene which suppose change the background
nodes-and-scene-instances-sprite-node hosted at ImgBB — ImgBB

Godot documentation
Nodes and scene instances — Godot Engine (stable) documentation in English

Zylo_X | 2023-03-26 16:48

How ?
Here there is no object to select an image or maybe there is and i dont know becuase i am new

Elina | 2023-03-26 17:07

here check this pic i tried it and it worked !!!
Screenshot-20230326-085831 hosted at ImgBB — ImgBB

image of the node + code
Screenshot-20230326-090034 hosted at ImgBB — ImgBB

extends Node2D
@onready var Back = $Background
var new_Background = preload("res://Data/back_cave.png")
var Old_Background = preload("res://Data/sci_fi_bg1.jpg")

func _input(event):
	if Input.is_key_pressed(KEY_8):
		Back.set_texture(new_Background)
	elif Input.is_key_pressed(KEY_7):
		Back.set_texture(Old_Background)

note : you have to use the numbers not the keypad ( under the numlock button)

Zylo_X | 2023-03-26 18:02