How to call an Autoload script from a string variable

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

I’m experimenting with localization methods. My idea right now is to have an autoload DISPLAY script which contains blank variables for strings, and have separate language autoload scripts with each language’s translated strings. The game should replace the values in DISPLAY with the strings from the selected language’s script. So I want something like:

var language = "English"
 
func _ready():
#both globaldisplay.gd and English.gd are autoload scripts
globaldisplay.menutext = get(language).menutext

Obviously, this doesn’t work. I’m just not sure how I can call the English.gd script using the “language” variable. Thanks!

:bust_in_silhouette: Reply From: db0

I wouldn’t use autoloads for each languages at all. Too much hassle to maintain. Make the various language files individual reference scripts with unique class name. Then have 1 autoloader which redirects to the correct class.

E.g.|

class_name EnglishTexts
extends Reference


const TEXT1:= "Dialogue 1"
const TEXT2:= "Dialogue 2"
...

Then in your language switch code

match selection:
  "English": language_texts = EnglishTexts.new()

Then later in the code

dialogue1_label.text = language_texts.TEXT1

You might be able to make it work without creating a class object at all, just using the class name directly,

Thanks for the help. I tried out the class replacing, but had some trouble with a “cannot assign a new value to a constant” error. I ended up just creating a local variable which I assigned the autoload English script to, and then called that variable to replace the main script text. For my project, it doesn’t increase maintenance too much, but I’ll keep the class solution in mind to experiment with more if I need something more elegant.

grognard3 | 2021-07-15 23:04

I’m confused why you are trying to assign new values to the text consts, but if it works for you, it’s fine :slight_smile:

db0 | 2021-07-16 07:28