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.

Dark GDK / saving and loading

Author
Message
robster
15
Years of Service
User Offline
Joined: 15th Dec 2008
Location:
Posted: 30th Dec 2010 23:32
I'm new to this and I want to be able to save and load. Some example code would be good thanks.

Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 31st Dec 2010 11:43
the whole idea is to open a file and write the information you need so that when you read them, you can figure out where you was before the save, so simply, to save, open a file and save some values there, these are up to you, but usually you would save global variables, classes info, etc, and on loading, just read the saved file and assign each value you read to its global variable or create class instances or whatever you want to do

Your signature has been erased by a mod - Please reduce it to 600x120 maximum size
robster
15
Years of Service
User Offline
Joined: 15th Dec 2008
Location:
Posted: 31st Dec 2010 13:44
ok, but my problems comes when i don't know how to code it to save/load. thanks

Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 31st Dec 2010 14:12
very simple: (this is C-style file read/write, i don't like the std way, but if you do so, it is very easy to switch if you want)


fprintf reference: http://www.cplusplus.com/reference/clibrary/cstdio/fprintf/

Your signature has been erased by a mod - Please reduce it to 600x120 maximum size
robster
15
Years of Service
User Offline
Joined: 15th Dec 2008
Location:
Posted: 31st Dec 2010 16:48
thanks for that il give it a go

robster
15
Years of Service
User Offline
Joined: 15th Dec 2008
Location:
Posted: 31st Dec 2010 18:03
ok im not really sure how to but i looked at your link and tryed not to come back and ask you again, but im not sure how to read the txt file and do something like if MissionID = 1 then load mission1.

I tried fscanf and fread but im kind of shooting in the dark here. thanks

Matty H
15
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 31st Dec 2010 20:36 Edited at: 31st Dec 2010 20:36
You can create a string and read each line of the file into the string with the getline function:



There is lots to learn, you will need to know how to manipulate strings.

robster
15
Years of Service
User Offline
Joined: 15th Dec 2008
Location:
Posted: 1st Jan 2011 14:48
no luck I think I should try to learn a bit more about strings because I'm not sure about them.

TechLord
21
Years of Service
User Offline
Joined: 19th Dec 2002
Location: TheGameDevStore.com
Posted: 1st Jan 2011 15:26
Heres an example of how to use File and String Libraries that come with Visual C++. I don't know what your experience is with using C++ but if you're very new to the language, I highly recommend you read this.

Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 2nd Jan 2011 16:56 Edited at: 2nd Jan 2011 17:03
well for parsing, you simply do this: (might be a bit slow, but this is easiest)



what happens there is:
you loop forever untill EOF character is read, store everything you read in the variable "variable", once a '=' is reached, terminate "variable" (by adding a '\0' at its ending) and go to stage 1 which is reading to "value" variable, now, once ';' is reached, terminate "value" and then use the info you have, repeat this untill EOF is reached

notice that we won't read the spaces (" "), why? because it will cause problems on your side, for example, in the file you have "ThisVar = 10", it will be read like "ThisVar " and " 10", no problem with the 10 but your string comparison won't succeed assuming you will compare "ThisVar" to "ThisVar "

also, for comparing, use:


simplest method i believe, can be optimized in many ways to suit what you want, hope this helps

Your signature has been erased by a mod - Please reduce it to 600x120 maximum size
robster
15
Years of Service
User Offline
Joined: 15th Dec 2008
Location:
Posted: 2nd Jan 2011 22:00
@hassan ok so now if I want the "10" for example I do


then use the i however I want

I tried this but can't seem to get it to work sorry if I'm being stupid

Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 3rd Jan 2011 14:48
how come it doesn't work? maybe the value variable holds wrong data?

Your signature has been erased by a mod - Please reduce it to 600x120 maximum size
robster
15
Years of Service
User Offline
Joined: 15th Dec 2008
Location:
Posted: 3rd Jan 2011 15:59 Edited at: 3rd Jan 2011 16:01
I don't think so I have been doing allot of trail ans error with it.

my save file has only the one line: level = (3);

and for the moment I only want it to read of the 3 then put the 3 into levelID so I can use it (thats when I tried using atoi)

heres the code (not much changed from yours)



should I have changed it more ^ ?

btw I do call LoadGame (); and SaveGame (); later on in the code however I can't tell if LoadGame Works the only indicator I can think of using is dbPrint (levelID); and if it shows 3 I would be happy but it only shows 0

thanks
rob

Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 4th Jan 2011 18:40
When you open a file to read/write data other than text you should use the binary function:
FILE* f=fopen("file.ext","rb"); (the "rb" is "read binary")
You will have to do the same with the write. This takes care of any character parsing that the file system does when reading such as an EOF combination.

The fastest code is the code never written.
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 5th Jan 2011 15:34 Edited at: 5th Jan 2011 15:37
oh sorry my fault, forgot a continue statement:


i believe value variable had "=3", because it added the = to it

also, you got one little problem there, this function is supposed to read all file info, you did:
levelID = atoi (value);
instantly without any checking,
it should look more like

or you could write a function to this for you as it will look messy with too many variables

also, im not sure whether fgetc reads the \n character (end of line), but just in case, you can add this:


Your signature has been erased by a mod - Please reduce it to 600x120 maximum size

Login to post a reply

Server time is: 2024-06-28 01:35:45
Your offset time is: 2024-06-28 01:35:45