change Sprite.texture inside the Player scene from singleton

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

I need help to connect a singleton to Sprite.texture inside Player scene where i can change the texture using a script i wrote.
What should i write in singleton to access the sprite variable mentiond?.
Root__
… |
Player
… |
… Sprite.texture

to detail my issue: that whenever I change the current scene the Player “Player.tscn” will be deleted and all current values will be lost. and i can’t figure how to save the variables in Player to the singleton so whichever the new scene is, it should bring back the Player values from singleton.
Thanks for your precious time.

:bust_in_silhouette: Reply From: AuthorSan
get_node("/root").get_child(1).find_node("Player").get_node("Sprite")

This worked for me.

what is the “.get_child(1)” job in this code line?

Sha6er | 2020-08-21 18:42

get_child(1)  will get you the current scene child of the root. Suppose your scene is level1 then It will get you level1 node. But it's not always the case, it all depends on the number of singletons you have in your project. In my project, there was on singleton code, so it took the 0th index so get_child(0) will get me my global script and get_child(1) will get me my scene node. But If I had one more singleton, then to access my scene node I would have used get_child(2)... I hope this explains

AuthorSan | 2020-08-22 06:46

yes it explains a lot. this would be a helpful information.

if my Player in the remote tree is shown as the following:
root

Global
Level1

Ysort

Player

Ysort is a child of Level1 and Player is a child of Ysort
show me how it will be wriiten depends on your earlier code:
get_node(“/root”).get_child(1).find_node(“Player”).get_node(“Sprite”)
thanks a lot.

Sha6er | 2020-08-22 20:18

Yes. Just read the docs for find_node() as there are other overloads too.

AuthorSan | 2020-08-23 06:44