The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

Why doesn't this work? I call the 'set_color' from another node and I see that the right match statement is executed but the sprite does not change from it's default

extends RigidBody2D

var Drop_Color

var Green_Sprite
var Brown_Sprite
var Purple_Sprite
var Tan_Sprite
var Yellow_Sprite

func _ready():

    Green_Sprite = load("res://Images/green_drop.png")
    Brown_Sprite = load("res://Images/brown_drop.png")
    Purple_Sprite = load("res://Images/purple_drop.png")
    Tan_Sprite = load("res://Images/tan_drop.png")
    Yellow_Sprite = load("res://Images/yellow_drop.png")

    set_physics_process(true)

func set_color(c):

    Drop_Color = c

    match Drop_Color:

        Common.Color_Green:
            get_node("Sprite").set_texture(Green_Sprite)

        Common.Color_Brown:
            get_node("Sprite").set_texture(Brown_Sprite)

        Common.Color_Purple:
            get_node("Sprite").set_texture(Purple_Sprite)

        Common.Color_Tan:
            get_node("Sprite").set_texture(Tan_Sprite)

        Common.Color_Yellow:
            get_node("Sprite").set_texture(Yellow_Sprite)
in Engine by (113 points)

Did you try swapping the load method with preload? Maybe that could change something? :-/

Just tired it, still doesn't work.

1 Answer

0 votes
Best answer

Not sure why I can't load the textures in the _ready() function but changing the code to this got it working:

extends RigidBody2D

var Drop_Color

var Green_Sprite
var Brown_Sprite
var Purple_Sprite
var Tan_Sprite
var Yellow_Sprite

func _ready():

    set_physics_process(true)

func set_color(c):

    Drop_Color = c

    match Drop_Color:

        Common.Color_Green:
            Green_Sprite = load("res://Images/green_drop.png")
            get_node("Sprite").set_texture(Green_Sprite)

        Common.Color_Brown:
            Brown_Sprite = load("res://Images/brown_drop.png")
            get_node("Sprite").set_texture(Brown_Sprite)

        Common.Color_Purple:
            Purple_Sprite = load("res://Images/purple_drop.png")
            get_node("Sprite").set_texture(Purple_Sprite)

        Common.Color_Tan:
            Tan_Sprite = load("res://Images/tan_drop.png")
            get_node("Sprite").set_texture(Tan_Sprite)

        Common.Color_Yellow:
            Yellow_Sprite = load("res://Images/yellow_drop.png")
            get_node("Sprite").set_texture(Yellow_Sprite)
by (113 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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.