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

Help! Level Creator Game: Loading and Saving

N

NoFontNL

Guest
Hi, when I try to load my level, sometimes, it ignores blocks and just leaves them blank.
Here's my code:
Code:
///scOpenLevelEdit(name) 

// Open a saved level

var fileName=argument[0];
instance_activate_object(par_couele);
if(file_exists(fileName)){
   
    with(par_couele){
        instance_destroy();
    }
    with(par_solid){
        instance_destroy();
    }
    //decrypt
    fast_file_key_crypt(fileName,fileName,true,"key");
    var levelLoadFile = file_text_open_read(fileName);
    if(!file_text_eof(levelLoadFile)){
        var levelMasterData=file_text_read_string(levelLoadFile);
        var levelArray=string_split("|",levelMasterData,true);
        var coueleCount=array_length_1d(levelArray);
       
        if(coueleCount>0){
            for(var i=0; i<coueleCount; i++){
                var instanceJson = levelArray[i];
               
                var instanceMap = json_decode(instanceJson);
                var _x = ds_map_find_value(instanceMap,"x");
                var _y = ds_map_find_value(instanceMap,"y");
                var _obj = real(ds_map_find_value(instanceMap,"obj"));
                  var _inst = instance_create(_x, _y, _obj);
                 if(_obj==obj_itemblockEditor || _obj==obBrickEditor){
                   _inst.item=ds_map_find_value(instanceMap,"item");
                }
               
                 
                ds_map_destroy(instanceMap);
            }
        }
    }
      file_text_close(levelLoadFile);
      fast_file_key_crypt(fileName,fileName,false,"key");
}
Code:
///scSaveLevel(name)

// Save a level

  instance_activate_object(par_couele);
var coueleCount= instance_number(par_couele);
var fileName=argument[0];
if(file_exists(fileName)){
    file_delete(fileName);
}
var saveFile = file_text_open_write(fileName);
for(var i=0; i<coueleCount; i++){
    var instance=instance_find(par_couele,i);
   
    //convert into map
    var instanceMap = ds_map_create();
    ds_map_add(instanceMap,"x" ,instance.x );
    ds_map_add(instanceMap,"y" ,instance.y );
    ds_map_add(instanceMap, "obj", instance.object_index);
    if(instance.object_index==obj_itemblockEditor || instance.object_index == obBrickEditor){
        ds_map_add(instanceMap,"item",instance.item);
    }
    jsonInstance=json_encode(instanceMap);
   
    file_text_write_string(saveFile,jsonInstance);
    file_text_write_string(saveFile,"|")
   
    ds_map_destroy(instanceMap);
}
file_text_close(saveFile);
fast_file_key_crypt(fileName,fileName,false,"key");
I do have a camera object, which deactivates par_couele's when they're outside the view. The blocks that disappear are always just not in the view. But why do they disappear when I'm activating all par_couele's at the start of each script?

If you found something please let me know!
 

Simon Gust

Member
The last time I did level editors, strange similar problems occured.
The reason being instance_activate_object() and probably other activation functions.
They take a step before everything is activated. So you probably end up not saving every instance of par_couele

To fix that, when I pressed "save" I called the activation and an alarm at the same time.
And in that alarm, the save script.
 
Top