0 votes

first thank for help me and sorry for my broken english.

what i meant by that is, i make customize scene where i can change skin/hair/cloth/eyes type and rgb color.

now, what i want is, when i click "OK" button, i will transfer to new map/scene, but i can keep all change i make in customize scene. but in new scene i make
"player"/kenematicBody2d with animationplayer follow in with skin/hair/cloth/eyes sprite.

it possible to change set.texture in animationplyer sprite in new map/scene?
if i choose eyes8.png and RGB 100,123,156 in customize/scene, then can set.texture in animationplyer sprite in new map/scene also change to eyes8.png and RGB value?

i read global function but i dont understand, there any tutorial or video how to use this function and achieve what i just ask?

this my customize scene code example

var gender = "male" 
var eyes_offset = Vector2(0,0
var eyes_num = 1
var male_eyes_style = 9
var female_eyes_style = 9
var eyes_style = male_eyes_style
#########RGB COLOR######

var red
var green
var blue

func _ready():

func _process(delta):


func _on_male_button_pressed():
    if gender == "female":
        gender = "male"
        eyes_style = male_eyes_style

    eyes_offset = Vector2(0,0)


func _on_female_button_pressed():
    if gender == "male":
        gender = "female"
        eyes_style = female_eyes_style

    eyes_offset = Vector2(0,3)



func eyes_ready():
    $playerBase/eyes.set_texture(load("res://player/"+gender+"/eyes/eyes"+str(randi() % eyes_style +  1)+".png"))
    $eyes_RGB/Red_bg/Red_scrollbar.set_value(randi() % 255 + 1)
    $eyes_RGB/Green_bg/Green_scrollbar.set_value(randi() % 255 + 1)
    $eyes_RGB/Blue_bg/Blue_scrollbar.set_value(randi() % 255 + 1)

func eyes_rbg_box():
    red = $eyes_RGB/Red_bg/Red_scrollbar.value / 255
    green = $eyes_RGB/Green_bg/Green_scrollbar.value / 255
    blue = $eyes_RGB/Blue_bg/Blue_scrollbar.value / 255



func _on_eyes_buttonLeft_pressed():
    eyes_num -= 1
    if eyes_num < 1:
        eyes_num = eyes_style


func _on_eyes_buttonRight_pressed():
    eyes_num += 1
    if eyes_num > eyes_style:
        eyes_num = 1


func set_eyes_style():
        ## use for right and left button  -+= number  
        $playerBase/eyes.set_texture(load("res://player/"+gender+"/eyes/eyes"+str(eyes_num) +".png"))
in Engine by (391 points)

1 Answer

+1 vote
Best answer

Godot games run in a scene tree, which always has a root node, the main Viewport.
The current scene is added as a child of the root.

|- CustomizationScene <-- your current scene

When you use change_scene(), Godot removes the current scene, and instances the next scene in its place.
That means everything in your customization scene is removed, and is no longer accessible in the next scene that replaced it.

Very often, "Auto-load" nodes (or singletons) are used as a solution to this.
change_scene() doesn't clear the entire scene tree, only the node considered to be the current scene. That means if Viewport has other children, they will stay alive and can still be accessed. Those nodes are commonly called "singletons", because the use case is to have one of each.

Auto-loads are nodes that are automatically added to the tree that way, and stay alive even if you change the current scene, because those nodes are added as siblings of the current scene:

|- CustomizationScene <-- current scene, only this one gets replaced
|- CustomSingleton <-- "auto-load" node, it stays where it is

So, to access your customization variables in your second scene, you can create a script which goes on an auto-load, and store your variables in that node.
Then, in your next scene, you can access that singleton by using get_tree().get_root().get_node("CustomSingleton"), or get_node("/CustomSingleton"), which matches where singletons are in the tree.
To simplify this use case, Godot allows you to access such singletons by name from any script, so if you create CustomSingleton, you can just write print(CustomSingleton.eyes_color) from anywhere.

See the doc for more info about setting up singletons: https://docs.godotengine.org/en/latest/getting_started/step_by_step/singletons_autoload.html

There are other possible approaches to this problem, for example not using change_scene() and swap child nodes manually, but the bottom line is, it's just a particular usage of the scene tree :)

by (28,744 points)
edited by

thank you very much, ill try it.

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.
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 webmaster@godotengine.org with your username.