Loading the next level using strcpy - help!

Game Editor comments and discussion.

Loading the next level using strcpy - help!

Postby Johnno » Thu Mar 29, 2012 11:14 pm

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.
Johnno
 
Posts: 23
Joined: Thu Mar 22, 2012 2:56 am
Score: 4 Give a positive score

Re: Loading the next level using strcpy - help!

Postby Jagmaster » Fri Mar 30, 2012 4:29 am

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.
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: Loading the next level using strcpy - help!

Postby Johnno » Fri Mar 30, 2012 5:54 am

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
Johnno
 
Posts: 23
Joined: Thu Mar 22, 2012 2:56 am
Score: 4 Give a positive score

Re: Loading the next level using strcpy - help!

Postby skydereign » Fri Mar 30, 2012 6:37 am

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];
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Loading the next level using strcpy - help!

Postby Johnno » Fri Mar 30, 2012 11:29 am

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.
Johnno
 
Posts: 23
Joined: Thu Mar 22, 2012 2:56 am
Score: 4 Give a positive score

Re: Loading the next level using strcpy - help!

Postby Jagmaster » Fri Mar 30, 2012 3:17 pm

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.
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: Loading the next level using strcpy - help!

Postby Johnno » Fri Mar 30, 2012 8:31 pm

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.
Johnno
 
Posts: 23
Joined: Thu Mar 22, 2012 2:56 am
Score: 4 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest