GameMaker SOLVED: Convert string containing variable name

Bee

Member
Hello all,

I've started learning about creating a json file and decoding the contents. I have a column that contains a variable name, eg. "obj_player.expr". How do I convert the string from the file to the actual variable? Asset_get_index doesn't seem to work.

Thanks!
 

FrostyCat

Redemption Seeker
Split that down the middle by the dot. Use the first half with asset_get_index(), and the second half with instance_find() followed by variable_instance_set() (GMS 2.x) or an equivalent script (GMS 1.x).
 
  • Like
Reactions: Bee

Bee

Member
Oooh that's great, thank you! So I've written the code below and it's still not registering. Is there anything about the code that you can think would cause problems?

Code:
var goalsMap = json_decode(goalsData);
var goalsList = ds_map_find_value(goalsMap, "default");
global.num_goals = ds_list_size(goalsList);
global.goals = ds_grid_create(global.num_goals, 11);

for (var i=0; i<global.num_goals; i++) {
    var data = ds_list_find_value(goalsList, i);
    global.goals[# i, GL_NUM] = real(data[? "Num"]);
    global.goals[# i, GL_TYPE] = real(data[? "Type"]);
    global.goals[# i, GL_LVL] = real(data[? "Level"]);
    global.goals[# i, GL_OBJ] = asset_get_index(data[? "Object"]);
    global.goals[# i, GL_VAR] = data[? "Var"];
    global.goals[# i, GL_MAX] = real(data[? "Max"]);
    global.goals[# i, GL_DESC] = data[? "Desc"];
    global.goals[# i, GL_REWARD_OBJ] = asset_get_index(data[? "Reward_Object"]);
    global.goals[# i, GL_REWARD_VAR] = data[? "Reward_Var"];
    global.goals[# i, GL_REWARD_ADJ] = real(data[? "Reward_Adj"]);
    global.goals[# i, GL_REWARD_DESC] = data[? "Reward_Desc"];
}


for (var i=0; i<global.num_goals; i++) {
    if (global.goals[# i, GL_LVL] == goal_type_lvl[global.goals[#i, GL_TYPE]]) {
        var curr_count = variable_instance_get(global.goals[# i, GL_OBJ], global.goals[# i, GL_VAR]);
        if (curr_count >= global.goals[# i, GL_MAX]) {
            goal_type_lvl[global.goals[# i, GL_TYPE]]++;
            var obj = global.goals[# i, GL_REWARD_OBJ];
            for (var j=0; j<instance_number(obj); j++) {
                var inst = instance_find(obj, j);
                var curr_value = variable_instance_get(inst, global.goals[# i, GL_REWARD_VAR]);
                variable_instance_set(inst, global.goals[# i, GL_REWARD_VAR], curr_value+global.goals[# i, GL_REWARD_ADJ]);
            }
        }
    }
}
 

Bee

Member
I should mention I know it's getting the data because the descriptions are printing correctly. Just the "levelling up" code from the block below doesn't seem to be working.
 

Bee

Member
I got it. Had to put an instance_find to get the instance ID of the object to pass into that first variable_instance_get. Thanks again!
 
Top