Badwrong

GMS2 Save System

May 9th, 2021
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #macro FILE_SYSTEM "system.ext"    // Edit for your game
  2. #macro FILE_EXT ".ext"             // Edit for your game
  3. #macro TIMESTAMP [current_year, current_month, current_day, current_hour, current_minute]
  4.  
  5.  
  6. /////// SAVE LOAD FUNCTIONS ////////////
  7.  
  8. function FileSaveMap(_filename, _map, _encode)
  9. {  
  10.     // Saves a DS _map to _filename.  _encode true or false
  11.  
  12.     var _string = json_encode(_map);
  13.     if _encode _string = base64_encode(_string);
  14.     var _buffer = buffer_create(string_byte_length(_string)+1, buffer_fixed, 1);
  15.     buffer_write(_buffer, buffer_string, _string);
  16.     buffer_save(_buffer, _filename);
  17.     buffer_delete(_buffer);
  18. }
  19.  
  20. function FileLoadMap(_filename, _decode)
  21. {
  22.     // Loads a file (json) and returns it as a DS map/list    
  23.     // _decode true or false depending on how you saved it previously
  24.  
  25.     if is_undefined(_filename) || !file_exists(_filename) return noone;
  26.    
  27.     var _buffer = buffer_load(_filename);
  28.     var _map = buffer_read(_buffer, buffer_string);
  29.     buffer_delete(_buffer);
  30.     if _decode _map = base64_decode(_map);
  31.     return json_decode(_map);
  32. }
  33.  
  34.  
  35. /////// SYSTEM AND PROFILES //////////
  36.  
  37. function FileLoadSystem()
  38. {
  39.     // Attempts to load the system file, or makes a default one
  40.  
  41.     var _system = FileLoadMap(FILE_SYSTEM, true);
  42.     if (_system == noone)
  43.     {
  44.         // Create System Defaults add more for your game
  45.         _system = ds_map_create();
  46.         _system[? "VOLUME_BGM"] = 0.5;
  47.         _system[? "VOLUME_SFX"] = 0.5;
  48.        
  49.         FileSaveMap(FILE_SYSTEM, _system, true);
  50.     }
  51.    
  52.     // Returns a DS map with system info
  53.     return _system;
  54. }
  55.    
  56. function FileListByExtension(_ext, _list)
  57. {
  58.     // Creates a list of files of the same extension
  59.     // This does not load the files, it just compiles their names as strings
  60.     // Optional _list for a previously created ds_list
  61.  
  62.     if is_undefined(_list) _list = ds_list_create();
  63.     else ds_list_clear(_list);
  64.    
  65.     var _file = file_find_first("*"+_ext, 0);
  66.    
  67.     while (_file != "")
  68.     {
  69.         ds_list_add(_list, _file); 
  70.         _file = file_find_next();
  71.     }
  72.     return _list;
  73. }
  74.    
  75. function FileLoadProfile(_file)
  76. {
  77.     // Loads a profile by file name or creates a default one
  78.     // Must declare global.ProfileList as a ds_list somewhere first
  79.     // Must declare global.Sytem as a ds_map first
  80.  
  81.     if is_undefined(_file) _file = global.ProfileList[| 0];
  82.     var _map = FileLoadMap(_file, true);
  83.     if (_map == noone)
  84.     {
  85.         // Make a default profile
  86.         _map = PlayerCreateProfile();
  87.         FileSaveMap(_map[? "NAME"]+FILE_EXT, _map, true);
  88.         global.System[? "DEFAULT_PROFILE"] = _map[? "FILE"];
  89.         FileSaveMap(FILE_SYSTEM, global.System, true);
  90.     }
  91.     return _map;
  92. }
  93.  
  94. function PlayerCreateProfile()
  95. {
  96.     // Creates a default player profile and returns it as a ds_map
  97.  
  98.     var _new = ds_map_create();
  99.     var _name = "save_"+NumToPaddedString(ds_list_size(global.ProfileList) + 1, 3);
  100.     _new[? "NAME"] = _name;
  101.     _new[? "FILE"] = _name+FILE_EXT;
  102.     _new[? "TIME_CREATED"] = TIMESTAMP;
  103.     _new[? "TIME_SAVED"]   = TIMESTAMP;
  104.  
  105.     // Some example things to add to a new profile
  106.     ds_map_add_map(_new, "STATS", ds_map_create());
  107.     ds_map_add_map(_new, "KEYS", ds_map_create());
  108.     return _new;
  109. }
  110.  
  111. ////// Helper Functions ///////
  112.  
  113. function NumToPaddedString(_num, _cnt)
  114. {
  115.     // Creates a padded string with _num an adds zeros according to _cnt
  116.     // for example,  _num = 5, _cnt = 3 will return 005
  117.     var _pad = "";
  118.     repeat (_cnt) _pad = string_insert("0", _pad, string_length(_pad)+1);
  119.     _pad = string_insert(_pad, string(_num), 1);
  120.     return string_copy(_pad, string_length(_pad) - _cnt + 1, _cnt);
  121. }
  122.  
  123.  
Add Comment
Please, Sign In to add comment