Implenting Font Options in Game Settings
Asked Answered
N

1

0

I want to implement a simple game option so that players can select a font type. It can be used as an accessibility option for players who suffer from Dyslexia. Or, give players options to choose from a more preferred font.

The thing is, I'm not sure how to implement it. Do I create multiple custom themes to do this?

Godot Project Folder:
https://workupload.com/file/dr9NbTAGT3p

Neanderthal answered 26/9, 2023 at 0:5 Comment(0)
N
0

Is this coded poorly? I've had options to change the font in the game, but the changing a node from another scene and script gave me trouble that I couldn't really solve. Instead I've opted to create another function to load only the changes for the options scene and a separate one for the title scene and it's script.

project folder
https://workupload.com/file/RNeYQ7GVyKc

Script for "Options_Menu:

`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 label_FS = $Label_fs
@onready var label_Fscr = $Label_fscreen
@onready var label_FontType = $Label_fontType
@onready var btn_1 = $HBoxContainer_OM/Button
@onready var btn_2 = $HBoxContainer_OM/Button2
@onready var btn_3 = $HBoxContainer_OM/Button3

var config = ConfigFile.new()
var FontSize = 75
var FontType = "res://assets/SpookyZombie.ttf"

func _ready() -> void:
pass

func _on_button_pressed() -> void:
config.set_value("Font", "Size", FontSize)
config.set_value("Font", "Font_Type", FontType)
config.save("user://config_save.cfg")

func _on_button_2_pressed() -> void:
var err = config.load("user://config_save.cfg")
if err != OK:
print("No configuration save file found")
return
for config_settings in config.get_sections():
var _cfg_settings_1 = config.get_value("Font", "Size")
label_bus1.add_theme_font_size_override("font_size", _cfg_settings_1)
label_bus2.add_theme_font_size_override("font_size", _cfg_settings_1)
label_bus3.add_theme_font_size_override("font_size", _cfg_settings_1)
btn_1.add_theme_font_size_override("font_size", _cfg_settings_1)
btn_2.add_theme_font_size_override("font_size", _cfg_settings_1)
btn_3.add_theme_font_size_override("font_size", _cfg_settings_1)
label_FS.add_theme_font_size_override("font_size", _cfg_settings_1)
label_Fscr.add_theme_font_size_override("font_size", _cfg_settings_1)
label_FontType.add_theme_font_size_override("font_size", _cfg_settings_1)
print("Loaded Font Size is ", _cfg_settings_1)
var cfg_settings_ft = config.get_value("Font", "Font_Type")
label_bus1.add_theme_font_override("font", load(
cfg_settings_ft))
label_bus2.add_theme_font_override("font", load(cfg_settings_ft))
label_bus3.add_theme_font_override("font", load(
cfg_settings_ft))
btn_1.add_theme_font_override("font", load(cfg_settings_ft))
btn_2.add_theme_font_override("font", load(
cfg_settings_ft))
btn_3.add_theme_font_override("font", load(cfg_settings_ft))
label_FS.add_theme_font_override("font", load(
cfg_settings_ft))
label_Fscr.add_theme_font_override("font", load(cfg_settings_ft))
label_FontType.add_theme_font_override("font", load(
cfg_settings_ft))
print("Loaded Font Type is ", _cfg_settings_ft)

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

func _on_h_slider_4_value_changed(value: float) -> void:
FontSize = (value)
print("FontSize = ", FontSize)

func _on_check_box_2_toggled(button_pressed: bool) -> void:
if button_pressed == true:
label_bus1.add_theme_font_override("font", load("res://assets/ZombikRegular.ttf"))
label_bus2.add_theme_font_override("font", load("res://assets/ZombikRegular.ttf"))
label_bus3.add_theme_font_override("font", load("res://assets/ZombikRegular.ttf"))
btn_1.add_theme_font_override("font", load("res://assets/ZombikRegular.ttf"))
btn_2.add_theme_font_override("font", load("res://assets/ZombikRegular.ttf"))
btn_3.add_theme_font_override("font", load("res://assets/ZombikRegular.ttf"))
label_FS.add_theme_font_override("font", load("res://assets/ZombikRegular.ttf"))
label_Fscr.add_theme_font_override("font", load("res://assets/ZombikRegular.ttf"))
label_FontType.add_theme_font_override("font", load("res://assets/ZombikRegular.ttf"))
FontType = "res://assets/ZombikRegular.ttf"
print("Font switched to ZombikRegular")
if button_pressed == false:
label_bus1.add_theme_font_override("font", load("res://assets/SpookyZombie.ttf"))
label_bus2.add_theme_font_override("font", load("res://assets/SpookyZombie.ttf"))
label_bus3.add_theme_font_override("font", load("res://assets/SpookyZombie.ttf"))
btn_1.add_theme_font_override("font", load("res://assets/SpookyZombie.ttf"))
btn_2.add_theme_font_override("font", load("res://assets/SpookyZombie.ttf"))
btn_3.add_theme_font_override("font", load("res://assets/SpookyZombie.ttf"))
label_FS.add_theme_font_override("font", load("res://assets/SpookyZombie.ttf"))
label_Fscr.add_theme_font_override("font", load("res://assets/SpookyZombie.ttf"))
label_FontType.add_theme_font_override("font", load("res://assets/SpookyZombie.ttf"))
FontType = "res://assets/SpookyZombie.ttf"
print("Font switched to default font")`

Title_Screen_Menu script:

`extends Control

@onready var btn_TScreen1: Button = $VBoxContainer0/Button1 #$/root/Title_Screen_Menu/VBoxContainer/Button1
@onready var btn_TScreen2: Button = $VBoxContainer0/Button2 #$/root/Title_Screen_Menu/VBoxContainer/Button2
@onready var btn_TScreen3: Button = $VBoxContainer0/Button3 #$/root/Title_Screen_Menu/VBoxContainer/Button3
@onready var btn_TScreen4: Button = $VBoxContainer0/Button4 #$/root/Title_Screen_Menu/VBoxContainer/Button4
@onready var label_GT: RichTextLabel = $RichTextLabel_GT

var config = ConfigFile.new()
var FontSize = 75
var FontType = "res://assets/SpookyZombie.ttf"

func config_ts_load():
var err = config.load("user://config_save.cfg")
if err != OK:
print("No configuration save file found")
return
for config_settings in config.get_sections():
var _cfg_settings_1 = config.get_value("Font", "Size")
btn_TScreen1.add_theme_font_size_override("font_size", _cfg_settings_1) # title screen button 1
btn_TScreen2.add_theme_font_size_override("font_size", _cfg_settings_1) # title screen button 2
btn_TScreen3.add_theme_font_size_override("font_size", _cfg_settings_1) # title screen button 3
btn_TScreen4.add_theme_font_size_override("font_size", _cfg_settings_1) # title screen button 4
label_GT.add_theme_font_size_override("normal_font_size", _cfg_settings_1) # title screen Rich Text label
print("Loaded Font Size for Title Screen Menu is ", _cfg_settings_1)
var cfg_settings_ft = config.get_value("Font", "Font_Type")
btn_TScreen1.add_theme_font_override("font", load(
cfg_settings_ft))
btn_TScreen2.add_theme_font_override("font", load(cfg_settings_ft))
btn_TScreen3.add_theme_font_override("font", load(
cfg_settings_ft))
btn_TScreen4.add_theme_font_override("font", load(cfg_settings_ft))
label_GT.add_theme_font_override("normal_font", load(
cfg_settings_ft)) # Rich Text Label
print("Loaded Font type for Title Screen Menu is ", _cfg_settings_ft)

func _ready() -> void:
config_ts_load()

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

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

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()`

Neanderthal answered 19/10, 2023 at 22:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.