• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Putting variable in a variable?

B

Butter

Guest
Hello, I'm new and just looking for some help. I'm wondering if its possible to put a variable in a variable?
Example: skin_stand_down = spr_stand_down[global.skin];
I'm trying to get skin_stand_down to be read as spr_stand_down0 etc.
If this isnt possible is their any other solution for having a ton of different sprite, like different types of skin, to be coded to be set as the sprite after being chosen by the player?
 

Relic

Member
Consider setting up an array to contain the information you want to call on

sprite_array[0]=spr_stand_down0
sprite_array[1]=spr_stand_down1;
etc...

Then, since you are usinga global variable to keep a hold of which skin option has been chosen by the player, you can find which sprite is required by doing
skin_stand_down = sprite_array[global.skin] (which is spr_stand_down0 if global.skin=0)
 

Smiechu

Member
Why you just don't try it yourself??? Your thinking is correct.
I assume what you want to do is to create an array in the create event of your controller object ie:
global.skin_stand[0] = spr_stand_0;
global.skin_stand[1] = spr_stand_1;
etc...
Than you want a global variable which indicates which set is currently chosen.
global.skin = 1;
Than in your player object in step event you set the sprite like this:
skin_stand = global.skin_stand[global.skin];

It looks ok... where is the problem?
 

chamaeleon

Member
@Butter When you need to do to do this for assets (sprites, objects, rooms, etc.) and not variables declared in code you can use asset_get_index().
Code:
skin_stand_down = asset_get_index("spr_stand_down" + string(global.skin));
 
Top