• 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!

GameMaker [SOLVED] Is there a way to adress a variable from a string ? (asset_get_index)

Fleoh

Member
Hello guys, I've got a question.

In Game maker, there is a way to adress an object from a string with the function asset_get_index.

Exemple :
Code:
weapon = "pistol";
sprite_index = asset_get_index("spr_"+ weapon);
Is there a way to do the same thing with a variable ?

Exemple :
Code:
sell_pistol = 50;
weapon = "pistol";
price = asset_get_index("sell_" + weapon);
I've tried a bunch of things and noone of them works...
Thanks for your help !
 

johnwo

Member
Just use a map?

Code:
prices = ds_map_create();
prices[? "pistol"] = 100;

var weapon = "pistol";
sprite_index = asset_get_index("spr_"+ weapon);
var price = prices[?weapon];
 

Binsk

Member
variable_instance_get and variable_global_get.

However, it is poor design if you are needing these.
 

Fleoh

Member
@Binsk Yep, I've seen that theses functions are mostly use for compatibility scripts.. I'll use maps instead, it'll be better.
 
Top