2015-06-11 16:29:47

Hi everyone,
I'd like to know how I can make a save file in BGT.
I have a script where the variables might change, and I'd like to have them saved, when I open the script again for example, I want the location, the city and the name to be exactly as I've set them when I've opened the script for the first time.
I hope I've explained it well.
Thanks in advance.

2015-06-11 17:34:36

It really depends on the specifics of your program.

The simplest way is to just put all the variables into a string, separated by a character or set of characters that doesn't appear in any of the variables (\n, or \r\n are nice for this, if you don't have any multi-line strings to save).
You'd then save that string to a file.
To load it, you'd read the contents of the file, then use the string_split function to get an array, which hopefully contains a list of all the variables you saved, in string format. You might need string_to_number, if you're saving numbers. And if you're saving objects with multiple properties, things get more complicated, but the principal is the same.

Watch out for empty strings! String_split ignores empty entries, so if you save any empty strings this way, your loaded array will be shorter than you expect.

看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
    George... Don't do that.

2015-06-12 06:03:31 (edited by Sam_Tupy 2015-06-12 06:05:30)

Hi there. Well, honestly this is very easy. One salution that I might recommend is one that I use and is easy, also one that works with most games. It's the dictionary object. All you do, is declair the dictionary.
dictionary save;
Then add values, with what ever keys you want, I do it like this, here is a complete function to save settings:
void save_settings()
{
dictionary d;
d.set("1", location);
d.set("2", city);
// just continue adding with 3 as the key, and the variable as the value, continue encreasing the number every value/variable you want to add
string text_to_write=serialize(d);
// now, encryption! Do this unles you want other bgt geeks to be able to change your data!
text_to_write=string_encrypt(text_to_write, "no_touching_my_data");
// now let's save our data
file f;
f.open("save.dat", "wb");
f.write(text_to_write);
f.close();
} // end save function
So basicly in that function we saved all the values to a dictionary. the function wants: get(string key, ? value) and the ? value, means value can accept any type of variable.
and now, let's say we need to load that data we saved
void load_data()
{
// retrieve the data
file f;
f.open("save.dat", "rb");
string text_to_read=f.read();
f.close();
// now for decryption
text_to_read=string_decrypt(text_to_read, "no_touching_my_data");
// now to get the data back into a dictionary
dictionary d=deserialize(text_to_read);
// now, load the data
if(d.exists("1"))
d.get("1", location);
if(d.exists("2"))
d.get("2", city);
// and on and on for every new value that is saved, and there you go
}
Note that checking weather a value exists before actually restoring it is necessary or else you could end up having blank strings and zeroed integers, which might be different from default values. The get function wants: get(string key, &out ? value)
&out means it writes the data to a variable rather than returning anything, so if you have location, if said value was saved in the dictionary it would just write to the location variable. I hope that little idea helps, I do this and it works well for me.

I am a web designer, and a game developer. If you wish see me at http://www.samtupy.com

2015-06-12 09:12:45

I like the dictionary way a lot! big_smile

2015-06-13 15:28:14 (edited by stefan_ilioaica 2015-06-13 15:44:08)

Hi, Sam. Thank you very much for the dictionary metod, it works really fine. I have one problem though.
When I select the option "load a saved game", my data won't load, and my script simply closes.
In the menu code, I wrote something like:
if(menu_result==2)
{
load_data();
}

Does someone know which the problem could be? Should I do something in order to load my... last used function or something like that?
If I write in the following way
if(menu_result==2)
{
load_data();
courtyard();
}
Now, the function will load, and I'm on the coordinates where I saved the script last time.
But I want when I load the script, if I save in library for example, I want to be in library, not in the courtyard, or out of the script....
Or it's really necessary to write this if at every function, and then write load_data();, followed by another line with the function name where I saved the game?
Sorry if it might sound a bit crazy, but I wanted to describe my problem and what I want to do as detailed as I could.

2015-06-14 01:57:49

Ah, yes, I see your problem. Now this is whare we start running into issues. You need a load map function, as followed
void load_map(string load)
{
if(load=="cortyard")
{
cortyard();
}
}
and on and on, just keep adding those statements.
So
if(menu_result==2)
{
load_data();
load_map(location); // change location to what ever variable saves the map.
}
Hope this helps.

I am a web designer, and a game developer. If you wish see me at http://www.samtupy.com

2015-06-14 13:50:53

This is the same for menus?
I have a cortyard where you move with arrow keys, and I have also some other locations like bedroom, kitchen which are menu based, and there you can change some variables as well.
Sorry if some of those questions sounds stupid, but I'm not very experienced in programming, and I'm making it just for fun in my free time.

2015-07-14 05:32:52

What exactly are you trying to save?  That can be complicated

Add me on Skype, search for The Evil Chocolate Cookie

2015-07-17 17:35:34

hello,

What i have done is loading the script after i have executed the load_game script.
My locations are stored in a script called game. thus:

void game()
{
if(location == 0)
{
dalgrim();
}
if(location == 1)
{
the_sewers();
}
// and so forth
}

To load the stored data i would do:

if(menu_result == 1)
{
load_game();
game();
}

with this method the game won't close down since the game loads the next instruction. let's say the saved location was 1. than the game will load location 1 and open the game there.

hth

greetz mike

Visit the following website to see what games we have:
http://www.nonvisiongames.com
Or the following English marketplace to see what retrogames and game merchandise I am selling:
https://www.retro-kingdom.com