super easy question : string to variable

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

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 !

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

Calinou | 2018-07-30 23:04

1 Like
:bust_in_silhouette: Reply From: Xrayez

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.

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 ! :wink:
I didn’t know about this “match” command, that’s really useful !

nonomiyo | 2018-07-28 07:40

:bust_in_silhouette: Reply From: nonomiyo

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)

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.

Xrayez | 2018-07-30 13:56

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 :confused:

nonomiyo | 2018-07-30 14:22

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.

Xrayez | 2018-07-30 14:27

this returns “null” but thanks Xrayez, I’ll look into dictionnaries and arrays :slight_smile:

nonomiyo | 2018-07-30 15:44