Changing Label node from another scene and it's script. [Godot 4.1.2]
Asked Answered
S

4

0

I'm having trouble figuring out how to change a label node from another scene. What I want is for my game settings option to be able apply font changes to a label that is from another scene and it's script.

As you can see from the commented out code, I've tried @export, but I do not understand it well enough. Can I make certain nodes global from other scenes and scripts?

Title_Screen_Menu script:
`extends Control

#@export var TS_Button: Button
@onready var btn_TScreen1 = $/root/Title_Screen_Menu/VBoxContainer/Button1
#@onready var btn_TScreen2 = $/root/Title_Screen_Menu/VBoxContainer/Button2
#@onready var btn_TScreen3 = $/root/Title_Screen_Menu/VBoxContainer/Button3
#@onready var btn_TScreen4 = $/root/Title_Screen_Menu/VBoxContainer/Button4

func _ready() -> void:
_on_button_2_pressed()
pass

func _on_button_1_pressed() -> void:
pass # Replace with function body.

func _on_button_2_pressed() -> void:
pass # Replace with function body.

func _on_button_3_pressed() -> void:
get_tree().change_scene_to_file("res://assets/options_menu.tscn")

func _on_button_4_pressed() -> void:
get_tree().quit()`

Options_Menu script:
`extends Control

const Settings_Save_Path := "user://config_save.cfg"
@onready var label_bus1 = $Label_bus1
@onready var label_bus2 = $Label_bus2
@onready var label_bus3 = $Label_bus3
@onready var btn_1 = $HBoxContainer/Button
@onready var btn_2 = $HBoxContainer/Button2
@onready var btn_3 = $HBoxContainer/Button3

var config = ConfigFile.new()
var FontStyle = "res://assets/MechanicalBold.otf"

func _ready() -> void:
pass

func _on_check_box_toggled(button_pressed: bool) -> void:
if button_pressed == true:
label_bus1.add_theme_font_override("font", load("res://assets/MechanicalBold.otf"))
label_bus2.add_theme_font_override("font", load("res://assets/MechanicalBold.otf"))
label_bus3.add_theme_font_override("font", load("res://assets/MechanicalBold.otf"))
btn_1.add_theme_font_override("font", load("res://assets/MechanicalBold.otf"))
btn_2.add_theme_font_override("font", load("res://assets/MechanicalBold.otf"))
btn_3.add_theme_font_override("font", load("res://assets/MechanicalBold.otf"))
TitleScreenMenu.btn_TScreen1.add_theme_font_override("font", load("res://assets/MechanicalBold.otf")) # title screen button
FontStyle = "res://assets/MechanicalBold.otf"
print(FontStyle)
if button_pressed == false:
label_bus1.add_theme_font_override("font", get_theme_default_font())
label_bus2.add_theme_font_override("font", get_theme_default_font())
label_bus3.add_theme_font_override("font", get_theme_default_font())
btn_1.add_theme_font_override("font", get_theme_default_font())
btn_2.add_theme_font_override("font", get_theme_default_font())
btn_3.add_theme_font_override("font", get_theme_default_font())
print("Default Godot font used")`





Sandstrom answered 12/10, 2023 at 19:13 Comment(0)
F
0

I'd just set a parameter of an autoload, and have the label object set its font based on that.

Ferrotype answered 13/10, 2023 at 2:32 Comment(0)
M
1

Sandstrom You could declare a signal in your menu script, emit the signal after you change the option and then have the other script listen to that signal and change the font.

Maxima answered 13/10, 2023 at 9:2 Comment(0)
S
0

Ferrotype I've tried autoloading the script and then the scene for my "Options".

I've noticed that my startup scene "title_screen_menu.tscn" would run twice. Another thing I saw is when both Scenes are autoloaded, it acts up and gives me an error. I have tried autoloading only the script "options_menu.gd" but with no good results.

In what ways can one set parameters to an autoload? I'm still much a beginner at this.

game project folder 1:
https://workupload.com/file/WBVPPYNB8EH

I then went on and tried another way by creating a function on the title screen scene/script that only loads it's configuration settings from the config file. Though, it seems like there is a better way for it and that way might be clunky and unnecessary bloat in coding.

game project folder 1c:
https://workupload.com/file/MjQNwJzVx7N

Sandstrom answered 13/10, 2023 at 21:17 Comment(0)
F
1

Sandstrom In what ways can one set parameters to an autoload?

Actually, I meant to say property. Just make the settings variables in the autoload script, which automatically makes them properties of its class. Then have any scripts that use fonts check those properties when they're created or updated.

If you don't want to have the other scripts check settings repeatedly, use signals to call a method in any class that needs to change, as Toxe says.

Ferrotype answered 14/10, 2023 at 1:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.