Custom Save System Help [SOLVED]

Started by Stranga, Tue 01/08/2017 04:01:56

Previous topic - Next topic

Stranga

Hello everyone!

I am working on a new game and I would like to build my own custom save system. What I would like to achieve by this is that at certain points in my game you use a telephone to save the game. But because I will be frequently updating the game with new assets I want to build my own save system using .dat files if possible.

What I want to know is:

- If or can I write Global Variables, Items, room and player position to a .dat file and load all that information.

I'm not sure if this would actually work if new assets were added to the game but I thougt I'd try and give it a shot.

HOW I GOT IT TO WORK!

First of all I am using dat files. I may change to ini's with the plugin later (not sure yet).

So when the player interacts with the phone(Save Point) my function looks like this

Code: ags


function oTelephone_Interact()
{
player.Walk(153, 118, eBlock, eWalkableAreas);
player.FaceDirection(eDirectionUp);

xPlayer = player.x;
yPlayer = player.y;
PlayerRoom = player.Room;

File *output = File.Open("$SAVEGAMEDIR$/Save.dat", eFileWrite);
//Write Player co-ordinates and room
output.WriteInt(xPlayer);
output.WriteInt(yPlayer);
output.WriteInt(PlayerRoom);
output.Close();
Display("Game Saved");
}


And when you click continue

Code: ags


function bContinue_OnClick(GUIControl *control, MouseButton button)
{
//  show_restore_game_dialog();

File *input = File.Open("$SAVEGAMEDIR$/Save.dat", eFileRead);
xPlayer = input.ReadInt(); 
yPlayer = input.ReadInt();
PlayerRoom = input.ReadInt();
input.Close();
player.ChangeRoom(PlayerRoom, xPlayer, yPlayer);
}


Basically, you can add any Global Variables to this and it works fine(I Think, so far only tested with int). I have also tested adding new assets to AGS and it still works without crashing the game! Although, this is just a simple method and a work around. To save all parameters, I believe this may or may not work.

Kara Jo Kalinowski

#1
Well sure, it's possible, but to my knowledge you can't just write objects/game characters to a file, you'd have to code everything yourself and then also write the code to load everything back in the format you chose. So you'd write the important data like object ID, room ID, x/y position, and then do inventory, and then do player position, but basically it's all coding everything by hand...by code anything is possible if you're good enough.

Disclaimer: I'm a newbie to AGS but from what I've seen, you can either 1) Deal with the built-in functions, 2) Find a module that someone else wrote that handles it, or 3) Code what you want and that's more customizable.

Stranga

See, I thought about that because I've done a similar save system in Game Maker Studio where you store everything in ini files. I thought it may have been the same process with the .dat files (besides AGS and the GM language). I suppose if it's possible where would be a good starting point? I have used the .dat files before with my other game, but they stored no data (It was an easier way to add unlockable content). And I only need the important data anyway to be stored which is a few global variables/strings, player x/y position and room ID (not worried about the inventory).

Cassiebsg

Well, you can either use the Write/Read built in function, but you'll be have to write everything sequentially (and you'll only be able to read it sequentially too... as far as I understand the manual) or you can also use an ini/txt file. There's a module for the ini file (think it's called FileIni or something similar).
And yes, you'll have to write all the info your self...
There are those who believe that life here began out there...

Stranga

I may be able to use the write/read function because I only want to be able to save the game sequentially. But I'll also take a look an an ini and text file save system also (Just to see what works out better).

JanetC

I tried this and I found it to be pretty much impossible due to not every important parameter being both read and write.

Crimson Wizard

#6
You do not have to save everything, just save cruicial story bits.

Be prepared for this: since AGS does not let you load and save data in other rooms, you would have to recreate room situation in every Room_Load function using some global variables for the reference, in case the game was loaded up recently.

If you are planning on upgrading the game, it is important to give indentifying names to things you write. Otherwise you will get into same problem as built-in save system in AGS has currently.

Text format would be much more convenient to use (like INI and JSON), only if AGS had built-in support for such. But since it does not, you would need a module or plugin for that. And with large amount of data reading such format with the lack of good string utilities in AGS (like key/value dictionaries for fast lookup) will be slow, and save files will be generally larger than in binary.
Depending on what you are going to write there, this may or not be a problem.
(Modern games often use zipped text files for save, to greatly reduce otherwise huge text file)
EDIT: I recall there was a string hash module, which may be of help here, but I never tried it myself, and do not know how actually good it is: http://www.adventuregamestudio.co.uk/forums/index.php?topic=54749.0

Stranga

The more and more I think about AGS's limitations with this is starting to seem less appealing. I've been testing a few things with ini files where I have say like a home base room. Each time I return there all global variables reset except for my story variable. That controls the progression in my game (Which works quite nicely).  I still have yet to test this custom save idea out. I do like that string hash idea CW. I did have an idea to Just save the global variables and creat say like a global SaveLocation variable and save a number which indicates a room. When loading it reads the variable number and for example if SaveLocation==2 player.changeroom(xy blah blah); then it reloads the other saved variables. This was my work around but I'm also not sure if that'll work.

Crimson Wizard

#8
In a team project I am working on right now there is a story warp system for testers that recreates any stage in game from just a dozen of story variables. If you do not need to save individual state (coordinates and animation frames) of every object in game, then custom save is feasible, just requires good planning.

Snarky

Quote from: JanetC on Tue 01/08/2017 14:59:24
I tried this and I found it to be pretty much impossible due to not every important parameter being both read and write.

How do you mean? If a parameter cannot be written, surely it won't change in-game, so you shouldn't need to store it anyway.

Crimson Wizard

Quote from: Snarky on Tue 01/08/2017 17:08:28
Quote from: JanetC on Tue 01/08/2017 14:59:24
I tried this and I found it to be pretty much impossible due to not every important parameter being both read and write.

How do you mean? If a parameter cannot be written, surely it won't change in-game, so you shouldn't need to store it anyway.

I think Janet could mean that some properties can be set but cannot be got.

Also, the biggest problem is that you cannot access any rooms but the one you are currently in, to save or load their states.

I think the only way currently is to define room states in a limited number of global variables.

Snarky

These problems don't seem that hard to work around:

1. For "ungettable" properties, put a wrapper around writes to store them in a variable yourself.
2. Save room state on exit and load it on entry.

Stranga

Crimson Wizard, your story warp idea is exactly what I am looking for! I just need to know how to implement it in a .dat or ini so when the game loads or the player clicks continue it will read the variable in the .dat and change the player to that room only and read or even set a global story variable to tell the player where they are in the story line. Basically, the only places to save the game will be when the player has no items in their inventory or between chapters. (If any of this makes sense)

Stranga

The only problem I could see with saving a room state is that I would not be able to update the game frequently. For instance, I plan to release my game in chapters. So once one chapter is done it will be released. Now if I add new assets to AGS this will initially disrupt the save state system or the inbuilt save system (Which I think is just a save state system anyway).

I think using Crimson Wizards Story warp idea may work because all I'd have to do is just read say one variable from an ini or dat file that tells where abouts the player is in the story line and just transfers the player to that room.

This is just a "work around" but if building an entire save system by reading and storing room states is feasible without the game crashing because of an update then that would be awesome!

Stranga

Solved it! Added my solution to original post!

Snarky

Yes, it's possible, but you would only save and restore data that you specify in the save/load functions: there's no way to just automatically save everything in the room (or rather, there is: The built-in AGS savegame functionality). If you add an asset, you just have to specify what to do when you load a save-game that doesn't have that asset (set the variables related to the asset to some default value, probably).

The limiting factor is really how many things you can be bothered to save, given that you have to deal with each of them individually and manually.

Stranga

Yeah I know, this was only my solution and what I was trying to achieve. If you're only saving a few things it seems fine. And coming from using Game Maker Studio you basically need to build your own save system from scratch anyway (which is  pain).

SMF spam blocked by CleanTalk