GML Is it possible to create a variable with a name that is the value of another variable?

XirmiX

Member
For my game that involves networking, all I've been doing in terms of vital data storage and retrieval is just create some ds_maps (They may be tedious sometimes, but I like them, now that I fully understand them) and store various pieces of data as keys and their values in various different ways. With that, I've been able to create an interesting system, but currently, I think mys system would be unplayable ad I'm trying to make a break-through up until you can shoot things and for them to show up on the client as well with this version of the build. Anyway...

In case of an integer, floating point or a boolean, I understand, this wouldn't be possible, due to the way GML is structured and for no way of the program to distinguish between whether you're looking at something as a variable name and its value or a new value in specific situations. But in the case of a string...

Lets say I have variable str_variable_1 declared to a string value of "this_string":
Code:
str_variable_1 = "this_string";
Could I then create a new variable, or in my specific case, a list that is called the same as the value of that variable? So, I would in the end have:

Code:
"this_string" = ds_list_create();
But of course, I couldn't simply write "this_string" as the variable name, I mean, some way of taking the value of a variable. May be something like:

Code:
str_variable_1.value = ds_list_create();
Not entirely sure if this is relevant, but I found this, and it might just be that this is not possible. Wish it was, I mean the value thing could make a lot of sense, could it not?
 

Homunculus

Member
As far as I know, there's no way to do this. If you need to dynamically set key / value pairs, as mentioned by you a ds_map is the right choice. What's wrong with your current system anyway?
 

TheSnidr

Heavy metal viking dentist
GMC Elder
Well, you could use ds_maps. Something like this:
Code:
map = ds_map_create();

str_variable_1 = "this_string";

//Now you can do this
map[? "this_string"] = ds_list_create();
//or this
map[? str_variable_1] = ds_list_create();
 

XirmiX

Member
As far as I know, there's no way to do this. If you need to dynamically set key / value pairs, as mentioned by you a ds_map is the right choice. What's wrong with your current system anyway?
I actually can, I just got confused with the whole projectile system, because there's quite a lot to take into account when trying to stitch things together like I want to, like projectile instance on server and client side, and plenty more jargon :p

And since there's multiple ways to declare this, I gotta think about my identifiers before I set them in motion.

But huh, okay, well, if that's not an option, it's okay.

In GMS 2:

variable_instance_set()

http://docs2.yoyogames.com/index.ht...variable_functions/variable_instance_set.html

e.g.:
variable_instance_set(id, "this_string", ds_list_create());
That seems interesting, though not what I'm looking for. But I also don't see the point of this really, I mean, you can just do something like:
instance_id.[variable name of the instance] = [whatever value];

So, unless it's faster, which I doubt, why use a function, when you can just do it in a simple variable format?

Well, you could use ds_maps. Something like this:
Code:
map = ds_map_create();

str_variable_1 = "this_string";

//Now you can do this
map[? "this_string"] = ds_list_create();
//or this
map[? str_variable_1] = ds_list_create();
Huh, didn't know that. I think this might actually help me at some point, cheers!

Hi XirmiX, what platform is your game for?
Why?
 
A

Adam Tompkins

Guest
If your making a networking game and hoping to release on Apple, you will not be successful due to IPv6-only issues with Game Maker Studio.
 

TheouAegis

Member
variable_instance_set would be very useful, except it is a compatibility script and the manual says not to use them. If it works, it is probably going to be a lot slower than just using a map as the other people have suggested. It would be a parsing function just like the old execute_string() function, which was also slow. And one of the things I did not like about that function was that it basically mint everything from the IDE was kept in the compiled program rather than fully compiled. However if the variables are fully compiled then that function shouldn't work properly as intended here.
 

ar_xiv

Member
Hey there. I'm trying to create 100 variables all with the value 0. What would be the best way for me to do this? Doing in create code, so not so big a performance worry.
GML:
for (var i = 0; i < 100; i++) {
    c+string(i) = 0;
}
Want to return
GML:
c0 = 0;
c1 = 0;
c2 = 0;
...
EDIT: I'm just going to be normal and create an array:
GML:
c = array_create(100, 0);
 
Last edited:

Nidoking

Member
The correct way would be to use an array. Many people have tried to explain why they want variables instead of entries in an array, but none of those explanations have had any merit.
 

FrostyCat

Member
Hey there. I'm trying to create 100 variables all with the value 0. What would be the best way for me to do this? Doing in create code, so not so big a performance worry.
GML:
for (var i = 0; i < 100; i++) {
    c+string(i) = 0;
}
Want to return
GML:
c0 = 0;
c1 = 0;
c2 = 0;
...
The best way for you to do this is to create a single array instead of 100 separate variables.
GML:
c = array_create(100, 0);
Then you refer to the entries like c[0] instead of like c0.

Whenever rookies want to make variable names out of string parts or consecutive numbers, most of the time it is just bad architecture, not an actual necessity. Structs and arrays should be used instead.

Also, make a new topic for your own questions, don't bump up old topics from 4 years ago.
 
Top