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

b4ux1t3 wrote: Wed Jul 19, 2023 3:43 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?`
Its a 2d game with a sort of room system

rooms are defined with a list

Code: Select all

var example = ["roomimage.png", "roomtoleft", "roomtoright", "roomup", "roomdown"]

Code: Select all

var roomtoleft = ["roomimage.png", "left", "right", "up", "down"]
I cant just put the list of the room to the left in the previous list as it is cyclical and causes an error

I want to use "roomtoleft" to refer to the list named roomtoleft

if i were to just

Code: Select all

currentroom = "roomtoleft"
it would just be the string
User avatar
stayathomedev
Site Admin
Posts: 82
Joined: Mon Jul 17, 2023 2:42 pm

Do you have a screenshot of the room setup you're doing?
Post Reply