by EvanBlack » Mon Oct 24, 2011 5:33 am
Having trouble figuring out how to save and load from an array of structs... Here is my code.
I changed it a couple of times to try different things and nothing worked. Here is the last thing I tried.
BINSAVE.ged Script:
- Code: Select all
typedef struct NewStruct
{
int a;
int b;
char c[20];
} NewStruct;
NewStruct myStruct;
NewStruct myStruct2;
FILE *g_DATAMAPFILE;
NewStruct * pMyStruct[2];
int SaveDataMap()
{
g_DATAMAPFILE = fopen("Level.txt", "wb");
if(g_DATAMAPFILE == NULL) {return 1;}
fwrite(pMyStruct, sizeof(NewStruct)*2, 1, g_DATAMAPFILE);
fclose(g_DATAMAPFILE);
return 0;
}
void init()
{
pMyStruct[0] = &myStruct;
pMyStruct[1] = &myStruct2;
pMyStruct[0]->a = 20;
pMyStruct[0]->b = 55;
strcpy(pMyStruct[0]->c, "String");
pMyStruct[1]->a = 99999;
pMyStruct[1]->b = 77777;
strcpy(pMyStruct[1]->c, "LOLFUCKLMAO");
if(!SaveDataMap()){ExitGame();}
}
BINLOAD.ged Script:
- Code: Select all
typedef struct NewStruct
{
int a;
int b;
char c[20];
} NewStruct;
NewStruct myStruct;
NewStruct myStruct2;
FILE *g_DATAMAPFILE;
NewStruct * pMyStruct[2];
int LoadDataMap()
{
g_DATAMAPFILE = fopen("Level.txt", "rb");
if(g_DATAMAPFILE == NULL) {return 1;}
fread(pMyStruct, sizeof(*pMyStruct[0]) * 2, 2, g_DATAMAPFILE);
fclose(g_DATAMAPFILE);
return 0;
}
void init()
{
pMyStruct[0] = &myStruct;
pMyStruct[1] = &myStruct2;
if(LoadDataMap())
{
ExitGame();
}
}
Edit: I got it to work using a for loop, but how do I do it without a loop?
(\__/) ( Soon... The world)
(O.o )< will be mine!____)
(> < ). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bunny Overlord 2012!