1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

GMS 2 SOLVED: Convert string containing variable name

Discussion in 'Programming' started by Bee, Aug 1, 2017.

Tags:
  1. Bee

    Bee Member

    Joined:
    Oct 28, 2016
    Posts:
    106
    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!
     
  2. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,694
    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).
     
    Bee likes this.
  3. Bee

    Bee Member

    Joined:
    Oct 28, 2016
    Posts:
    106
    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]);
                }
            }
        }
    }
    
     
  4. Bee

    Bee Member

    Joined:
    Oct 28, 2016
    Posts:
    106
    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.
     
  5. Bee

    Bee Member

    Joined:
    Oct 28, 2016
    Posts:
    106
    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!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice