Advertisement
pompasaur

save_game_data

Oct 30th, 2022
2,082
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Game Maker 5.74 KB | Source Code | 0 0
  1. ///@description save_game_data(file_name,overwrite_group_name1,overwrite_group_name2,overwrite_group_name3,overwrite_group_name4,overwrite_group_name5,overwrite_group_name6,overwrite_group_name7,overwrite_group_name8);
  2. ///@param file_name
  3. ///@param overwrite_group_name1
  4. ///@param [overwrite_group_name2]
  5. ///@param [overwrite_group_name3]
  6. ///@param [overwrite_group_name4]
  7. ///@param [overwrite_group_name5]
  8. ///@param [overwrite_group_name6]
  9. ///@param [overwrite_group_name7]
  10. ///@param [overwrite_group_name8]
  11. function save_game_data(_fileName,_overwrite_group1,_overwrite_group2 = "N/A",_overwrite_group3 = "N/A",_overwrite_group4 = "N/A",_overwrite_group5  = "N/A",_overwrite_group6 = "N/A",_overwrite_group7 = "N/A",_overwrite_group8 = "N/A"){
  12.     /*  "What is an overwrite group name?"
  13.        
  14.         For very simple games you may only need to save large-scale variables, such as level progress, or collectible progress.
  15.         But for more complex projects, you may need to save data that is exclusive to say, a room (such as preserving instance positions within a room),
  16.         or maybe you have multiple playable characters and you want to save their individual stats.
  17.        
  18.         You don't want to delete the ENTIRE save file because then all the other data that you aren't changing
  19.         (like rooms that you currently aren't in, or characters that you aren't currently playing)
  20.         will end up being lost in the process!
  21.        
  22.         That is why overwrite groups exist. You separate instances by the current room name, or by the current character object name, however
  23.         you want to divvy it up.
  24.        
  25.         If you don't need to group save data because your game is simple, no worries! Just use the same overwrite group name for everything!
  26.    
  27.     */
  28.    
  29.     var _saveData = array_create(0), //our array of structs that we're going to edit and then save into a json
  30.         _currentEntity = noone, //the current entity within the json array that we're reading
  31.        
  32.         _overwriteGroup = array_create(argument_count,"N/A"); //an array storing all the overwrite groups we are going to check
  33.         for (var i = 1; i < argument_count; i++){
  34.             _overwriteGroup[i-1] = argument[i];
  35.         }
  36.        
  37.    
  38.     //First we're going to pull data from the pre-existing file, if there is one
  39.     if (file_exists(_fileName)){
  40.         _saveData = parse_json_from_file(_fileName);
  41.        
  42.         //Now we're going to check if any of the overwrite groups we've specified are present in the pre-existing json
  43.         for (var i=0; i < array_length(_saveData); i++) {
  44.             _currentEntity = array_get(_saveData,i);
  45.             for (var g = 0; g < array_length(_overwriteGroup); g++){ //this loop is to check every overwrite group entry, since we can have up to 8
  46.                 if (_currentEntity.overwriteGroup == _overwriteGroup[g]){
  47.                     array_delete(_saveData,i,1);
  48.                     i -= 1;
  49.                 }
  50.             }
  51.         }
  52.     }
  53.    
  54.     //Now we're going to create our new save data
  55.     for (var g = 0; g < array_length(_overwriteGroup); g++){ //g is going to represent each overwrite group we're checking for
  56.         //INDIVIDUAL DATA FIELDS//
  57.         /*This is where we're going to add any new structs for individual data pieces we'll want to store in each overwrite group.
  58.         We can save instance data, global data, anything that you can fit into a struct you can put here.
  59.         It's important to remember that every object we add here, we'll need to add a instance_destroy function for it in the [load_game_data] script.
  60.         Also, for all data not related to objects, you'll need to add it into the [load_game_data] for it actually work.*/
  61.        
  62.         if (_overwriteGroup[g] == room_get_name(room)){
  63.             /*When you want to split save data into different overwrite groups, you'll need to use checks like these to ensure they're
  64.             separated properly. I would recommend using room names for groups that are split by room.*/
  65.            
  66.             //OBJECT EXAMPLE//
  67.             /*
  68.             This is entirely just to give an example of how you'll want to structure each new entry into this save script.
  69.             Feel free to delete "pSaveMeBasic" if you don't plan to use it.
  70.             It's vitally important that every single entry have an [overwriteGroup] and [loadType] variable.
  71.             If any entry doesn't have an [overwriteGroup] and [loadType] variable you will get a fatal error.
  72.             */
  73.             with(pSaveMeBasic) //with() will loop through every instance of the object_index given within the current room.
  74.             {
  75.                 _currentEntity =
  76.                 {
  77.                     overwriteGroup : room_get_name(room),
  78.                     loadType : "object", //this will tell us what variables to look for when loading in
  79.                     object : object_get_name(object_index), //We must save the NAME of any assets we preserve because their index can change as the game is updated
  80.                     y : y,
  81.                     x : x,
  82.                     hspeed : hspeed,
  83.                     vspeed : vspeed,
  84.                     visible : visible,
  85.                     sprite_index : sprite_index,
  86.                     image_index : image_index,
  87.                     image_speed : image_speed,
  88.                     image_xscale : image_xscale,
  89.                     image_yscale : image_yscale,
  90.                     image_angle : image_angle,
  91.                     image_blend : image_blend,
  92.                     image_alpha : image_alpha,
  93.                     depth : depth
  94.                 }
  95.                 array_push(_saveData,_currentEntity);
  96.             }
  97.            
  98.             //YOU'D PUT THE NEXT ENTRY FOR THIS OVERWRITE GROUP HERE//
  99.         }
  100.        
  101.         if (_overwriteGroup[g] == "globalData"){
  102.             /*As stated in the case above, these checks are vitally important to segment our overwrite groups to where we want them.
  103.             Here is an example of how you might want to do an overwrite group for data that isn't attached to specific room*/
  104.             _currentEntity =
  105.             {
  106.                 overwriteGroup : "globalData",
  107.                 loadType : "globalData", //this will tell us what variables to look for when loading in
  108.                 health : health,
  109.                 lives : lives
  110.             }
  111.             array_push(_saveData,_currentEntity);
  112.                
  113.             //YOU'D PUT THE NEXT ENTRY FOR THIS OVERWRITE GROUP HERE//
  114.         }
  115.     }
  116.  
  117.     //
  118.     var _string = json_stringify(_saveData);
  119.     save_string_to_file(argument0,_string);
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement