Using strings as to reference variables

Post any engine-related development questions or issues you are having to receive advice, support, or help.
K29
Posts: 3
Joined: Mon Jul 17, 2023 11:28 pm

how do I use strings to reference variables?

SOLVED

Code: Select all

get()
Last edited by K29 on Sun Jul 23, 2023 4:12 am, edited 1 time in total.
User avatar
stayathomedev
Site Admin
Posts: 82
Joined: Mon Jul 17, 2023 2:42 pm

What are you attempting to do in the script? GdScript?
User avatar
TheBigExist
Posts: 1
Joined: Tue Jul 18, 2023 6:05 am

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")
User avatar
TheArduinoGuy
Posts: 5
Joined: Tue Jul 18, 2023 10:42 am

Question unclear. What exactly are you trying to do?
jgodfrey
Posts: 1
Joined: Tue Jul 18, 2023 7:43 pm

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"]
K29
Posts: 3
Joined: Mon Jul 17, 2023 11:28 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
User avatar
stayathomedev
Site Admin
Posts: 82
Joined: Mon Jul 17, 2023 2:42 pm

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"
User avatar
b4ux1t3
Posts: 8
Joined: Mon Jul 17, 2023 11:18 pm

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?`
Owen Lloyd
Posts: 30
Joined: Wed Jul 19, 2023 2:37 am
Location: Brisbane Australia

Interesting question. That sounds like using pointers, by ref, etc.

Does gdscript provide a way to use pointers?
agrimminck
Posts: 2
Joined: Sun Jul 23, 2023 12:58 am

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
Post Reply