Cannot assign textures to a Sprite for a background

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


var backgTexture

func setBackground(givenType):
	
	if (givenType == "police"):
		backgTexture = load("res://.import/police.png-d5c5a446bf0cc91e8b78326343ed084d.stex")
	elif (givenType == "outsidev3"):
		backgTexture = load("res://.import/outsidev3.jpg-fdfbccb12bf54ec9b9ae4a84f740b464.stex")
	else:
		backgTexture = load("res://.import/outsidev2.png-14ac76f6f6acd4eedf8d7d1256814568.stex")
	$"backgroundSprite".set_texture(backgTexture)
	

func _ready():
	setBackground("police")

	

I’ve been trying to assign a background to a Sprite under a node for a while now. Getting pretty confused lol.

I’ve seen people answer that you have to have an Absolute pathway, but I have no idea how to find the string for that. Thanks in advance.

from what i know you should load the .png file directly instead of the .stex imported version.

Andrea | 2020-05-27 09:05

:bust_in_silhouette: Reply From: whiteshampoo

Untested!

extends Sprite

export var police_texture : Texture = null
export var outside_texture : Texture = null
export var default_texture : Texture = null

onready var Background : Sprite = $backgroundSprite

func setBackground(givenType) -> void:
	match givenType:
		"police":
			Background.set_texture(police_texture)
		"outsidev3":
			Background.set_texture(outside_texture)
		_:
			Background.set_texture(default_texture)

func _ready() -> void:
	assert(police_texture)
	assert(outside_texture)
	assert(default_texture)
	setBackground("police")

you have to drag&drop your textures into the inspector of your sprite, (see export) or you’ll get an Assertion-Error. (You want this. this prevents you from making mistakes)

EDIT:
you don’t want stuff from “.import”
Please ignore that folder :wink:

:bust_in_silhouette: Reply From: lyji

Figured it out. Here’s my code:

extends TextureRect
    var police_texture = preload("res://backgrounds/police.png")
    var outsidev3_texture = preload("res://backgrounds/outsidev3.jpg")
    var backofhouses_texture = preload("res://backgrounds/backofhouses.jpg")
    var bedroombackground_texture = preload("res://backgrounds/bedroombackground.png")
    var insidecafe_texture = preload("res://backgrounds/insidecafe.webp")
    var livingroombackground_texture = preload("res://backgrounds/livingroombackground.png")
    var outsidecafe_texture = preload("res://backgrounds/outsidecafe.png")
    var default_texture = preload("res://backgrounds/outsidev2.png")
    #^ named outsidev2.png
    var givenTexture = default_texture
    
    func setBackground(givenTexture) -> void:
    	match default_texture:
    		"police":
    			set_texture(police_texture)
    		"outsidev3":
    			set_texture(outsidev3_texture)
    		"backofhouses":
    			set_texture(backofhouses_texture)
    		"bedroombackground":
    			set_texture(bedroombackground_texture)
    		"insidecafe":
    			set_texture(insidecafe_texture)
    		"livingroombackground":
    			set_texture(livingroombackground_texture)
    		"outsidecafe":
    			set_texture(outsidecafe_texture)
    		_:
    			set_texture(default_texture)
    	
    func _ready() -> void:
    	setBackground(police_texture)
	

It may not be the most efficient, but it still works. I used a TextureRect, and had to right click it and choose add script. Works fine so far