How can i convert a string into a variable?

S

Skortcher

Guest
I have two variables: global.level1 and global.level2. I want to be able to reference both variables using a for loop and writing level = "global.level" + string(i) /* i is either 1 or 2 */ and then converting level which holds a string value into a variable. Thanks for the help.

Mod edit: If you are here from search, this is now possible via variable_global_ and variable_instance_ functions (or asset_get_index for assets), but if you are trying to append a number to a variable name, you should probably be using an array instead.
 
Last edited by a moderator:
W

whale_cancer

Guest
real( str ) is the function you are looking for.

As an side, not knowing exactly what you are doing, I find it results more predictable and organized when I handle things as reals and then convert to strings only when displaying information to the player.
 

jo-thijs

Member
@whale_cancer, you misinterpreted the question,
the OP wants to have a string set to a variable name and then retrieve thevalue of that variable.

@Skortcher, in GM:S, this isn't possible.
However, it sounds like arrays will help you do what you want to do.
You can write this:
Code:
for(var i = 0; i < 2; i++) {
    level = global.level[i];
    ...
}
and don't have the variables global.level1 and global.level2,
but rather assign a value to global.level[0] and global.level[1].
 
J

Jaqueta

Guest
I think that in your case, it would be better to use an Array.
Code:
global.level[i]
EDIT: freakin ninjas xD
 
Top