Reputation:
I am trying to make a game on Scratch that will use a feature to generate a special code, and when that code is input into a certain area it will load the stats that were there when the code was generated. Except I don't know how to make it and I couldn't find a clear answer for how to make it.
I would prefer that the solution be:
Upvotes: 2
Views: 5106
Reputation: 203
Here's a simpler method of saving and loading data:
griffpatch recently uploaded a video (Feb. 25) on how to write save data in an extremely simple way.
This uses 3 custom blocks (to make sure it doesn't take up as many blocks) with all set to "Run without screen refresh" so it takes as little time as possible (to simply put it, 1/60th of a second): Read Value and Write Value (text). I used this in my Cookie Clicker game (which I am still currently in the process of making, I sadly haven't made the code to check for illegal values but that is obviously not the question) so I could make a way to copy save data. I made the load save data function myself.
Here is a screenshot of the "Read Value" definition (sorry about not being able to add images yet I just joined so I could answer the question more clearly. Also remember that any variable seen is set to one sprite only.:
Here is a screenshot of the "Write Value (text)" function (parenthesis because it is defined as an input):
You also need to make sure that you write the value for all of the functions that need to be saved when something is clicked. (to broadcast a message to write the value of all of the functions, saved to a list from which you can copy from, and then you need to ask a question to make sure you have all of the code) For example:
Part 1 of the load save data function (You don't need to add anything to the bottom)
Part 3 of the load save data function (The bottom of it)
Then, you need to make sure that you have a way to upload your save progress. Here's the code for the 3rd custom block you will need:
This sets "c (character)" to the answer (the input for the save data) because the game needs to know what to input for each variable that you need saved.
Hope this helps!
Edit: Forgot to mention that you don't need to set the list to "for this sprite only." You will only need that for the variables
that any variable seen is set to one sprite only.
Edit 2: Sorry if my formatting confused you a bit, you have to set all three custom blocks to "Run without screen refresh"
This uses 2 custom blocks (to make sure it doesn't take up as many blocks) with both set to "Run without screen refresh" so it takes as little time as possible
Upvotes: 0
Reputation: 11
The technique you mentioned is used in many scratch games but there is two option for you when making the save/load system. You can either do it the simpler way which makes the code SUPER long(not joking). The other way is most scratchers use, encoding the data into a string as short as possible so it's easy to transfer.
If you want to do the second way, you can have a look at griffpatch's video on the mario platformer remake where he used a encode system to save levels.https://www.youtube.com/watch?v=IRtlrBnX-dY The tips is to encode your data (maybe score/items name/progress) into numbers and letters for example converting repeated letters to a shorter string which the game can still decode and read without errors
If you are worried it took too long to load, I am pretty sure it won't be a problem unless you really save a big load of data. The common compress method used by everyone works pretty well. If you want more data stored you may have to think of some other method. There is not an actual way to do that as different data have different unique methods for things working the best. Good luck.
Upvotes: 1
Reputation: 319
You can put all of the programs in a custom block with "Run without screen refresh" on so that the program runs instantly.
If you save the stats using variables, you could combine those variable values into one string divided by /s. i.e. join([highscore]) (join("/") (join([kills]) (/))
NOTE: Don't add any "/" in your stats, you can probably guess why.
Now "bear" (pun) with me, this is going to take a while to read
Then you need the variables:
[read]
for reading the inputted code
[input]
for storing the numbers
Then you could make another function that reads the code like so: letter ([read]) of (code)
and stores that information to the [input] variable like this: set [input] to (letter ([read]) of (code)). Then change [read] by (1)
so the function can read the next character of the code. Once it letter ([read]) of (code)
equals "/", this tells the program to set [*stat variable*] to (input)
(in our example, this would be [highscore]
since it was the first variable we saved) and set [input] to (0)
, and repeat again until all of the stats variables are filled (In this case, it repeats 2 times because we saved two variables: [highscore] and [kills]).
This is the least amount of code that it takes. Jumbling it up takes more code. I will later edit this answer with a screenshot showcasing whatever I just said before, hopefully clearing up the mess of words above.
Upvotes: 1