Search code examples
godotgdscriptgodot4

Escape Block Textures Not Loading After Leaving First Room


Godot Version: v4.2.stable.official [46dc27791]

I am trying to have an escape block with a TextureRect that has two different textures--an animated one where it's awake and an inanimate one where it's asleep. The textures for my escape blocks display properly on the first room (only if the player just started--no changing scenes beforehand) and appear as a blank white texture other times (i.e. when leaving and reentering the scene). I have tried rewriting my code and using different methods and nodes to swap out the escape block textures, but all of them result in the white texture appearing at least some of the time.

I have tried rewriting my code and using different methods and nodes to swap out the escape block textures, but all of them result in the white texture appearing at least some of the time.

When I changed the wake and sleep functions to something simple (albiet not desired):

func wake():
  modulate.a = 1
  texture.set_pause(false)

The textures still fail to load. Looks like it has nothing to do with the code failing to swap out the sprites, but rather something else (I don't wanna make assumptions here).

I also tried rewriting the escape block code to use the wake and sleep textures directly rather than applying them to a single texture:

func wake():
    wake_rect.visible = true
    sleep_rect.visible = false
    modulate.a = 1

(Small snippet shown for convenience - full escape block code is on the pastebin I linked)

Still not working.

Can anyone help me figure out what I’m doing wrong or link me to an answer for reference? I’ve looked around but couldn’t find anything.


Solution

  • I FINALLY FIXED IT LET'S GOOOOOOOOOOOO

    I saved both the awake and asleep textures as .tres files and rewrote the code to have a single TextureRect use the .tres files:

    extends StaticBody2D
    
    var shape
    var awake
    var done = false
    var temp_texture: Texture2D
    
    @export var reverse = false # Reverse = sleep before panic, wake afterwards
    
    @onready var collision_shape = $CollisionShape2D
    
    @onready var texture_rect = $BlockTexture
    @onready var texture = $BlockTexture.texture
    
    const ESCAPE_BLOCK_AWAKE = preload("res://texture/escape_block_awake.tres")
    
    const ESCAPE_BLOCK_SLEEP = preload("res://texture/escape_block_sleep.tres")
    
    func wake():
        texture_rect.texture = ESCAPE_BLOCK_AWAKE
        modulate.a = 1
        set_collision_layer_value(1, true)
        set_collision_layer_value(2, true)
        set_collision_layer_value(3, true)
        set_collision_layer_value(4, true)
        #print("hi hi")
        
    func sleep():
        texture_rect.texture = ESCAPE_BLOCK_SLEEP
        modulate.a = 0.5
        set_collision_layer_value(1, false)
        set_collision_layer_value(2, false)
        set_collision_layer_value(3, false)
        set_collision_layer_value(4, false)
        #print("bye bye")
        
    func flip():
        if GameManager.panic:
            if reverse:
                wake()
            else:
                sleep()
        else:
            if reverse:
                sleep()
            else:
                wake()
    
    # Called when the node enters the scene tree for the first time.
    func _ready():
        texture_rect.visible = true
        texture_rect.size.x = collision_shape.shape.size.x
        texture_rect.size.y = collision_shape.shape.size.y
    
    func _process(delta):
        texture_rect.visible = true
        texture_rect.size.x = collision_shape.shape.size.x
        texture_rect.size.y = collision_shape.shape.size.y
    
        if GameManager.panic:
            if reverse:
                wake()
            else:
                sleep()
        else:
            if reverse:
                sleep()
            else:
                wake()
    

    (It took me a few minutes to trim out a lotta unused code LMAO) The textures load! Another issue solved.