Saving, Loading text from files. I need help

Talk about making games.

Saving, Loading text from files. I need help

Postby xenidius93 » Sun Nov 16, 2014 5:50 pm

Hello. I need to make highscore for my game. I can make only working "points", because I don't know, how to save & load names/nicks.

If someon can help me :/, here's the project. In actor GAME_END, in the script's my problem. "Error line 13: Incompatible types: cannot convert from '* char' to 'ARY[256]char'". But why? BestDude and Dude are strings... really I don't know, how to do this... Please help me -_-"

https://www.mediafire.com/?bgf4ha29mgo24d2
xenidius93
 
Posts: 37
Joined: Mon Jul 28, 2014 4:24 am
Score: 1 Give a positive score

Re: Saving, Loading text from files. I need help

Postby gamemakerdude » Mon Nov 17, 2014 2:13 am

I can't say for certain because I am running the old version of GE on Mac but from the way you worded it, I would venture you could solve this by using the sprintf function to write one string into the other. For example...

Code: Select all
sprintf(bestDude, "%s", Dude); // writes whatever is contained in the Dude string into the bestDude string


If that doesn't fix your problem, I'll try to look at it on a windows computer the next time I have the chance.
User avatar
gamemakerdude
 
Posts: 72
Joined: Thu Sep 13, 2012 4:12 pm
Location: Texas
Score: 7 Give a positive score

Re: Saving, Loading text from files. I need help

Postby xenidius93 » Mon Nov 17, 2014 1:10 pm

Thank you, but the problem is with this script:

loadVars("highscore", "highscore");
Phighscore = highscore;
if(Phighscore < Ppoints)
{
Phighscore = Ppoints;
highscore = Phighscore;
saveVars("highscore", "highscore");
CreateActor("ENTER_NAME", "icon", "(none)", "(none)", -100, 0, true);


loadVars(BestDude, "BestDude");

BestDude = Dude;
saveVars(BestDude, "BestDude");
}


And here's this error "Error line 13: Incompatible types: cannot convert from '* char' to 'ARY[256]char'"
xenidius93
 
Posts: 37
Joined: Mon Jul 28, 2014 4:24 am
Score: 1 Give a positive score

Re: Saving, Loading text from files. I need help

Postby gamemakerdude » Tue Nov 18, 2014 1:28 am

Your problem in this code is the line that states:

Code: Select all
BestDude=Dude;


Instead, replace it with this.
Code: Select all
sprintf(BestDude, "%s", Dude);
User avatar
gamemakerdude
 
Posts: 72
Joined: Thu Sep 13, 2012 4:12 pm
Location: Texas
Score: 7 Give a positive score

Re: Saving, Loading text from files. I need help

Postby xenidius93 » Tue Nov 18, 2014 11:25 am

Okay, but it still doesn't load the BestDude variable from text file BestDude.txt :/. Probably it's very simple fail in my code, but it's not that simple for me :cry:
xenidius93
 
Posts: 37
Joined: Mon Jul 28, 2014 4:24 am
Score: 1 Give a positive score

Re: Saving, Loading text from files. I need help

Postby lcl » Tue Nov 18, 2014 11:59 am

xenidius93 wrote:Okay, but it still doesn't load the BestDude variable from text file BestDude.txt :/. Probably it's very simple fail in my code, but it's not that simple for me :cry:

Your problem is not that it isn't loading the variable, but instead that you never save any information into it. It seems like as if you we're saving the contents of Dude, but you have never set the Dude variable to hold any characters.
Another problem is that even if you first set Dude to contain the text of the ENTER_NAME actor, it still won't work with just that code, because that code gets run the instant GAME_END actor is clicked, so nothing has been written on the ENTER_NAME actor before saving. I'd recommend you to scrap that code and give it another try. Some things to note:

1) You don't have to have a new save group for each variable. Actually, it makes a lot more sense to have all the variables that are saved and loaded at the same time, to be in the same save group. So, instead of having save groups BestDude and highscore (by the way, that naming is bad practice because it can lead you get the save group names and the variable names mixed, use something like "highscoreGroup" instead).


2) Just so that you know, there are far easier ways of defining global variables than having a complete Global Code page dedicated to only one variable declaration.
You can either list all the global variables in the same Global Code page, like this:
Code: Select all
char * Dude;
int Phighscore;
int Ppoints = 0;

And then give the page a name like "Global variables". Again, you're current naming practice is bad in this place as well. You are cheating yourself into thinking that the names of the Global Code pages are somehow related to the names of the variables you're defining within them.

Or you can just create the global variables through the Variables dialog. Whether to use this or the upper method just depends on what you prefer, but you definitely shouldn't create new Global Code pages for each global variable you want to define.


3) About saving and loading.. Whenever you want to save variables, first make sure you have set some values to each of the variables in the save group, and then call saveVars() on that save group.
And when loading variables, first call loadVars() on the correct save group, and then use the values loaded into the variables.
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Saving, Loading text from files. I need help

Postby xenidius93 » Tue Nov 18, 2014 12:50 pm

Hmm. Then can you tell me, how can I save writing text via ENTER_NAME to string BestDude?
xenidius93
 
Posts: 37
Joined: Mon Jul 28, 2014 4:24 am
Score: 1 Give a positive score

Re: Saving, Loading text from files. I need help

Postby lcl » Tue Nov 18, 2014 2:29 pm

Just use sprintf() or strcpy() to copy the contents of ENTER_NAME.text to BestDude and then call saveVars() for the correct save group. Then, when loading, call loadVars() for the save group and then use sprintf() or strcpy() to copy the contents of BestDude to the text of the actor that shows the highscore keeper's name.
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Saving, Loading text from files. I need help

Postby xenidius93 » Tue Nov 18, 2014 3:38 pm

lcl, tell me, what's wrong with this now, except variables in global code? :|
https://www.mediafire.com/?4mtj2eecbktcj4g

Can you show me very simple save string/load string method with code written by you? Or maybe simple project, or something?
xenidius93
 
Posts: 37
Joined: Mon Jul 28, 2014 4:24 am
Score: 1 Give a positive score

Re: Saving, Loading text from files. I need help

Postby lcl » Tue Nov 18, 2014 5:04 pm

What was wrong with your code was that you always just stored an empty string to BestDude, because you called saveVars in the wrong place, in the mouse button down of the GAME_END actor, before the player gets a chance to write anything into the ENTER_NAME actor. You should have saved it in the ENTER_NAME actor's key down (return) event.

But since your ged is a little messy and codes are in wrong places, and that makes it difficult to understand, here's a very basic ged of how to save and load a string variable. :)
Attachments
saveLoadString.zip
(422.29 KiB) Downloaded 141 times
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Saving, Loading text from files. I need help

Postby xenidius93 » Fri Nov 21, 2014 4:36 pm

YOU ARE MY MASTER! :D. Thank you soo much. Finally I can continuexD
xenidius93
 
Posts: 37
Joined: Mon Jul 28, 2014 4:24 am
Score: 1 Give a positive score

Re: Saving, Loading text from files. I need help

Postby lcl » Fri Nov 21, 2014 4:52 pm

Haha, you're welcome. I'm glad I was of help. :D
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest