Re: Using strings as to reference variables
Posted: Sun Jul 23, 2023 3:40 am
Its a 2d game with a sort of room systemb4ux1t3 wrote: ↑Wed Jul 19, 2023 3:43 pmSome folks have mentioned dictionaries and `get` already, but I'd like to take a different approach to help you.K29 wrote: ↑Tue Jul 18, 2023 10:41 pmvar v1 = "v2"stayathomedev wrote: ↑Tue Jul 18, 2023 2:37 am What are you attempting to do in the script? GdScript?
var v2
some how use v1 to refrence v2
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?`
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 want to use "roomtoleft" to refer to the list named roomtoleft
if i were to just
Code: Select all
currentroom = "roomtoleft"