Page 1 of 1

Loading the next level using strcpy - help!

PostPosted: Thu Mar 29, 2012 11:14 pm
by Johnno
I'm trying to get the following code to work but it keeps sending me back to the start menu, not level 2 (level02.dat).

I've set up the following Variable on both levels
SaveLevel
Integar
Global
Array: no
Save group: data

Level01 I set up the following:

Global script
Code: Select all
int level;


Collision - script
Code: Select all
level = 2;
SaveLevel = level;
saveVars ('game.sav", data");


Score screen I set up

Global script
Code: Select all
int level;
int loadlevel;


Keydown - script (to start the level)
Code: Select all
loadVars ("game.sav", "data");
level = SaveLevel;

if (level = 2);
strcpy("loadlevel", "level02.dat");

if (level = 3);
strcpy("loadlevel","level03.dat");

if (level = 4);
strcpy("loadlevel","level04.dat");

LoadGame("loadlevel");


I set up a Text Actor to see if "level" did in fact = 2 and it does, so the var code is good.

Re: Loading the next level using strcpy - help!

PostPosted: Fri Mar 30, 2012 4:29 am
by Jagmaster
In the function strcpy, don't use quotes around the name of the string, only around the value you're copying. strcpy(text, "TEXT STRING etc...");

Likewise, if you're copying two strings together, neither of the values should have quotes. strcpy(string1, string2);

And like likewise, you shouldn't have the name of the string in quotes around the name of the string in any function. This is because it'll read the name of the string as a value and not the string itself (unless you would want the function to read the name of the string variable, which those circumstances are merely non-existent).

LoadGame("loadlevel");// this will load the game loadlevel.

LoadGame(loadlevel);// this will load the game with the title that matches the string's value.


I may have over explained that. But hey, I got some finger exercise! :D

EDIT: might wanna check that your levels you're loading are in the same directory as the game itself. Otherwise it won't work either.

Re: Loading the next level using strcpy - help!

PostPosted: Fri Mar 30, 2012 5:54 am
by Johnno
OK I did that but a get a "Suspicios pointer conversion" error.
All the .dat files and the .exe are in the same folder.

Code: Select all
loadVars ("game.sav", "data"); //load the game save file "game.sav"
level = SaveLevel;

if (level=2); //game level 2
strcpy(loadlevel, "level02.dat"); //load level02.dat


if (level=3); //game level 3
strcpy(loadlevel,"level03.dat"); //load level03.dat


if (level=4); //game level 4
strcpy(loadlevel,"level04.dat"); //load level04.dat

SaveXP = xp; //the xp score
saveVars ("xp.sav", "data"); //save the xp score to the "xp.sav" file

LoadGame(loadlevel); //load level in the strcpy string

Re: Loading the next level using strcpy - help!

PostPosted: Fri Mar 30, 2012 6:37 am
by skydereign
Two problems, first is with your if statements.
Code: Select all
if(level=1) // this is always true, and will set level equal to one

if(level==1) // this is what you want, it is true when level is equal to 1

Second, you are trying to write a string into an int, which I would tend to agree, that's a suspicious pointer conversion. You should change loadlevel to a string
Code: Select all
char loadlevel[30];

Re: Loading the next level using strcpy - help!

PostPosted: Fri Mar 30, 2012 11:29 am
by Johnno
Hey thanks for the explanation of "if (level==1)".
I'm not sure where to put "char loadlevel[30];" nor what it really does.

I'm getting "Unexpected declaration" when I put it in here..
Code: Select all
loadVars ("game.sav", "data"); //load the game save file "game.sav"
level = SaveLevel;

if (level==2); //game level 2
char loadlevel[30];
strcpy(loadlevel, "level02.dat"); //load level02.dat


if (level==3); //game level 3
strcpy(loadlevel,"level03.dat"); //load level03.dat


if (level==4); //game level 3
strcpy(loadlevel,"level04.dat"); //load level04.dat

SaveXP = xp; //the xp score

saveVars ("xp.sav", "data"); //save the xp score to the "xp.sav" file
LoadGame(loadlevel); //load the level in the "strcpy" string


Can you give me an example of it working and what it is doing in the code. Once I understand that I should be alright.

Sorry, I'm a newbie. I've only been using GE for about 4 weeks now, and I'm starting to get to the limit of my (short) programing knowledge.

Re: Loading the next level using strcpy - help!

PostPosted: Fri Mar 30, 2012 3:17 pm
by Jagmaster
Variable declarations like that can go in global code. What that line does is create a string with the size of 30. You could also go to the variable tab and make a global string variable if you wanted to. Doing it in global code makes it easier to edit later.

Another thing about the if statements; those statements shouldn't have a semicolon after them. It's also customary to put the "functions to be executed if the statement is true" within brackets. If you only have one function to do, then I think you can do it without brackets, but whatever function has to be followed by the statement in the same line (correct me if I'm wrong).

if(something)
{
dosomething();
dosomethingelse();
}

if(something)dosomething();

I'll also point out that it might be less cumbersome if you just put the loadgame lines in the if statements directly.
Like so...

Code: Select all
    loadVars ("game.sav", "data"); //load the game save file "game.sav"
    level = SaveLevel;
    SaveXP = xp; //the xp score
    saveVars ("xp.sav", "data"); //save the xp score to the "xp.sav" file

    if (level==2) //game level 2
{
    LoadGame( "level02.dat"); //load level02.dat
}


    if (level==3) //game level 3
{
    LoadGame( "level03.dat"); //load level03.dat
}


    if (level==4) //game level 4
{
    LoadGame( "level04.dat"); //load level04.dat
}

   
   

Just make sure that any function you still want to run is executed before the LoadGame function. Otherwise the game will load and you'll be in a different file.

Re: Loading the next level using strcpy - help!

PostPosted: Fri Mar 30, 2012 8:31 pm
by Johnno
Hey thanks Jagmaster,

It's working, I don't know why I complicated that so much.
I appreciate the explanation about Variable declaration too, I'm staring (very slowly) to understand. Might need to do some more general c++ tutes.