GMS 2 [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