Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Important Advice:
- //For this example, I use simple numbers because I want you to be able to learn how to use this,
- //but you shouldn't actually use simple numbers in your Save File, because people can open it
- //and edit it by writing "500" and already have 500 coins without earning them.
- //So, even though I use simple numbers for this example, you should create a way to encrypt them
- //in your own head. For example, if the number of the first line of the text file equals the
- //highest level unlocked by your customer, you should think of your own symbols to hide the number.
- //Instead of the number "50" meaning all levels have been unlocked, I would put "zxpx2a" instead.
- //You should keep a notebook that will allow you to know what the symbols mean.
- //And if you think you need real encryption, you actually don't, because for professional games,
- //One person could finish the whole game fairly and unlock all items fairly, but he might
- //share that Save File on the internet, so all people could copy it and have all items unlocked.
- //That's why you don't need to use real encryption to hide the progress. Just use your own symbols
- //that will deter most people from just writing "100" on every line of the Save File and having
- //all items unlocked.
- //You should obviously use real encryption for multi-player online games, but I only enjoy
- //single-player games, so I don't have advice for that. And if you're attempting to create a fair
- //and balanced multi-player online game, you probably have already decided to NOT use the
- //WebGL & JavaScript platform, because the source code can be viewed by everyone.
- //This script works well in Firefox, Chrome, and the Edge browser.
- //It doesn't work in Internet Explorer, but wise people won't use that one.
- //Usage instructions:
- //Put this script in a text editor and save it as a file called "SaveAndLoadData.js".
- //Put it in your source directory with the other ".js" files, and attach the line below to your main script
- //by putting it in the HTML section at the top (don't include the two forward slashes at the beginning
- //of the line, of course):
- //<script type="text/javascript" src="src/SaveAndLoadData.js"></script>
- //
- //
- //(I don't know how to add this to the "compiled" version of CopperLicht, which combines the separate source
- // files as one huge ".js" file, so I can't help you do that.)
- //
- //
- //When you want to Save the game, use this:
- //SaveFileDialog();
- //
- //
- //When you want to load the game, use this:
- //LoadFileDialog();
- //
- //
- //You will definitely need to modify this script to attach all of the variables that you want to be included in
- //the Saves of your game. Please read all of the comments in this file for more information.
- //The information in the variable below will be added to the Save File. I used this for testing.
- var DataToPlaceInFile = null;
- var textFile = null;
- //I will show you how to use the two lines below, so you can check the current progress of a player.
- ThePlayerHasReachedThisLevelSoFar = 5;
- NumberOfCoinsAcquiredByPlayerSoFar = 40;
- makeTextFile = function (text) {
- var data = new Blob([text], {type: 'text/plain'});
- // If we are replacing a previously generated file we need to
- // manually revoke the object URL to avoid memory leaks.
- if (textFile !== null) {
- window.URL.revokeObjectURL(textFile);
- }
- textFile = window.URL.createObjectURL(data);
- return textFile;
- };
- var fileReader = new FileReader();
- var create = document.getElementById('create'),
- textbox = document.getElementById('textbox');
- SaveFileDialog = function () {
- var SaveDataToATextFile = document.createElement('a');
- SaveDataToATextFile.setAttribute('download', 'savefile.txt');
- //Imagine this: While playing the game, the player has reached level 2, and he has 40 coins.
- //During part of your game's script, you will have a variable that stores the highest level
- //reached, and the number of coins acquired. So, imagine that your variables for those parts
- //are called "ThePlayerHasReachedThisLevelSoFar" and "NumberOfCoinsAcquiredByPlayerSoFar".
- //To combine them into one text file, I decided to first combine them into one large variable,
- //called "DataToPlaceInFile". I'll add a backslash and a lowercase "n" after each line,
- //like this: "\n". That will allow each part of the code to be written on a separate line.
- //So, imagine that the previous status of the Save File showed that the highest level reached
- //by the player is Level 2, and the number of coins acquired is 30. Now, imagine that the
- //player reached Level 5, and he has 40 coins now. Those variables should be done by your
- //own script, but when you modify this "SaveAndLoadData" file so it can capture those variables,
- //it would work like this example:
- DataToPlaceInFile = ThePlayerHasReachedThisLevelSoFar + "\n" + NumberOfCoinsAcquiredByPlayerSoFar;
- //Now, we will start the process of creating the Save File.
- SaveDataToATextFile.href = makeTextFile(DataToPlaceInFile);
- document.body.appendChild(SaveDataToATextFile);
- // wait for the link to be added to the document
- window.requestAnimationFrame(function ()
- {
- var SimulateAMouseClick = new MouseEvent('click');
- SaveDataToATextFile.dispatchEvent(SimulateAMouseClick);
- document.body.removeChild(SaveDataToATextFile);
- });
- }, false;
- var LoadDataFromSaveFile = document.createElement("input");
- LoadDataFromSaveFile.setAttribute("id", "SelectFileToImport");
- LoadDataFromSaveFile.setAttribute("type", "file");
- LoadFileDialog = function()
- {
- var SimulateAMouseClick = new MouseEvent('click');
- LoadDataFromSaveFile.dispatchEvent(SimulateAMouseClick);
- }
- LoadDataFromSaveFile.onchange = function(e) {
- file = LoadDataFromSaveFile.files[0];
- fileReader.onload = function(){
- // Show contents of entire file as one string, for testing.
- //console.log(this.result);
- // Separate contents of file into separate lines, so you can manipulate them later.
- var lines = this.result.split(/[\r\n]+/g); // tolerate Windows linebreaks and Unix linebreaks.
- for(var line = 0; line < lines.length; line++){
- //I used the line below to make the lines appear in the console, just to test the code.
- console.log(lines[line]);
- }
- //Line 1 of the text file contains the highest level that has been reached by the player.
- //During the loading process, I will store it in a separate variable, so the game will start
- //with these accomplishments already unlocked and available.
- //Note: For Computers, the first item in lists is always zero instead of one.
- //That's why the code says "if (line = 0)" instead of "if (line = 1)".
- ThePlayerHasReachedThisLevelSoFar = lines[0];
- //Now, I will put the value of Line 2 into a separate variable.
- //Line 2 stores the number of coins acquired by the player when he last saved the game.
- NumberOfCoinsAcquiredByPlayerSoFar = lines[1];
- };
- fileReader.readAsText(file);
- e.target.value = null;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement