how do we change sprite's color from menu option?

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

hi godot…
i created an options scene and there is a ability to change color of character’s sprite.
i created a button and connected it to the option’s script and used codes like this:

func _on_Salmon_pressed():
     color = "salmon"

then i wrote codes in character’s script like this:

onready var color_options = preload("res://scenes/Menu/Options.tscn").instance().color
 if color_options == "salmon":
    sprite.set_modulate(Color(98,5,45))

but when i loaded the scene, character’s color didn’t change, what did i do wrong?

What?

  1. How is that comment relevant to your question?
  2. Shouldn’t your if statement be inside of a function?

SIsilicon | 2018-09-26 12:25

hi @SIsilicon
the recent comment is the solution…

mdswamp | 2018-09-26 23:12

Maybe you ought to put it up as an answer instead of a comment.

SIsilicon | 2018-09-26 23:26

:bust_in_silhouette: Reply From: mdswamp

(SOLUTION)
1- create a singleton
2- use get and set function in singleton for changing desire variables:

:slight_smile:

(CODE IS NOT MINE, ITS “GDQUEST” 's CODES)

extends Node

const SAVE_PATH = "res://config.cfg"

var config_file = ConfigFile.new()
var _settings = {"debug": {"body_color": Color(1,1,1)}}

func _ready():

     save_settings()
     load_settings()

func save_settings():
     for section in _settings.keys():
         for key in _settings[section].keys():
             config_file.set_value(section, key, _settings[section][key])
     config_file.save(SAVE_PATH)

func load_settings():
     var error = config_file.load(SAVE_PATH)
     if error != OK:
        PRINT("Failed Loading Settings File. error code %s" % error)
        return[]
     for section in _settings.keys():
         for key in _settings[section].keys():
             _settings[section][key] = config_file.get_value(section, key)
             print("%s: %s" % [key, section])
     return[]

func get_setting(category, key):
    return _settings[category][key]


func set_setting(category, key, value):
    _settings[category][key] = value