If Exists

Non-platform specific questions.

If Exists

Postby foleyjo » Fri Aug 26, 2011 4:44 pm

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
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: If Exists

Postby Game A Gogo » Fri Aug 26, 2011 4:56 pm

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.
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: If Exists

Postby Jagmaster » Fri Aug 26, 2011 5:02 pm

So, is sdg the extension for a saved variable file?
User avatar
Jagmaster
 
Posts: 875
Joined: Sun May 08, 2011 4:14 pm
Location: Not where you think.
Score: 82 Give a positive score

Re: If Exists

Postby foleyjo » Fri Aug 26, 2011 5:09 pm

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)
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: If Exists

Postby Game A Gogo » Fri Aug 26, 2011 9:58 pm

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
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: If Exists

Postby Game A Gogo » Fri Aug 26, 2011 10:04 pm

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.
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: If Exists

Postby foleyjo » Sat Aug 27, 2011 8:31 am

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.
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest