0 votes

Hello, I have a super noobie question. I know that you can use str("variable") to turn a variable into a string, but how do you turn a string into a variable ?

I want to load a text depending on the language selected.

var language = "_eng"
var help_eng = "Hi, people !  :)"
helppanel.text = "help"+str(language)

This returns "help_eng" for example, instead of "Hi, people ! :)"

How should I do it ?
thank you !

in Engine by (245 points)

Consider using the built-in translation system in Godot; it is more robust and translator-friendly. See Internationalizing games in the Godot documentation.

2 Answers

+1 vote

Godot has a special method named str2var but I don't think it will solve your problem. What you actually need is being able to use TranslationServer to translate your strings in multiple languages. If this is too overkill for your use case, you can do something like this instead:

var language = "_eng"
var help

match language:
    "_eng":
        help = "Hi people!"
    "_es":
        help = "Hola gente!"
    _: # language not supported, fallback to default
        help = "Hi people!"

helppanel.text = help

You can add additional match values to translate your messages that way, but it will soon become quite inefficient to do so for every string. See official docs regarding how to setup proper translations for your game.

by (1,348 points)

thank you for your answer Xrayez !

the str2var didn't work as intended, it added single 'quotation marks', but I followed your way of doing it and now it works ! ;)
I didn't know about this "match" command, that's really useful !

0 votes

Hi, I'm back to this topic because I'm having the same syntax problem and the "match" thing isn't gonna help this time. (Plus I really need to understand this !)

var sprite4 = "that's what I want to see printed in the console"
var currentturn = 4;
var supertext = "sprite"+currentturn;      << how can I write that, please ?

print(supertext)
by (245 points)

Well the following should work for converting an integer to a string:

var supertext = "sprite" + str(currentturn)

Not sure what you want to achieve though because you seem to already know this based on your original question.

Or do you want to somehow retrieve the variable's name itself?

var sprite4 = "that's what I want to see printed in the console"

If that's the case I don't think it's possible in GDScript.

yes, I wanted to retrieve the variable's name and have "that's what I want to see printed in the console", printed in the console...

but if it's not possible, I guess I'll have to attack the problem from a different angle :/

Oh well, actually I see the solution, I forgot you can actually get the property by name with get method:

From within your script:

var supertext = "sprite" + str(currentturn) # should be "sprite4"

# The value of `sprite4` variable within the script
var what_you_want = get(supertext)

Take a look at dictionaries and arrays to perhaps solve your problem more efficiently.

this returns "null" but thanks Xrayez, I'll look into dictionnaries and arrays :)

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.