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 something

Author
Message
Oster200
13
Years of Service
User Offline
Joined: 21st Jan 2011
Location:
Posted: 13th Feb 2011 01:37
ive searched every where to get this answer but i just cant find it. any ways the question is im making a 2d scroll shooter like Dark Invaders (tutroial that Dark GDK gives you) but i want to make up grades so the money is the points you get that is all figured out but i cant seem to figure out how to do this...( example ) making speed upgrade when player clicks on the speed upgrade then i want it to be there through ALL the levels not just the level that they get it on but to make this shorter

the real question is how to make the game remember the upgrades through all the levels

thank you
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 13th Feb 2011 14:41 Edited at: 13th Feb 2011 14:42
save the upgrade to a file, the best way is the following:

write a struct that can hold all the data that you want to save, for example:


and then to save do this:
1- create an instance of the data:

2- fill the data

3- write it to a file:


that's it, info saved, and for loading, same thing but reversed:
1- open the file with ios::in

2- read data

3- fill in your game variables using the read data:


a few things to notice:
1- you need to #include <iostream> and #include <fstream>
2- you need to add "using std::fstream;" and "using std::ios;", or simply just "using namedspace std;"
3-MYFILEPATH.MYFORMAT, can be something like "data\\info.ASD", or whatever, first part (before ".") is the path, and after "." is the format (or extension)

Oster200
13
Years of Service
User Offline
Joined: 21st Jan 2011
Location:
Posted: 14th Feb 2011 00:07
THANK YOU i will have to try this out i will tell you if something is wrong but at this point thankyou
Oster200
13
Years of Service
User Offline
Joined: 21st Jan 2011
Location:
Posted: 14th Feb 2011 00:49
oh yes a question i never writen somthing to a file and so i dont quite under stand how it works so if you could please tell me or show me a link on how they work it would help me under stand what im doing thank you in advanced
Oster200
13
Years of Service
User Offline
Joined: 21st Jan 2011
Location:
Posted: 14th Feb 2011 01:04
okay i dont know what is going on you should please tell me where to put these because where i put them i got 33 errors and like i said im just like improving on Dark Invaders and they have every thing listed such as game start, game wait and stuff like that so tell me what i should change and what i have to do sorry im being in the way so much but i really need some help.
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: &lt;script&gt; alert(1); &lt;/script&gt;
Posted: 14th Feb 2011 08:23 Edited at: 14th Feb 2011 23:53
alright, what is going on:


same thing goes to loading, but as for the notes, just add this at the top of your program...


where to place them? well simply and obviously, you save the data when you want to save, and load it when you want to load, for example, loading can be at the beginning of the program and saving can be after each level, or when the user requests that

oh and you might get some linking errors, you should do this:
in VC++: go to project -> <project name> properties -> configuration properties -> C/C++ -> code generation -> runtime library, make it /MT

Oster200
13
Years of Service
User Offline
Joined: 21st Jan 2011
Location:
Posted: 14th Feb 2011 23:26
Srry a few more things. well i wont let me use "#using namespace std;" it said "error C2006: '#using' : expected a filename, found 'namespace'"

and it said

fatal error C1190: managed targeted code requires a '/clr' option

since you updated it with ur last post am i still supposed to use somthing from it all im using now is the saving thing and the #include <iostream>
#include <fstream>
#using namespace std;

I feel so stupid!! ahh.

Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: &lt;script&gt; alert(1); &lt;/script&gt;
Posted: 14th Feb 2011 23:55 Edited at: 14th Feb 2011 23:56
ah my bad, a typo in my previous post (fixed), there is not # before "using"

Oster200
13
Years of Service
User Offline
Joined: 21st Jan 2011
Location:
Posted: 15th Feb 2011 04:52
okay i got it to save but it is writing in like a werid languge like if i open up the note book it looks like this  i dont know if it is how i script it

int SAVEDATA = 1;
iPlayerSpeed = 15;

fstream file; //create an fstream object, which is used to open files
file.open ( "Save.txt", ios::out | ios::binary ); //use "file" object to open "FILEPATH.MYFORMAT", ios::out means that we will output info (because we are saving), and ios::binary states that we want it to be a binary file
if ( !file.is_open ( ) ) //check if the file is opened or not
{
//the file is not open, the method probably failed to open the file, handle the error..
}
file.write ( (char*)&iPlayerSpeed, sizeof SAVEDATA ); //write "save" object to the file
file.close ( ); //close the file that we opened
WLGfx
17
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 15th Feb 2011 09:20
When you're writing to the file you are saving the actual data structure (not a printed text file). Although a text file is easier to read in note pad it is much harder to code anything to read it back into your program.

If you have a struct with a load of values you will find it easier (and quicker) to read and write to files using the above method than to print it out as a text file.

Warning! May contain Nuts!
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: &lt;script&gt; alert(1); &lt;/script&gt;
Posted: 15th Feb 2011 09:27 Edited at: 15th Feb 2011 09:29
it is also (for large files) much MUCH smaller in size, for example, if you have a float, it could be something like 12345.2123, so, if you write that in a file, it will write <strlen("12345.2123")> bytes in the file (10 bytes), but if you use this method, it will write 4 bytes only (sizeof float), not to mention that this is encrypted, if it's formatted text, anyone can open that file and edit it, would be very easy to hack

what are you doing in the above code? this is not the way i explained..you are saving iPlayerSpeed, why? save everything at once, save the whole structure

Oster200
13
Years of Service
User Offline
Joined: 21st Jan 2011
Location:
Posted: 15th Feb 2011 22:28
well because i dint understand

SAVEDATA save;

like the int what would ui put as you can see the one above for SAVEDATA i put 1 so i dint know what to put for save
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: &lt;script&gt; alert(1); &lt;/script&gt;
Posted: 15th Feb 2011 22:44
SAVEDATA is a structure, so instead of
int variable;
you can do
SAVEDATA variable

first one is an int, second one is a SAVEDATA, to assign a value to an int you just do x = 1, but for a structure, you can use the "." to access the members, for example:


very simple, you just want to define a structure (or lets say a type) that can hold everything you want to be memorized by the program after it shuts down, or write each variable independently, which means more messy code

Oster200
13
Years of Service
User Offline
Joined: 21st Jan 2011
Location:
Posted: 15th Feb 2011 23:01
okay im understanding this better so where ABC is i would put SAVEDATA than the int iV1 would be Speed? and im taking it as float fV2 is just another example then myVariable.iVl would look like myVariable.Speed = ? ( i dont know what to put in there ) so... is myVariable to load it?
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: &lt;script&gt; alert(1); &lt;/script&gt;
Posted: 16th Feb 2011 10:30 Edited at: 16th Feb 2011 10:31
you can add WHATEVER you want in the struct, and call it WHATEVER you want, the whole point is, you want this struct to be able to contain all the data you to be saved...you want to save speed? add

you want to save money?

thats the whole thing, now you data struct is made up of 2 parts, money and speed, so just do this:

and to load:


that's it!

Oster200
13
Years of Service
User Offline
Joined: 21st Jan 2011
Location:
Posted: 16th Feb 2011 23:43
YEA it works thank you

Login to post a reply

Server time is: 2024-11-19 14:53:54
Your offset time is: 2024-11-19 14:53:54