Advertisement
pompasaur

load_game_data

Oct 30th, 2022
2,268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Game Maker 4.37 KB | Source Code | 0 0
  1. ///@description load_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 load_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.     var _wipedObjects = false, //this will be changed to the overwrite group name after wiping objects within that group
  29.     _currentEntity = noone, //the current entry within the array that we're reading
  30.    
  31.     _overwriteGroup = array_create(argument_count,"N/A"); //an array storing all the overwrite groups we are going to check
  32.     for (var i = 1; i < argument_count; i++){
  33.         _overwriteGroup[i-1] = argument[i];
  34.     }
  35.    
  36.     if (file_exists(_fileName))
  37.     {
  38.         var _loadData = parse_json_from_file(_fileName); //we're gonna pull all data from the json and translate it back into an array
  39.        
  40.         //We're going to go through every entry in the JSON array
  41.         for (i=0; i < array_length(_loadData); i++) {
  42.             var _currentEntity = array_get(_loadData,i);
  43.             for (var g = 0; g < array_length(_overwriteGroup); g++){ //We're also to loop through every group to check each one
  44.                 if (_currentEntity.overwriteGroup == _overwriteGroup[g]){ //if we've found a group we intend to load from...
  45.                    
  46.                     //LOADING OBJECTS//
  47.                     /*The general idea is that, if you have individual instances that you want to save per room, it really only makes sense
  48.                      to name the overwrite group the room name.
  49.                      So, should we be loading instances from the overwrite group named after the room we're currently in,
  50.                      we don't want duplicates, so we'll delete all existing instances of objects we intend to save,
  51.                      and then recreate them while also giving them the values we saved in the JSON for them.*/
  52.                     if (_currentEntity.overwriteGroup == room_get_name(room)){
  53.                         if (!_wipedObjects){
  54.                             //PUT ALL OBJECTS YOU WANT TO SAVE/LOAD INTO THIS LIST OF DESTROYING OBJECTS!
  55.                             instance_destroy(pSaveMeBasic);
  56.                             instance_destroy(tmpPaletteSwap);
  57.                             //instance_destroy(oExample);
  58.                         }
  59.                         _wipedObjects = true; //We only want to do this once
  60.                     }
  61.                    
  62.                     //Now we're going to actually load in our data
  63.                     //OBJECTS
  64.                     /*This one block of code should cover all objects you'd want to recreate from the save data*/
  65.                     if (_currentEntity.loadType == "object"){ //If for some reason you don't want to use the name "object" for load type, just make sure it's consistent for all objects you want to load
  66.                         instance_create_depth(
  67.                             _currentEntity.x,
  68.                             _currentEntity.y,
  69.                             _currentEntity.depth,
  70.                             asset_get_index(_currentEntity.object),
  71.                             _currentEntity //We're going to load in the entire struct
  72.                         );
  73.                     }
  74.                    
  75.                     //FLOATING DATA
  76.                     if (_currentEntity.loadType == "globalData"){
  77.                         health = _currentEntity.health;
  78.                         lives = _currentEntity.lives;
  79.                     }
  80.                    
  81.                     //ADD ANY OTHER FLOATING DATA YOU WANT TO SAVE/LOAD HERE
  82.                 }
  83.             }
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement