Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Question on saving game files

Author
Message
Yodaman Jer
User Banned
Posted: 19th Sep 2009 17:16
Before I ask my question, I need to say that I will be unable to test anything out until tomorrow or even the day after. I am asking simply because I'm far too curious and can't go that long without finding out!

What does a Save Game file need to contain? Do you just need to write some variables in your code that tells the computer...



...and that would tell the computer to put the player where they last saved in the game?
In other words, do you just need to save the 'lev#' variables to the file and then have the program read them back, so that the player can resume from where they left off? I imagine this process is also used for saving lives and health information, as well as coins and scores.

So, yeah. Is that the basic idea of the whole thing?

-Yodaman Jer.

Great quote: "If you want lots of people to play your game make it simple; lots of people are simple." -Obese87
BN2 Productions
20
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 19th Sep 2009 20:03
From what I have decided on (never did it, just thought about it), its just store some variable info and load it back, based on what your game requires. If you are using the STATE variable structure (one loop and if state is 1 go to the menu, 2 is level 1 etc) you could easily just store that for quick loading and placing you into the code as necessary without an if branch for loading. Other than that, its whatever you need: ammo, health, character level, position, gameplay time, etc.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 19th Sep 2009 20:47
Like BN2 is saying. You could save your game variables with whatever value they have and then load them back in to the game and assign those loaded values to the in game variables. If you wanted to get super detailed you could save every single value of every single variable in the game at the time of the save.

When you load the load file back in, assign each value back to your variables.

Could be anything and everything:


Enjoy your day.
Yodaman Jer
User Banned
Posted: 19th Sep 2009 21:31
Wow!

I honestly didn't expect a pseudo-code example, Latch. That's fantastic!, thanks! Now I have something to look to as a guide.

It looks a lot easier than I would have thought. I remember several months ago I asked a similar question involving saving a high-score table and couldn't grasp the concept, yet with this it looks almost as easy as pie.

Unfortunately I don't have time to test anything out, and likely won't until Monday evening or so...so I'll let you all know how it works out then. I'll just put together a small 'level' with two platforms and a sphere you can control (that's how I always test things out). If I can get this down, I can finally make the game I've been wanting to since early 2007. w00t!

Thanks again! Cheers, mate! (I'm not actually British, lol.)

-Yodaman Jer.

Great quote: "If you want lots of people to play your game make it simple; lots of people are simple." -Obese87
Dark Dragon
16
Years of Service
User Offline
Joined: 22nd Jun 2007
Location: In the ring, Kickin\' *donkeybutt*.
Posted: 19th Sep 2009 22:33
Lol,i'd never even thought about htis in a while. Cool, latch.

(\__/) HHAHAHAHAHAH!
(O.o ) / WORLD DOMINATION!!!!!!!!!!
(> < )
BN2 Productions
20
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 19th Sep 2009 22:36
Also, if you keep your values in arrays then you can just use SAVE ARRAY and then use a fileblock to store them all into a single file, but that might be more trouble than it is worth.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
t10dimensional
15
Years of Service
User Offline
Joined: 22nd Mar 2009
Location: Code Cave, USA
Posted: 20th Sep 2009 22:47 Edited at: 20th Sep 2009 22:48
I have a question too on the same subject.

Can you save diffrent files on one notepad?(As in game files)I've thought of makeing 3 diffrent files on 3 diffrent notepads.But that seems unneeded.

A good game is one percent inspiration, ninety-nine percent perspiration.
Dreamsenshi
14
Years of Service
User Offline
Joined: 4th Sep 2009
Location: USA
Posted: 21st Sep 2009 05:23
Interesting to think about; all ends up depending on how complicated (or uncomplicated) you want your game to be. I've been wondering about some of these same things too, so this is helpful.

Your error is my treasure. =^--^=
Yodaman Jer
User Banned
Posted: 21st Sep 2009 20:06
I don't think I'd need my game files to be quite as complicated as Latch demsontrated, i.e., I don't need to store the player's exact position when they save (unless I choose to go that route), I was thinking about just saving all of the variable information (health, lives, points and amount of coins) and then reading it back into the game. That way the .txt file would look like this:



The 'GameLev' variable is what tells the program what level to resume the game in. After the player completes each level, GameLev increases and is then written to the file when the player saves.

I'll get to trying this out here soon.

-Yodaman Jer.

Great quote: "If you want lots of people to play your game make it simple; lots of people are simple." -Obese87
BN2 Productions
20
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 21st Sep 2009 22:00
It might be best in the text file to structure all of the values with the same number of characters. So, instead of 25, you could have 0025. What this would allow you to do is use READ STRING and then have the value found with VAL(RIGHT$(stringname$,4)).

This was a method I used for loading control schemes at one point and it worked out pretty well.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Yodaman Jer
User Banned
Posted: 21st Sep 2009 22:20
Can you give me an example, BN2? I tried out Latch's example...it worked with saving, but I couldn't get it to load properly (I'll test it out after a slight modification, but it will be later). It kept saying that the file didn't exist...even though it did.

Though the cause is most likely linked to the fact that I have it load the file directly after saving.

-Yodaman Jer.

Great quote: "If you want lots of people to play your game make it simple; lots of people are simple." -Obese87
BN2 Productions
20
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 21st Sep 2009 22:44
Sure.

Here is your text file:

Its easy to read with the labels (Health= Coins= etc etc) however, if you get any characters that aren't numbers at the beginning of a string, then VAL will return 0.

There are 2 options:
1. Format all of the numbers to the same number of digits like so

This just matches it to the item with the most digits required
The code would look something like this:



2.Read the line from the file and scan it to find the "=" and take the number to the right
The code would look something like this:


Note that the code above isn't tested (I think I got some syntax wrong here and there). It is just to give you a feel for what is going on.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Yodaman Jer
User Banned
Posted: 21st Sep 2009 23:22
Thanks, BN2! Both of those examples look great.

I had forgotten about the VAL command, I think I'll look into that more and try and experiment with assigning read-in variables back into the main code...I'll let you all know how it works out!

Btw, is it a good idea to have the saving and loading routines in separate parts of the program? For example, the "SaveGame" routine would be its own block of code, as well as "LoadGame"? Common sense is telling me that yes, it's a good idea to separate the two.

-Yodaman Jer.

Great quote: "If you want lots of people to play your game make it simple; lots of people are simple." -Obese87
Yodaman Jer
User Banned
Posted: 21st Sep 2009 23:43 Edited at: 21st Sep 2009 23:47
Well, I didn't exactly use your example BN2, but I did use the VAL command the way that TDK demonstrates in his tutorials on file access (I should have read those first before posting, I feel silly now). My result is this:



It writes the information to the file perfectly and I can read it in Notepad, but when I try to load it back into the program and print the info to the screen it only displays '0'. I imagine that's because I didn't format the code properly and didn't use your technique to read anything to the right of a line. I'll work on it.

EDIT:

I just erased the words before the numbers in the file, like so:



That fixed it perfectly! Now to figure out how to encrypt it so that the player can't mess anything up...because if they're like me, they'll want to open the files and see what it does, and change the variables around without knowing what they do, and that could 'break' their game file.

-Yodaman Jer.

Great quote: "If you want lots of people to play your game make it simple; lots of people are simple." -Obese87
BN2 Productions
20
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 22nd Sep 2009 00:21
Probably what was returning the 0 was you were getting character that weren't numbers in the string. So, for instance, "=25" would evaluate to 0. However "25" would be 25. This is why I suggested the leading 0's.

To encrypt it, you could place it in a fileblock, which makes the file hard to get at (also if you have multiple files for saving you could store them all together or just throw all your saves into one file).

Alternatively you could also put in a number that is the sum of all of the other numbers. If the file has been tampered with, you will know because things won't add up (perhaps multiply the total by a constant so that it isn't as easy to discern). So for instance:

Being the text file. The 2544 is the total of the actual variable values. If someone changes the 25 to 100, it won't add up and your program could realize that it is broken.


Actually, it might be interesting to just write all of the bytes into an IMAGE file, rather than a text file. The program would be able to decipher it with memblocks, but the user wouldn't be able to do anything really to it.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Yodaman Jer
User Banned
Posted: 22nd Sep 2009 00:57
Heh, I think I'll just stick with the text file...I have no idea how to use memblocks...

I have just two more questions:

1)-How do I assign the loaded in variables to the in-game variables? This is what my loading procedure looks like:



2)-How do I set it up so that there's more than one save file (or slot)? Would I do something like...



...and then just have each file name's own writing/reading procedure?

Thanks for all of the help so far guys!

-Yodaman Jer.

Great quote: "If you want lots of people to play your game make it simple; lots of people are simple." -Obese87
BN2 Productions
20
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 22nd Sep 2009 02:38
Well I don't think you would need separate procedures for loading different save files, since you will only have one active game at a time. For the variables, you could either do a for-next loop (though it would require that all of the data be in an array so that you can reference them with the loop counter) or just a read the first value, set that variable to what is read, repeat repeat. Not clean, but the only other way I can think of.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Yodaman Jer
User Banned
Posted: 22nd Sep 2009 04:26
Quote: "I don't think you would need separate procedures for loading different save files, since you will only have one active game at a time."


This is true. I suppose there only needs to be one active game file at a time...it would certainly cut down on arguments between siblings!

"Hey, get done, I wanna play my game file!"
"No, dude, I gotta get past this boss!"
"But you've been playing all day!"
"Well, tough bananas! Let me finish this level..."
"No! Mom, tell Stewart he has to let me play!"
"Shut up, Timmy!"



Yeah, having only one file will cut down on that, fo' sho.

I've tested everything, and the save/load engine I coded works great! I even got it to load the file with lines that have not only the variable number, but a description of what the number is as well. And no, the description doesn't display in-game.

Here's my code...it's a bit messy, but it was a few quick tests jumbled together without any forethought put into it and I plan on re-writing some of the larger code.



Like I said, it's a *bit* messy.

Thanks to everyone who helped me to understand saving and loading files! A round for everybody!

...Don't worry, it's non-alcoholic.

-Yodaman Jer.

Great quote: "If you want lots of people to play your game make it simple; lots of people are simple." -Obese87
Yodaman Jer
User Banned
Posted: 22nd Sep 2009 22:53
Hmm, I've got an interesting problem.

Actually, the code does exactly what I'm telling it to...which is the problem.

Code:



What's happening is that this line of code...



...seems to work great, until you try and re-load the level.

The value gets saved correctly, but because each value (2,3,4) has their own cube to turn solid, the last number it saves on will then only light up that one cube, no matter what value it's on. I tried fixing it by doing this:


And



But it still still didn't work. Then all of the cubes were lit up and nothing was being saved properly.

What I'm trying to do is set up a World screen, similar to what Mario Bros. 3 introduced. This is the only problem I'm having, other than that everything is working great.

The Media is attached, and the source code is included. If anybody can help me solve this problem, that would be great!

(Note: There is no reset option as of yet. Every time the game starts it loads the saved game. In order to reset you have to go into the 'Save Data' folder and delete the 'SavedGame.CGF' file.)

-Yodaman Jer.

Great quote: "If you want lots of people to play your game make it simple; lots of people are simple." -Obese87

Attachments

Login to view attachments
Yodaman Jer
User Banned
Posted: 23rd Sep 2009 05:16 Edited at: 23rd Sep 2009 06:09
[EDIT: The idea I describe below works great! If you're curious, read on.]

Well, I have an idea on how to fix it. It seems the solutions come when you least expect it, but I can't test it right now (don't have access to my computer that has DBC installed). I'd just like some feedback on whether I'm right or not...

Since each World of my game will have only four levels, that's only three more variables I'll have to add to my game (since level one in each world is unlocked by default). By assigning each world access port (currently the blocks) its own flag variable, it should read and write it to the gamesave file properly. Using the WorldLev variable to change each flag to 1, it should look something like this...

Pseudo-code:



Writing that to the external save file would look something like...



Of course the file will be formatted differently than what I show above, but you get the idea. Just a few more variables can make life so much simpler...

-Yodaman Jer.

Great quote: "If you want lots of people to play your game make it simple; lots of people are simple." -Obese87
BN2 Productions
20
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 23rd Sep 2009 20:42


A note here, as far as I know, you cannot format it that way. It needs to be:



If not then I guess I made a mistake while testing it.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 23rd Sep 2009 21:10 Edited at: 23rd Sep 2009 21:11
Why are you saving these files with strings? why not use write long or write byte(if the number is under 255) that way you can just read it back in without all this string stuff? it's sooooo much easier. and the file would be smaller. not by a lot but it would be.

Because i have been watching this thread and no one has done anything with the labels so in a sense they are just taking up bytes of memory for nothing.

New Site! Check it out \/
BN2 Productions
20
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 23rd Sep 2009 22:20
True, the labels don't do anything, but they do improve readability of the file itself (which is a plus from a development side as you can see immediately what is saved for each category without referencing the order).

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Yodaman Jer
User Banned
Posted: 23rd Sep 2009 22:43 Edited at: 23rd Sep 2009 22:46
Quote: "Why are you saving these files with strings?"


That's just the way I learned in TDK's tutorials. I plan on changing them in the future (like today).

Quote: "...the labels don't do anything, but they do improve readability of the file itself (which is a plus from a development side as you can see immediately what is saved for each category without referencing the order)."


That's exactly why I'm doing that. Just to make sure that everything is saving properly. In the final version I plan to just have the numbers written instead of the numbers plus the comments.

I've gotten some decent progress done, but for some reason I've encountered a speed issue (it was fine, then started running slow whenever I ran the program). So, after I'm done with a couple more tests, I plan to completely re-write the "engine" of the program. I'm going to start by programming the menu first (and work on the load functions from that), then build the Worlds. Each World will look somewhat like this:



I'm glad to say that everything works so far, I'm just having trouble with speed. I'll figure it out, it can't be too bad.

EDIT:

Silly me. The program was running slow because my virus software was updating itself. However, I'm still going to rewrite the entire thing. Thanks to everyone who has given me tips and help on this!

-Yodaman Jer.

Great quote: "If you want lots of people to play your game make it simple; lots of people are simple." -Obese87
t10dimensional
15
Years of Service
User Offline
Joined: 22nd Mar 2009
Location: Code Cave, USA
Posted: 30th Sep 2009 18:07
Quote: "That fixed it perfectly! Now to figure out how to encrypt it so that the player can't mess anything up...because if they're like me, they'll want to open the files and see what it does, and change the variables around without knowing what they do, and that could 'break' their game file. "


How do you encrypt?

A good game is one percent inspiration, ninety-nine percent perspiration.
Yodaman Jer
User Banned
Posted: 30th Sep 2009 18:31
Quote: "How do you encrypt?"


I'm pretty sure all you have to do is use 'Write Long' instead of 'Write String'. That then encrypts the information.

However, I think I'm just going to continue using 'Write String' for now, because it's helping me learn how to design my own file formats.

I'm abandoning my game project right now, as I've started learning how to make my own level editor. I've also been testing out the DBPro trial extensively and rather like it, so I may resume my project in there. You can read my blog on why I'm going with DBPro, but I'll tell you right here that it's just not powerful enough for what I want and need. I'll definitely still use DBC for small things though.

-Yodaman Jer.

Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 1st Oct 2009 05:19
Quote: "I'm pretty sure all you have to do is use 'Write Long' instead of 'Write String'."


No, not really. that just makes it so you can't read it. this is do to the fact that all notepad does is take every byte of the file and turn it into a character. when you take a number it's bytes don't make a sensable sentence so it comes out like that. try this:



That will(when opened in notepad) will say "Hi!"

New Site! Check it out \/
t10dimensional
15
Years of Service
User Offline
Joined: 22nd Mar 2009
Location: Code Cave, USA
Posted: 1st Oct 2009 05:33 Edited at: 1st Oct 2009 05:34
Thanks anyways though.I just need somthing my little brother can't figure out.

But for future puposes how would you do it?Could you, when you make a final executeable, make it where it hides the notepad like it does the other stuff.Or is there a better way to stop cheating?

A good game is one percent inspiration, ninety-nine percent perspiration.
Yodaman Jer
User Banned
Posted: 1st Oct 2009 06:16
What you can do is give your file an extension based off of your game. For example, my game that I plan to make is called Coins Galore (or will be, rather), so I save files with the extension .CGF, which stands for "Coins Galore File". This way, Notepad can't open it by default and the only way to access it is if you right click the file and choose "open with" from the menu--then you have to browse through your computer's system files until you find Notepad. It won't encrypt, but it makes it harder to get into.

-Yodaman Jer.

t10dimensional
15
Years of Service
User Offline
Joined: 22nd Mar 2009
Location: Code Cave, USA
Posted: 1st Oct 2009 06:33
Ok, sounds good thanks!

Razerx

Login to post a reply

Server time is: 2024-05-09 11:41:37
Your offset time is: 2024-05-09 11:41:37