Page 1 of 2

Using strings as to reference variables

Posted: Mon Jul 17, 2023 11:31 pm
by K29
how do I use strings to reference variables?

SOLVED

Code: Select all

get()

Re: Using strings as to reference variables

Posted: Tue Jul 18, 2023 2:37 am
by stayathomedev
What are you attempting to do in the script? GdScript?

Re: Using strings as to reference variables

Posted: Tue Jul 18, 2023 6:37 am
by TheBigExist
I'm pretty sure it's the 'set()' function that you're looking for. To change the variable, just put

Code: Select all

set("your_string", value)
This would let you change the value, but if you want to use a string to use the value, use the 'get()' function.

Code: Select all

get("your_string")

Re: Using strings as to reference variables

Posted: Tue Jul 18, 2023 11:31 am
by TheArduinoGuy
Question unclear. What exactly are you trying to do?

Re: Using strings as to reference variables

Posted: Tue Jul 18, 2023 10:24 pm
by jgodfrey
Depending on the details, you might also find a Dictionary to be useful. For example:

Code: Select all

var myVars = {"string1": "John Doe", "string2": 101}
Then, you could reference one of the above items using something like:

Code: Select all

myVars.string1

...or...

myVars["string1"]

Re: Using strings as to reference variables

Posted: Tue Jul 18, 2023 10:41 pm
by K29
stayathomedev wrote: Tue Jul 18, 2023 2:37 am What are you attempting to do in the script? GdScript?
var v1 = "v2"
var v2

some how use v1 to refrence v2

Re: Using strings as to reference variables

Posted: Wed Jul 19, 2023 3:39 am
by stayathomedev
K29 wrote: Tue Jul 18, 2023 10:41 pm var v1 = "v2"
var v2
some how use v1 to refrence v2
You can always set a variable to equal another variable if they are the same type.

Code: Select all

var v1 = "string"
var v2 = v1 # v2 = "string"

Re: Using strings as to reference variables

Posted: Wed Jul 19, 2023 3:43 pm
by b4ux1t3
K29 wrote: Tue Jul 18, 2023 10:41 pm
stayathomedev wrote: Tue Jul 18, 2023 2:37 am What are you attempting to do in the script? GdScript?
var v1 = "v2"
var v2

some how use v1 to refrence v2
Some folks have mentioned dictionaries and `get` already, but I'd like to take a different approach to help you.

If you're trying to store the name of the second variable in the first one, then you're trying to do something called "meta-programming" (albeit a very simple version of it), that is, act on your source code directly from your source code. That's. . .generally not a good idea and isn't well supported in GDScript outside of things like `get`. There's usually a better way to do what you're trying to do!

I'd recommend telling us what specific thing you're trying to accomplish with this code, and that'd give us the context to figure out if you have what's known as an "XY Problem", where, in trying to solve problem Y, you get stuck on solving problem X, when you could have just solved problem Y directly.

Can you tell us what you're trying to do with this code, maybe give us a bit of code around what you're trying to do?`

Re: Using strings as to reference variables

Posted: Wed Jul 19, 2023 5:32 pm
by Owen Lloyd
Interesting question. That sounds like using pointers, by ref, etc.

Does gdscript provide a way to use pointers?

Re: Using strings as to reference variables

Posted: Sun Jul 23, 2023 1:15 am
by agrimminck
Owen Lloyd wrote: Wed Jul 19, 2023 5:32 pm Interesting question. That sounds like using pointers, by ref, etc.

Does gdscript provide a way to use pointers?
it does when you use dictionaries, and if i'm not mistaken, they work for arrays aswell