Godot Version
4.6.1 Stable
Question
Is it possible to take a var example : bool and automatically convert the name of the var into a string value?
in this example I have a var stringexample : String = ““ and my example bool var has a setter function that I intend to set the stringexample as the name of the example var if example is ever set to true.
var example : bool:
set(switch):
example = switch
if switch == true:
stringexample # = ??? idk lmao, It should be "example" but I want to know if there's a way to do this automatically.
But idk how or if it’s possible and it feel obscure enough that I’m willing to ask about it.
Basically if example = true, I want stringexample = “example”.
Any way to do that algorithmically/procedurally/iteratively?
This is because I intend for there to be more bool variables that all will do the same thing if set to true, so an example2 var will make stringexample = “example2” and so on and so forth. IDM if I have to do it manually, but I’m wondering…
Thanks in advance for your time!
No.
The reason for this is because when your code is run through the interpreter, the variable loses its name.
However…
- You could do it with a match statement.
- You can do it with Dictionary keys.
If either of those fit your needs, and you want more info, let me know and I’ll explain them.
1 Like
I’m up to my eyebrows in match statements and dictionaries lmao, it’s messy work, but I’m willing to hear you out on both options. Please, elaborate.
Take your time, I’m heading off to work and won’t be back for the next 7/8 hours. Thank you again.
Why would you ever need this. If you think you need it then you’re likely doing something wrong and there’s a better approach to solve your problem than what you’re attempting with those variable names.
3 Likes
Keep in mind, I agree with @normalized that there’s a better way to do what you’re doing. If you were to take a step back and explain what problem this not working solution solves, we could probably give you a better solution.
Match Statement
So I realized that if you’re matching a bunch of boolean values, a match statement won’t work. Instead, you’ll have to hard code it.
var string_example: String
var example: bool:
set(value):
example = value
if example == true:
stringexample = "example"
Dictionary
var string_example: String
var examples: Dictionary[bool]
var example: Variant:
set(value): #We set the value with the key
example = examples.value # We store it as a boolean
if example == true:
stringexample = value
1 Like
It’s not that serious in the way that I “need” it, I was, as I explained in my OP, wondering if it were possible.
I am keenly aware that there are different approaches, I’m considering one now that I was told that this method isn’t possible and to do it this way, manually setting the String value is my best option.
I haven’t written myself into a corner where this is a load bearing problem, I could take the booleans and redo the idea into a single integer value that itself is in a match statement.
Thanks for both of your help.