How can I keep a change in a material the same when changing or reloading scenes using singletons.

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

I have some code that changes the color of the player when I press a button:

var color = SpatialMaterial.new()


func _on_Blue_pressed():
    color.albedo_color = Color(0, 0, 1)
    $MeshInstance.set_surface_material(0, color)

But when I change the scene it resets the color.

I know the basics of how singletons work but I have no idea how to keep the color the same using them. I assume I would need to change the player’s color in the singleton’s script but I cannot figure out how to do that, does anyone know how I can do this?

:bust_in_silhouette: Reply From: exuin

You can store the color of the material in the Singleton, like player_color or something. After reloading the scene, you can fetch the color from the Singleton and set it for the material.

I have tried this but I don’t know how I would get the player’s material.
And how would I detect when the scene is changed or reloaded?

DerangedPufferfish | 2021-05-17 11:49

I figured out how to get the player’s material and set it using the singleton but I have no idea how I would know when the scene is changed.

DerangedPufferfish | 2021-05-17 13:55

The ready function will be called again when the scene is reloaded

exuin | 2021-05-17 13:57

So the _ready() function will be called every time the scene changes if it is on the singleton?

DerangedPufferfish | 2021-05-17 14:00

No, the _ready function will be called on the scene that was reloaded/changed.

exuin | 2021-05-17 14:02

I think I get it now, thanks.

DerangedPufferfish | 2021-05-17 14:11