Load Script

Hi

Does anyone see any thing wrong with this load script? Everything executes but there are no changes when I return to my main room:


Code:
with (SentryBatObject)
{
    
    instance_destroy();   
    
    
}









if (file_exists("sentry.sav"))
{
    
    var loadFile = file_text_open_read("sentry.sav");
    
    if (!file_text_eof(loadFile))
    {
        //masetrData one long string
        masterData = file_text_read_string(loadFile);
        
        var SentryArray  = scr_split(masterData, "|");
        var SentryCount = array_length_1d(SentryArray);
        
        if (SentryCount > 0)
        {
            //freach sentry
            for (var i = 0; i < SentryCount; i++)
            {
                var SentryJSON = sentryArray[i];
                //deserialize
                var sentryMap = json_decode(SentryJSON);
                
                //extract values
                var _x = ds_map_find_value(sentryMap, "x");
                var _y = ds_map_find_value(sentryMap, "y");
                var _target_placement_x = ds_map_find_value(sentryMap, "target_placement_x");
                var _target_placement_y = ds_map_find_value(sentryMap, "target_placement_y");
                var _flight = ds_map_find_value(sentryMap, "flight");
                var _sprite_index  = ds_map_find_value(sentryMap, "sprite_index");
                var _new_bat = ds_map_find_value(sentryMap, "new_bat");
                var _snap = ds_map_find_value(sentryMap, "snap");
                var _hp = ds_map_find_value(sentryMap, "hp");
                var _damage = ds_map_find_value(sentryMap, "damage");
                var _killed_a_brain = ds_map_find_value(sentryMap, "killed_a_brain");
                var _opponent = ds_map_find_value(sentryMap, "opponent");
                var _under_attack = ds_map_find_value(sentryMap, "under_attack");
                var _global_current_collision_instance = ds_map_find_value(sentryMap, "global.current_collision_instance");
                var _colliding_flag = ds_map_find_value(sentryMap, "colliding_flag");
                var _global_dropped_bat = ds_map_find_value(sentryMap, "global.dropped_bat");
                var _speed = ds_map_find_value(sentryMap, "speed");
                var _hp_minus = ds_map_find_value(sentryMap, "hp_minus");
                var _took_a_hit = ds_map_find_value(sentryMap, "took_a_hit");
                            
            
                //create a sentry bat
                var inst;
                inst = instance_create_depth(x, y, 0, SentryBatObject);
                
                inst.x = _x;
                inst.y = _y;
                inst.target_placement_x = _target_placement_x;
                inst.target_placement_y = _target_placement_y;
                inst.flight = _flight;
                inst.sprite_index = _sprite_index;
                inst.new_bat = _new_bat;
                inst.snap = _snap;
                inst.hp = _hp;
                inst.damage = _damage;
                inst.killed_a_brain = _killed_a_brain;
                inst.opponent = _opponent;
                inst.under_attack = _under_attack;
                global_current_collision_instance = _global_current_collision_instance;
                inst.colliding_flag = _colliding_flag;
                global.dropped_bat = _global_dropped_bat;
                inst.speed = _speed;
                inst.hp_minus = _hp_minus;
                inst.took_a_hit = _took_a_hit;
                
            }
        }
    }
    
}


file_text_close(loadFile);
ds_map_destroy(sentryMap);
 

sylvain_l

Member
IDK, but I don't feel it's right to have your local var declaration in a for loop. (I don't do it, and GMS is so forgiving, so it can be OK)

else, as TheouAegis I suspect you are just creating the instance in the wrong room...
I return to my main room:
if you are creating the instance in a "loading room" and then go back to your "main room"; of course, you won't see the loaded instances; as you create them in the loading room you just left.
 

Paskaler

Member
Store the file name you want to load in a global variable, and then run the loading code after you switched rooms
 
Ok I did that. So, it is saving to a JSON file like it is supposed to but my load function doesn't seem to work. Can you look over it and see if there are any glaring errors?

Code:
if (file_exists("sentry.sav"))
{
    
    var loadFile = file_text_open_read("sentry.sav");
    
    if (!file_text_eof(loadFile))
    {
        //masetrData one long string
        masterData = file_text_read_string(loadFile);
        
        var SentryArray  = scr_split(masterData, "|");
        var SentryCount = array_length_1d(SentryArray);
        
        if (SentryCount > 0)
        {
            //freach sentry
            for (var i = 0; i < SentryCount; i++)
            {
                var SentryJSON = sentryArray[i];
                //deserialize
                var sentryMap = json_decode(SentryJSON);
                
                //extract values
                var _x = ds_map_find_value(sentryMap, "x");
                var _y = ds_map_find_value(sentryMap, "y");
                var _target_placement_x = ds_map_find_value(sentryMap, "target_placement_x");
                var _target_placement_y = ds_map_find_value(sentryMap, "target_placement_y");
                var _flight = ds_map_find_value(sentryMap, "flight");
                var _sprite_index  = ds_map_find_value(sentryMap, "sprite_index");
                var _new_bat = ds_map_find_value(sentryMap, "new_bat");
                var _snap = ds_map_find_value(sentryMap, "snap");
                var _hp = ds_map_find_value(sentryMap, "hp");
                var _damage = ds_map_find_value(sentryMap, "damage");
                var _killed_a_brain = ds_map_find_value(sentryMap, "killed_a_brain");
                var _opponent = ds_map_find_value(sentryMap, "opponent");
                var _under_attack = ds_map_find_value(sentryMap, "under_attack");
                var _global_current_collision_instance = ds_map_find_value(sentryMap, "global.current_collision_instance");
                var _colliding_flag = ds_map_find_value(sentryMap, "colliding_flag");
                var _global_dropped_bat = ds_map_find_value(sentryMap, "global.dropped_bat");
                var _speed = ds_map_find_value(sentryMap, "speed");
                var _hp_minus = ds_map_find_value(sentryMap, "hp_minus");
                var _took_a_hit = ds_map_find_value(sentryMap, "took_a_hit");
                            
            
                //create a sentry bat
                var inst;
                inst = instance_create_depth(x, y, 0, SentryBatObject);
                
                inst.x = _x;
                inst.y = _y;
                inst.target_placement_x = _target_placement_x;
                inst.target_placement_y = _target_placement_y;
                inst.flight = _flight;
                inst.sprite_index = _sprite_index;
                inst.new_bat = _new_bat;
                inst.snap = _snap;
                inst.hp = _hp;
                inst.damage = _damage;
                inst.killed_a_brain = _killed_a_brain;
                inst.opponent = _opponent;
                inst.under_attack = _under_attack;
                global_current_collision_instance = _global_current_collision_instance;
                inst.colliding_flag = _colliding_flag;
                global.dropped_bat = _global_dropped_bat;
                inst.speed = _speed;
                inst.hp_minus = _hp_minus;
                inst.took_a_hit = _took_a_hit;
                
            }
        }
    }
    
}


file_text_close(loadFile);
ds_map_destroy(sentryMap);
 

TheouAegis

Member
This code runs inside of a script, right? If so, then post your code that calls that script. If this code is not inside of a script, that post the code that runs before and after this code.
 

sylvain_l

Member
was curious so did a silly test. (edit: was to test if the local var declare in a loop was ok.. and yes GMS2 is OK with it)

let's be clear your loading code is never run! Else you would have seen a crash debug error window complaining about the fact that at line 24
Code:
var SentryJSON = sentryArray[i];
you are assigning an non array index blablabloa
because you declare your local var as SentryArray (with an upercase S not a lowercase).

so as other asked you, what's the code around; that should call that loading code. because at the moment it's feels to me, it's not run at all.



and juste a side note; if you are really using an instance ID here:
Code:
var _global_current_collision_instance = ds_map_find_value(sentryMap, "global.current_collision_instance");
very bad idea; never save an instance ID and reload it; it can work in rare cases. But most of the cases it's just a call to very nasty bug. (there are lot of things that can made your instances to have a different ID at each run.
 
Last edited:
Ok. Code around.

LOADGAME OBJECT (Clicking this sets a flag that when you return to the main room loads the game)

Step Event
Code:
//room_goto(global.curent_room);
draw_text(x,y,"Game Loaded");
global.loading_flag = true;
Draw Event
Code:
if (global.option_selected == 2)
{
    draw_set_colour(c_red);   
}

else
{
    draw_set_colour(c_white);
}


draw_set_font(LargeRavie);
draw_text(x,y, "Load Game" );
draw_set_font(Arial_Font_Modified);
Left Down Event

Code:
//room_goto(global.curent_room);
draw_text(x,y,"Game Loaded");
global.loading_flag = true;
PLAYER OBJECT

Step Event

The step event among othe rthings calls the load script if the global_loading_flag = true; (if it was set to load in the loading screen)

Code:
//////////////Code related to saving and loading////////////////

global.current_room = room;

if (global.loading_flag == true)
{
    show_debug_message("Loading Flag");
    scr_load();
    global.loading_flag = false;
}

if (global.saving_flag == true)
{
    show_debug_message("Saving Flag");
    scr_save();
    global.saving_Flag = false;
}
 
Top