One file for multiple levels

Game Editor comments and discussion.

One file for multiple levels

Postby jimmynewguy » Sat Oct 15, 2011 3:16 pm

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 :(
Working on a probably too ambitious project! Wild-west-adventure-RPG-shooter-thing.
User avatar
jimmynewguy
 
Posts: 1137
Joined: Sat Mar 31, 2007 6:27 pm
Score: 89 Give a positive score

Re: One file for multiple levels

Postby Game A Gogo » Sat Oct 15, 2011 4:17 pm

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
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: One file for multiple levels

Postby jimmynewguy » Sat Oct 15, 2011 5:40 pm

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.
Working on a probably too ambitious project! Wild-west-adventure-RPG-shooter-thing.
User avatar
jimmynewguy
 
Posts: 1137
Joined: Sat Mar 31, 2007 6:27 pm
Score: 89 Give a positive score

Re: One file for multiple levels

Postby Game A Gogo » Sat Oct 15, 2011 5:46 pm

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
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: One file for multiple levels

Postby jimmynewguy » Sat Oct 15, 2011 5:55 pm

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)
Attachments
Squares 2.zip
(8.03 KiB) Downloaded 76 times
Working on a probably too ambitious project! Wild-west-adventure-RPG-shooter-thing.
User avatar
jimmynewguy
 
Posts: 1137
Joined: Sat Mar 31, 2007 6:27 pm
Score: 89 Give a positive score

Re: One file for multiple levels

Postby Game A Gogo » Sat Oct 15, 2011 6:29 pm

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
Attachments
Levels.zip
(128 Bytes) Downloaded 81 times
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: One file for multiple levels

Postby jimmynewguy » Sat Oct 15, 2011 7:17 pm

Weird, thanks gogo. Hopefully it'll all work out in the end!
Working on a probably too ambitious project! Wild-west-adventure-RPG-shooter-thing.
User avatar
jimmynewguy
 
Posts: 1137
Joined: Sat Mar 31, 2007 6:27 pm
Score: 89 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest