Reading a Save Slot Description-SOLVED

Started by Kinoko, Wed 28/07/2004 08:08:33

Previous topic - Next topic

Kinoko

I need to set 4 labels in a GUI to read "EMPTY" the very first time the game is run, and then differ every other time depending on what the player has saved (that part I can figure out on my own).

The way I figured I'd do this (since I couldn't just SetLabelText to "EMPTY" in game start, as it would do that every time) is by inputting some misc text like "Nothing" into the GUI labels in the GUI editor, and in game_start (I tried a lot of things before I got to this code):

Code: ags

SetLabelText (11, 4, saveslota);
StrCopy (placename, "EMPTY");

if (StrComp ("Nothing", saveslota)==0) {
StrFormat (saveslota, "%s", placename);
}


(I'm using StrFormat instead of directly putting "EMPTY" in with StrCopy because it's convenient for me in the future.)

Of course this doesn't work because saveslota hasn't been given a string yet, which I didn't think about at first. Is there some way to read whatever text is directly typed into the GUI labels' properties window?

The only other way I can think of is to make the label a list box instead, and then use a code like:

Code: ags

ListBoxGetItemText (11, 26, 1, saveslota);

StrCopy (placename, "EMPTY");

if (StrComp ("Nothing", saveslota)==0) {
StrFormat (saveslota, "%s", placename);
}


Which then leaves me with the problem of inputting text into the listbox for the first time the game is executed only.

Anyone? ^_^; I'd appreciate any suggestions.

Jet Kobayashi Zala

#1
Why not set the labels' default text to "EMPTY" and then replacing it with the save data information in game_start ()?

That is, in game_start (), something like this:

Code: ags

string buffer;
int handle;

if ( handle = FileOpen ( "savegame0.sav", FILE_READ ) { // Using the first save slot
Ã,  FileRead ( handle, buffer ); // Or whatever to read save data
Ã,  FileClose ( handle ); // Don't forget to close it
Ã,  SetLabelText ( SOMEGUI, 1, buffer ); // Set the label
}


That should do the trick, I believe. I'm not familiar with FileRead () specifically, but that should work as a general idea.

Gilbert

If the games are saved with AGS's standard save game system, use GetSaveSlotDescription() instead. Eg:
if (GetSaveSlotDescription (1, saveslota)) SetLabelText (11, 4, saveslota);
else SetLabelText (11, 4, "EMPTY");


If you want to enter text directly into a label, I think the only way is to make it into a textbox. However, as you want 4 of such labels at the same time that can cause problem, my suggestion is to provide buttons to save to eact slot, when you click a button, it'll prompt you for the description.

Kinoko

#3
Actually, I don't want a typed description, the description will be default depending on where the player saves. Anyway, I have the saving part of this down. The only part that's a problem is finding out if there are saved files already er... in existance.

I didn't explain it very well to begin with, so sorry for that. I was really hoping nobody had replied to this because 5 minutes after I turned the net off, I realised my thinking was flawed :P For some reason, I was thinking that once text had been set in the game, it would stay like that next time by default. Blame hours and hours of scripting beforehand frying my brain.

So, I guess my new concern is whether I can see if a saved file exists, and if it's possible to read certain variables from them. If it's possible, I'd like to have a couple of variables like time elapsed and level (save game specific stuff, anyway) displayed at the start of the game.

EDIT: Actually, I was thinking, if that sort of thing is too difficult...

Would it be possible to, at the time of saving a game, write the variables I want displayed to a seperate file that's pulled up every time this save game dialogue is pulled up? I'm sorry if the answer to this is obvious from the manual, but all the File functions are very new to me.

Gilbert

#4
Well my codes already handles the part whether the save game is available, as for the if (GetSaveSlotDescription (1, saveslota))... condition.

Copied from manual:

GetSaveSlotDescription (int slot, string buffer)

Gets the text description of save game slot SLOT into the provided BUFFER. If the slot number provided does not exist this function returns 0, if successful it returns 1.

Quote from: Kinoko on Wed 28/07/2004 10:52:42
So, I guess my new concern is whether I can see if a saved file exists, and if it's possible to read certain variables from them. If it's possible, I'd like to have a couple of variables like time elapsed and level (save game specific stuff, anyway) displayed at the start of the game.

EDIT: Actually, I was thinking, if that sort of thing is too difficult...

Would it be possible to, at the time of saving a game, write the variables I want displayed to a seperate file that's pulled up every time this save game dialogue is pulled up? I'm sorry if the answer to this is obvious from the manual, but all the File functions are very new to me.

It's actually very possible, apart from using separate files to save the info (which is not that hard, but you have to be familiar with the file functions), you can actually do this by putting all the info. you need into the save game description text and retrieve them and analyze them using the Str... functions later.

For example, if you want to save and retrieve the following 3 data fields to and from savegame:
- Text (say all of them are of EXACTLY 16 characters)
- Time (say, in seconds, maxed to 999999)
- Number of savings (say, maxed to 9999 times)

You may design the description to be of this format:
16 characters of text, 6 characters to store time, 4 characters to store number of savings -- that adds up to 26 characters in length. So, for example the following string:
"FOUNTAIN        0012040005"
contains the text "FOUNTAIN        " (note that it's exactly 16 characters), Time=1204 seconds, Savings=5 times.

To do that is not that difficult, provided text is a string holding a text of EXACTLY 16 characters, time holds the time in second, saves holds the number of times you saved. The following line of code will format a string to the desired format as mentioned above:
StrFormat(tmpstr,"%s%06d%04d",text,time,saves);
Which you can use as save game description in your save games:
SaveGameSlot (slot, tmpstr);

(since it's getting long, I'll start a new post for the retrieving part)





Gilbert

(Continued)
To retrieve the info back from a save game slot it's a bit tricky (as String functions for AGS are not quite complete at the moment).
Basically what you need is to:
Step 1: Get the description text into a temp string
Step 2: Get its first 16 characters as text
Step 3: Convert the next 6 characters into interger time
Step 4: Convert the next 4 characters into interger saves

Idea for codes:
int ii;
string tmpstr, tmpstr2;
if (GetSaveSlotDescription (slot, tmpstr)) {// if save game available, completes Step 1
Ã,  StrCopy(text, tmpstr);
Ã,  StrSetChar(text, 16, 0); //Truncates to 16 characters, completes Step 2
Ã,  ii=16;
Ã,  StrCopy(tmpstr2,"");
Ã,  while (ii<=21) { //extract character #16 to #21 into tmpstr2
Ã,  Ã,  StrFormat(tmpstr2,"%s%c",tmpstr2,StrGetCharAt(tmpstr,ii));
Ã,  Ã,  ii++;
Ã,  }
Ã,  time=StringToInt (tmpstr2); //Completes Step 3
Ã,  StrCopy(tmpstr2,"");
Ã,  while (ii<=25) { //extract character #22 to #25 into tmpstr2
Ã,  Ã,  StrFormat(tmpstr2,"%s%c",tmpstr2,StrGetCharAt(tmpstr,ii));
Ã,  Ã,  ii++;
Ã,  }
Ã,  saves=StringToInt (tmpstr2); //Completes Step 4
} else { //Save slot does not exists
Ã,  StrCopy(text, "EMPTY");
Ã,  time=0;
Ã,  saves=0;
}


This is just an idea, and the codes weren't tested, also, you may need to modify them to suit your own needs.



Kinoko

FANtastic ^_^ Thanks. I'll muck around with this for a bit and let you know how it goes.

Bernie

Here's another way to check if a save game exists:

If you retrieve the save slot description of a nonexistant game, the text retrieved will be invalid slot 1, invalid slot 2 and so on.

You could run a check if the save slot description contains "invalid slot" and then set the label text to "empty".

Kinda like this:

string save1;
GetSaveSlotDescription(1,save1);
if (StrContains(save1, "invalid slot")>-1) {StrCopy(save1, "Empty 1");}
SetLabelText(SAVEGUI,0,save1);

GetSaveSlotDescription(2,save1);
if (StrContains(save1, "invalid slot")>-1) {StrCopy(save1, "Empty 2");}
SetLabelText(SAVEGUI,1,save1);

...and so on.

Jet Kobayashi Zala

Yeah, sorry, I should have explained it better. Just GetSaveSlotDescription () or FileOpen gets an error (like a file not existing), it returned 0. My bad. I should have been more thorough. Sumimasen.

Gilbert

Actually checking the return value of GetSaveGameDescription() is the safest way, as it's official and is documented. Those "invalid..." descriptions weren't documented, and can change at any point of updates, furthermore, checking the content of a string is considered more troublesome than just checking an integer return value.

Kinoko

Okay, it took me many, many hours late at night and my sanity, but I got it working based on your code, Gilbot, thanks! ^_^ Your code was fine but I had to make it work in my script and fix various things up and... well, it took awhile. If anyone else wants to use the code...
This line:
Code: ags
StrSetChar(text, 16, 0); //Truncates to 16 characters, completes Step 2


Just needs to be changed to this:
Code: ags
StrSetCharAt(text, 16, 0); //Truncates to 16 characters, completes Step 2

Gilbert

Heh yea, my fault for not testing the codes first. ;)

SMF spam blocked by CleanTalk