Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Ethan D on Fri 03/07/2009 00:00:50

Title: GUI for saving game
Post by: Ethan D on Fri 03/07/2009 00:00:50
Im not sure what is wrong I used an article on the AGS help site but I cant get this part to work properly.  No errors come up but it doesnt actually save anything and no name appears in the list box.  The code below is for the save button.  Any ideas?


function Button8_OnClick(GUIControl *control, MouseButton button)
{
int totalSaves = lstSaveGames.ItemCount; //number of saves on list
String saveName = txtSaveName.Text; //holds value of txtSaveName

//Check for a save name. If none, tell them.  Else, go on.
if(saveName == "")
{
   Display("Enter a name for your save file.");
   return;
}
//If there is no saved games yet, just save it.
if(lstSaveGames.SelectedIndex == 1)
{
   SaveGameSlot(totalSaves+1, saveName);
   gSaveGame.Visible = false;
}
else
{
   int checkSave = 0; //runs through the list and checks for double entries
   while(checkSave != totalSaves)
   {
     if(saveName == lstSaveGames.Items[checkSave]) // one matches, so overwrite it.
     {
       //if so, overwrite the selected save game
       gSaveGame.Visible = false;
       SaveGameSlot(savegameindex[checkSave], saveName);
       return;
     }
     checkSave++;
   }
   //if we've made it here, then there is no match; just save to a new slot.
   if(totalSaves < 20)
   {
     gSaveGame.Visible = false;
     SaveGameSlot(totalSaves+1, saveName);
   }
   else
   {
     Display("The maximum number of saved games has been reached; overwrite or delete some old ones.");
   }
}
Title: Re: GUI for saving game
Post by: GuyAwesome on Fri 03/07/2009 00:24:18
One obvious thing:

if(lstSaveGames.SelectedIndex == 1)


Should maybe be -1? (I.e if nothing is selected, make new save.) But I don't know if that'd stop it from ever saving. (It's also missing a } at the end, but I'm assuming that's a copy/paste error :))

Do any names appear in the savebox if you skip the GUI and just use, e.g., SaveGameSlot(1, "Test"); (try assigning it to a keypress, for testing purposes...)? If not, the problem may not be with the 'save' code, but with the 'turning-the-GUI-on' code. What've you got there? Otherwise, unless you already have 20 savegames or always select the second one before saving, I can't see why that wouldn't work. In fact, once I changed the control names to fit, that function saves and loads fine for me.
Title: Re: GUI for saving game
Post by: Ethan D on Fri 03/07/2009 00:29:43
I had the game run the test save and it didnt show up.  Any idea why that wouldnt work?
Title: Re: GUI for saving game
Post by: GuyAwesome on Fri 03/07/2009 00:34:09
What do you mean by 'run the test save' - what slot are you trying to load, and how? As I said, that function - apart from a few control names (which would cause probably compile errors if they were wrong for you) - works fine.
Title: Re: GUI for saving game
Post by: Ethan D on Fri 03/07/2009 00:45:38
I tried this
SaveGameSlot(1, "Test");
and it didnt show up in the list box.
Title: Re: GUI for saving game
Post by: GuyAwesome on Fri 03/07/2009 00:49:14
If you try RestoreGameSlot(1); directly, does it load or crash? If it crashes, then like I said: "the problem may not be with the 'save' code but with the 'turning-the-GUI-on' code." (I.e., you're not actually filling the listbox with the save games...)

EDIT:
Also, are the savegames actually being created in the save folder? (By default 'My Documents\My Saved Games\(game name)')
Title: Re: GUI for saving game
Post by: Ethan D on Fri 03/07/2009 00:52:20
The game didnt crash it just reloaded the first page.  When you say turning on the
GUI do you mean making it visible?
Title: Re: GUI for saving game
Post by: GuyAwesome on Fri 03/07/2009 00:54:34
Yes, that's what I meant, sorry. (Also, see my edit, just before you posted...)
EDIT:
Actually, not just making it visible (e.g. gSaveLoad.Visible = true;) - that's kind of my point. You also need a ListBox.FillSaveGameList (http://www.adventuregamestudio.co.uk/manual/ListBox.FillSaveGameList.htm) in there, if you haven't got one...
Title: Re: GUI for saving game
Post by: Ethan D on Fri 03/07/2009 01:16:37
wow... that was simple.  I just didn't have the ListBox.FillSaveGameList.

I put it in the global repeatedly execute...I'm not sure if that's the right place though.
But it is working now

Thanks
Title: Re: GUI for saving game
Post by: GuyAwesome on Fri 03/07/2009 01:21:49
It's probably NOT the right place, actually ... It's not wrong, as such, just an unnecessary process - you don't need to update it every single game loop. Just add it whenever you turn the save/Load GUI(s) on. Check out the show_save_game_dialog() andshow_restore_game_dialog() functions in the default game template script, for example.

Out of interest, what article did you read that didn't bother to mention such an important step?

And, you're welcome :)
Title: Re: GUI for saving game
Post by: Ethan D on Fri 03/07/2009 01:40:09
It was this one: 
http://www.americangirlscouts.org/agswik/Creating_Custom_Save_and_Load_Dialogs

But I looked at it and it was at the bottom of the page which I hadnt gotten to yet.
Oh well.

Thanks for all the help.
Title: Re: GUI for saving game
Post by: Ethan D on Fri 03/07/2009 01:49:29
Well I thought I had it working but now whenever I try to load or save it says to select one even though it is selected. And when I click on another file it moves back to the first one instantly.  I cant find a reason in the code...

Do I need a code in the listbox?
Title: Re: GUI for saving game
Post by: GuyAwesome on Fri 03/07/2009 02:16:22
How many save games do you have, and do you still have that FillSaveGameList in rep_ex? It sounds like you have 20 (so you need to select one to overwrite in 'save' mode), and it keeps refreshing the ListBox contents (deselecting your selection). Constant refilling would also explain not being able to load (same thing - cleared selection).
Title: Re: GUI for saving game
Post by: Ethan D on Fri 03/07/2009 05:20:01
Yeah the problem was in the repeatedly executing of the fillsavegamelist.  Its working now.