1. Hey! Guest! The 32nd GMC Jam will take place between Feb 22nd, 12:00 UTC (Friday noon) and Feb 25th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

Help! Level Creator Game: Loading and Saving

Discussion in 'Programming' started by NoFontNL, Feb 11, 2019.

  1. NoFontNL

    NoFontNL Member

    Joined:
    Feb 10, 2018
    Posts:
    170
    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!
     
  2. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    2,994
    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.
     
    NoFontNL likes this.

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