0 votes
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.

asked May 27 in Engine by lyji (33 points)

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

2 Answers

0 votes
Best answer

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

answered May 31 by lyji (33 points)
0 votes

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 ;)

answered May 27 by whiteshampoo (976 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.