Page 1 of 1

One file for multiple levels

PostPosted: Sat Oct 15, 2011 3:16 pm
by jimmynewguy
I have this as my global code to save all the levels onto one file for a game:
Code: Select all
int MAP[16][10][8];
// Saves ALL levels
void save_levels()
{
    FILE * map = fopen("Levels", "w");
    int i,j,k;
    for(k=0; k<16; k++)
    {
        for(j=0; j<10; j++)
        {
            for(i=0; i<8; i++)
            {
                fputc(MAP[k][j][i], map);
            }
        }
    }
    fclose(map);
}
// Loads ALL levels
void load_levels()
{
    FILE * map = fopen("Levels", "r");
    int i,j,k;
    for(k=0; k<16; k++)
    {
        for(j=0; j<10; j++)
        {
            for(i=0; i<8; i++)
            {
                MAP[k][j][i] = fgetc(map);
            }
        }
    }
    fclose(map);
}
// Display NUM level
void display_map(int num)
{
    int i,j;
    for(j=0; j<10; j++)
    {
        for(i=0; i<8; i++)
        {
            if(MAP[num][j][i]>0 && MAP[num][j][i] < 10)
            {
                char * anim = getAnimName(MAP[num][j][i]-1);
                CreateActor("tile", anim, "(none)", "(none)", 30*j, 30*i, true);
            }
            else if(MAP[num][j][i] >= 10)
            {
                char * anim = getAnimName(MAP[num][j][i]-9);
                CreateActor("goal", anim, "(none)", "(none)", 30*j, 30*i, true);
            }
        }
    }
}
load_levels is called on start up of the game to avoid deleting any other saves.

My question is this: It seems to work now, but can this cause problems in the future? I just have this feeling that I messed up the thinking somehow. Or does someone have a better way to do this? I don't want to have 16 level files sitting in the same folder as the .exe. Too bad GE can't get into folders.

Thanks :)

EDIT: I lied, I filled the whole screen and tried to save then load and display only to find several squares missing :(

Re: One file for multiple levels

PostPosted: Sat Oct 15, 2011 4:17 pm
by Game A Gogo
your fopen's are slightly wrong

fopen("Levels", "w"); should be fopen("Levels", "w+b");

and fopen("Levels", "r"); should be fopen("Levels", "r+b");

Since you're working into binary with bits and bytes and not using it for text purposes. I don't know if this will fix your missing squares problem... if it still happens it has something to do with your displaying your level, haven't looked at that part much

Re: One file for multiple levels

PostPosted: Sat Oct 15, 2011 5:40 pm
by jimmynewguy
I changed it to +b and it doesn't work still. But thanks for letting me know what that meant.

The display code shouldn't really be the problem though, it's one I've used before that worked always. So I'm not too sure what the problem is... I'll keep messing with it I guess.

Re: One file for multiple levels

PostPosted: Sat Oct 15, 2011 5:46 pm
by Game A Gogo
could you send me the GED and data? I'll try to figure it out, since I've never had problems like that. I think it has something to do with converting int to char back to int

Re: One file for multiple levels

PostPosted: Sat Oct 15, 2011 5:55 pm
by jimmynewguy
Here: The problem only seems to occur randomly when there is another level saved as well.... Really bothering me when I think it's working and then it doesn't.

s will save and l will load delete gets rid of the actors

Try loading level 1 then saving it as level 0 then reload level 0 and some blocks will be blue and others gray (if you get what I mean)

Re: One file for multiple levels

PostPosted: Sat Oct 15, 2011 6:29 pm
by Game A Gogo
hmm... what I found out is the level file has blank spot in it.
Try this one, I tried saving and loading, everything looks fine

Re: One file for multiple levels

PostPosted: Sat Oct 15, 2011 7:17 pm
by jimmynewguy
Weird, thanks gogo. Hopefully it'll all work out in the end!