Advertisement
Guest User

Save&Load System for CopperLicht Game Engine

a guest
May 26th, 2018
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Important Advice:
  2. //For this example, I use simple numbers because I want you to be able to learn how to use this,
  3. //but you shouldn't actually use simple numbers in your Save File, because people can open it
  4. //and edit it by writing "500" and already have 500 coins without earning them.
  5. //So, even though I use simple numbers for this example, you should create a way to encrypt them
  6. //in your own head. For example, if the number of the first line of the text file equals the
  7. //highest level unlocked by your customer, you should think of your own symbols to hide the number.
  8. //Instead of the number "50" meaning all levels have been unlocked, I would put "zxpx2a" instead.
  9. //You should keep a notebook that will allow you to know what the symbols mean.
  10. //And if you think you need real encryption, you actually don't, because for professional games,
  11. //One person could finish the whole game fairly and unlock all items fairly, but he might
  12. //share that Save File on the internet, so all people could copy it and have all items unlocked.
  13. //That's why you don't need to use real encryption to hide the progress. Just use your own symbols
  14. //that will deter most people from just writing "100" on every line of the Save File and having
  15. //all items unlocked.
  16. //You should obviously use real encryption for multi-player online games, but I only enjoy
  17. //single-player games, so I don't have advice for that. And if you're attempting to create a fair
  18. //and balanced multi-player online game, you probably have already decided to NOT use the
  19. //WebGL & JavaScript platform, because the source code can be viewed by everyone.
  20.  
  21.  
  22.  
  23. //This script works well in Firefox, Chrome, and the Edge browser.
  24. //It doesn't work in Internet Explorer, but wise people won't use that one.
  25.  
  26.  
  27.  
  28. //Usage instructions:
  29. //Put this script in a text editor and save it as a file called "SaveAndLoadData.js".
  30. //Put it in your source directory with the other ".js" files, and attach the line below to your main script
  31. //by putting it in the HTML section at the top (don't include the two forward slashes at the beginning
  32. //of the line, of course):
  33. //<script type="text/javascript" src="src/SaveAndLoadData.js"></script>
  34. //
  35. //
  36. //(I don't know how to add this to the "compiled" version of CopperLicht, which combines the separate source
  37. // files as one huge ".js" file, so I can't help you do that.)
  38. //
  39. //
  40. //When you want to Save the game, use this:
  41. //SaveFileDialog();
  42. //
  43. //
  44. //When you want to load the game, use this:
  45. //LoadFileDialog();
  46. //
  47. //
  48. //You will definitely need to modify this script to attach all of the variables that you want to be included in
  49. //the Saves of your game. Please read all of the comments in this file for more information.
  50.  
  51.  
  52.  
  53.  
  54.  
  55. //The information in the variable below will be added to the Save File. I used this for testing.
  56. var DataToPlaceInFile = null;
  57.  
  58. var textFile = null;
  59.  
  60. //I will show you how to use the two lines below, so you can check the current progress of a player.
  61. ThePlayerHasReachedThisLevelSoFar = 5;
  62. NumberOfCoinsAcquiredByPlayerSoFar = 40;
  63.  
  64. makeTextFile = function (text) {
  65.  
  66.     var data = new Blob([text], {type: 'text/plain'});
  67.  
  68. // If we are replacing a previously generated file we need to
  69. // manually revoke the object URL to avoid memory leaks.
  70.     if (textFile !== null) {
  71.         window.URL.revokeObjectURL(textFile);
  72.     }
  73.  
  74.     textFile = window.URL.createObjectURL(data);
  75.  
  76.     return textFile;
  77. };
  78.  
  79.  
  80. var fileReader = new FileReader();
  81.  
  82.  
  83. var create = document.getElementById('create'),
  84. textbox = document.getElementById('textbox');
  85.  
  86. SaveFileDialog = function () {
  87.     var SaveDataToATextFile = document.createElement('a');
  88.     SaveDataToATextFile.setAttribute('download', 'savefile.txt');
  89.    
  90.     //Imagine this: While playing the game, the player has reached level 2, and he has 40 coins.
  91.     //During part of your game's script, you will have a variable that stores the highest level
  92.     //reached, and the number of coins acquired. So, imagine that your variables for those parts
  93.     //are called "ThePlayerHasReachedThisLevelSoFar" and "NumberOfCoinsAcquiredByPlayerSoFar".
  94.     //To combine them into one text file, I decided to first combine them into one large variable,
  95.     //called "DataToPlaceInFile". I'll add a backslash and a lowercase "n" after each line,
  96.     //like this: "\n". That will allow each part of the code to be written on a separate line.
  97.     //So, imagine that the previous status of the Save File showed that the highest level reached
  98.     //by the player is Level 2, and the number of coins acquired is 30. Now, imagine that the
  99.     //player reached Level 5, and he has 40 coins now. Those variables should be done by your
  100.     //own script, but when you modify this "SaveAndLoadData" file so it can capture those variables,
  101.     //it would work like this example:
  102.     DataToPlaceInFile = ThePlayerHasReachedThisLevelSoFar + "\n" + NumberOfCoinsAcquiredByPlayerSoFar;
  103.    
  104.     //Now, we will start the process of creating the Save File.
  105.     SaveDataToATextFile.href = makeTextFile(DataToPlaceInFile);
  106.     document.body.appendChild(SaveDataToATextFile);
  107.  
  108.     // wait for the link to be added to the document
  109.     window.requestAnimationFrame(function ()
  110.     {
  111.         var SimulateAMouseClick = new MouseEvent('click');
  112.         SaveDataToATextFile.dispatchEvent(SimulateAMouseClick);
  113.         document.body.removeChild(SaveDataToATextFile);
  114.     });
  115.  
  116. }, false;
  117.  
  118.  
  119. var LoadDataFromSaveFile = document.createElement("input");
  120. LoadDataFromSaveFile.setAttribute("id", "SelectFileToImport");
  121. LoadDataFromSaveFile.setAttribute("type", "file");
  122.  
  123.  
  124. LoadFileDialog = function()
  125. {
  126.     var SimulateAMouseClick = new MouseEvent('click');
  127.     LoadDataFromSaveFile.dispatchEvent(SimulateAMouseClick);   
  128. }
  129.  
  130.  
  131. LoadDataFromSaveFile.onchange = function(e) {
  132.  
  133.     file = LoadDataFromSaveFile.files[0];
  134.  
  135.     fileReader.onload = function(){
  136.        
  137.         // Show contents of entire file as one string, for testing.
  138.         //console.log(this.result);
  139.  
  140.         // Separate contents of file into separate lines, so you can manipulate them later.
  141.         var lines = this.result.split(/[\r\n]+/g); // tolerate Windows linebreaks and Unix linebreaks.
  142.         for(var line = 0; line < lines.length; line++){
  143.             //I used the line below to make the lines appear in the console, just to test the code.
  144.             console.log(lines[line]);
  145.         }
  146.  
  147.        
  148.         //Line 1 of the text file contains the highest level that has been reached by the player.
  149.         //During the loading process, I will store it in a separate variable, so the game will start
  150.         //with these accomplishments already unlocked and available.
  151.         //Note: For Computers, the first item in lists is always zero instead of one.
  152.         //That's why the code says "if (line = 0)" instead of "if (line = 1)".
  153.         ThePlayerHasReachedThisLevelSoFar = lines[0];
  154.            
  155.         //Now, I will put the value of Line 2 into a separate variable.
  156.         //Line 2 stores the number of coins acquired by the player when he last saved the game.
  157.         NumberOfCoinsAcquiredByPlayerSoFar = lines[1];
  158.        
  159.        
  160.     };
  161.     fileReader.readAsText(file);
  162.     e.target.value = null;
  163. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement