Saving/Loading?

Non-platform specific questions.

Saving/Loading?

Postby Lacotemale » Sat Aug 17, 2013 6:32 pm

So I want to save my game. I decided, simple... I will just use saveVars. However, saveVars only works for global variables it seems and some of my stuff like arrays is declared locally like:

Code: Select all
int numArray[10];


How can I save everything? Do I need to use an alternative method?

Thanks in advance for any help!
User avatar
Lacotemale
 
Posts: 285
Joined: Wed Dec 08, 2010 7:47 pm
Location: /home
Score: 7 Give a positive score

Re: Saving/Loading?

Postby skydereign » Sat Aug 17, 2013 7:03 pm

Lacotemale wrote:However, saveVars only works for global variables

Not quite. saveVars only works with global variables in save groups (therefore they must be created by the variable gui). You can create an array by creating a global variable, and setting array to yes. They also need to be put into a save group which is how saveVars works. If you want to save variables created in global code you'll need to do something different.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Saving/Loading?

Postby Lacotemale » Sat Aug 17, 2013 7:30 pm

Problem with declaring arrays in the GUI is I can't do 2D arrays like: numArray[1][5];

:|

What is the alternate method?
User avatar
Lacotemale
 
Posts: 285
Joined: Wed Dec 08, 2010 7:47 pm
Location: /home
Score: 7 Give a positive score

Re: Saving/Loading?

Postby skydereign » Sat Aug 17, 2013 9:58 pm

The other method is to use file pointers (FILE*). You can open a file, and write the contents of your array into it. Then do the reverse operation when reading it back in. You have a few choices for the actual writing and reading. If you use fprintf/fscanf then you can create human readable text files. If that isn't what you want, you could use fwrite/fread. There are several topics on these forums about file pointers if you need somewhere to start.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Saving/Loading?

Postby Lacotemale » Sat Aug 24, 2013 10:27 am

Actually I might be able to make saveVars and loadVars work for me. Would this be possible?

Code: Select all
//saveVars

data[0]=someValue1;
data[1]=someValue2;
//etc...

//loadVars

someValue1 = data[0];
someValue2 = data[1];
//etc...


I want your opinion before I go and do all the coding. (and maybe find out it doesn't work) :D
User avatar
Lacotemale
 
Posts: 285
Joined: Wed Dec 08, 2010 7:47 pm
Location: /home
Score: 7 Give a positive score

Re: Saving/Loading?

Postby skydereign » Sat Aug 24, 2013 7:39 pm

That would work, but remember one dimensional arrays can be saved using saveVars and loadVars. You were talking about two dimensional arrays, which is why you would need FILE*. Anyway, that method is a rather bad way to go about it, as you are doubling the number of variables, without needing to.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Saving/Loading?

Postby Lacotemale » Sat Sep 14, 2013 4:48 pm

I got loading saving working for one variable! :mrgreen:

However, I wanted to write out structs and it is proving to be difficult. :?

Code: Select all
fwrite(&charDataArray, sizeof(struct charData),1, WRITE);


It reports something about a lvalue missing. :|
User avatar
Lacotemale
 
Posts: 285
Joined: Wed Dec 08, 2010 7:47 pm
Location: /home
Score: 7 Give a positive score

Re: Saving/Loading?

Postby skydereign » Mon Sep 16, 2013 9:25 am

From a bit of testing, that seems to trigger when the variable doesn't exist.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Saving/Loading?

Postby Lacotemale » Mon Sep 16, 2013 1:04 pm

Ah you are correct! Its working now (kinda) but the Char* values are getting corrupted or something. :?
User avatar
Lacotemale
 
Posts: 285
Joined: Wed Dec 08, 2010 7:47 pm
Location: /home
Score: 7 Give a positive score

Re: Saving/Loading?

Postby Lacotemale » Thu Oct 17, 2013 7:15 pm

So writing to file works but not reading. The value in the file is 9.

The following code in a C file:

Code: Select all
if(fscanf(READ, "%d", &i) == 1)
   {
     gold = (READ, "%d", &i);
   }


Produces the value 9, however... inside GameEditor the above code produces 8571312. What is going on here?? :shock:
User avatar
Lacotemale
 
Posts: 285
Joined: Wed Dec 08, 2010 7:47 pm
Location: /home
Score: 7 Give a positive score

Re: Saving/Loading?

Postby skydereign » Sat Oct 19, 2013 3:56 am

That code I don't think is required to work (in terms of the C standard). The line where you try to set gold doesn't make sense. Regardless you shouldn't be doing that, as fscanf already read in that value. Just set gold equal to i, or skip the if statement entirely, and just use &gold instead of &i (whichever happens to suit your code better).
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Saving/Loading?

Postby Lacotemale » Mon Oct 28, 2013 12:53 pm

I set gold = i but now it just crashes GameEditor. :/
User avatar
Lacotemale
 
Posts: 285
Joined: Wed Dec 08, 2010 7:47 pm
Location: /home
Score: 7 Give a positive score

Re: Saving/Loading?

Postby skydereign » Mon Oct 28, 2013 7:54 pm

Lacotemale wrote:I set gold = i but now it just crashes GameEditor. :/

What version and os are you using? And what is the code that causes that?
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Saving/Loading?

Postby Lacotemale » Mon Oct 28, 2013 8:26 pm

I was running the Windows version on Linux... but only now did I try the Linux version and it works!!! XD

Sorry for causing trouble over a non-issue. :oops:
User avatar
Lacotemale
 
Posts: 285
Joined: Wed Dec 08, 2010 7:47 pm
Location: /home
Score: 7 Give a positive score

Re: Saving/Loading?

Postby skydereign » Mon Oct 28, 2013 8:37 pm

Lacotemale wrote:I was running the Windows version on Linux... but only now did I try the Linux version and it works!!! XD

Yeah, the windows version of 1.4.0 had an file io problem that caused odd behaviour.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron