Page 1 of 1

If Exists

PostPosted: Fri Aug 26, 2011 4:44 pm
by foleyjo
Is there a command to see if a file exists?
When my game first loads to the main menu I want to say:

If savefile exists then load it
if not assign these values to variables

Re: If Exists

PostPosted: Fri Aug 26, 2011 4:56 pm
by Game A Gogo
you can use this:

Code: Select all
if(fopen("savefile.sdg","r+b"))
{
    //File exists
}
else
{
    //File doesn't exists
}


fopen return NULL when there's no file.

Re: If Exists

PostPosted: Fri Aug 26, 2011 5:02 pm
by Jagmaster
So, is sdg the extension for a saved variable file?

Re: If Exists

PostPosted: Fri Aug 26, 2011 5:09 pm
by foleyjo
Cheers gogo.

1 question about that - What exactly does the r+b bit mean?

Also I just realised the array I wish to save I have set up in the global code and not used the variable option in game-editor. I'll explain why with the section of code
Code: Select all
const int maxlevels = 3;
int BLevelComplete[maxlevels];  //This array needs to be saved
int LevelX[maxlevels] = {960,-320,320};
int LevelY[maxlevels] = {-720,-240,-240};


How would I go about saving this? Would I need to recreate it using the Variable option.

(on a different note I keep pressing the + to give you a point but all it is doing is refreshing the page)

Re: If Exists

PostPosted: Fri Aug 26, 2011 9:58 pm
by Game A Gogo
Jagmaster wrote:So, is sdg the extension for a saved variable file?

so, it's something I made on the spot, files usually have file extension... sdg is for SaveDGame

Re: If Exists

PostPosted: Fri Aug 26, 2011 10:04 pm
by Game A Gogo
foleyjo wrote:Cheers gogo.

1 question about that - What exactly does the r+b bit mean?

Also I just realised the array I wish to save I have set up in the global code and not used the variable option in game-editor. I'll explain why with the section of code
Code: Select all
const int maxlevels = 3;
int BLevelComplete[maxlevels];  //This array needs to be saved
int LevelX[maxlevels] = {960,-320,320};
int LevelY[maxlevels] = {-720,-240,-240};


How would I go about saving this? Would I need to recreate it using the Variable option.

(on a different note I keep pressing the + to give you a point but all it is doing is refreshing the page)


r+b means Read (binary mode), w+b means Write (binary mode), you can omit the +b so it's in "normal" mode... I don't really know the difference but I always use binary mode.

Hmmm... if you're not used with scripting, it might be hard for you to understand... but if you need to use those kinds of variable saving, you're better off creating the variable in the variable box and use the GE functions, just make sure your array is big enough as I don't think you can use constants for the size.

if you wanted to do it the hard way you'd need to understand how the << and >> bit wise operations work and about variable size in bits and byte.

Re: If Exists

PostPosted: Sat Aug 27, 2011 8:31 am
by foleyjo
Thanks again .

I will take the easy way for now. I do hope in the future I will be able to try and understand the more difficult method.

The only reason I chose the declaration method I'm using is so I can use the maxlevels constant to update the 3 arrays I'm using as I add levels to the game.
It's not really vital to making things work.