Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Totora Games on Fri 19/06/2020 05:28:26

Title: Custom Save/Load Button Problem
Post by: Totora Games on Fri 19/06/2020 05:28:26
Hi everyone! Sorry if this is a question that was asked a lot of times before. I'm programming a point & click adventure and I have design some custom GUI's as well. I made Save/Load buttons, but non of them seems to be working at all. If I save a game and then try to load it, when I enter the Load GUI the ListBox where the save games supposedly meant to be is empty. I can't find a solution and it gets very frustrating.

I'm pretty sure that this is a scripting error, but I can't find a way to solve it. I have very little knowledge about scripting and that's why I post this, hoping for someone who can help me.   

Thank you! I'll be waiting for your answers!
Title: Re: Custom Save/Load Button Problem
Post by: Slasher on Fri 19/06/2020 06:08:32
Without seeing how you have scripted it you could try this:

Code (ags) Select

// pressing load button that opens Load gui
show_restore_game_dialog();


Is the Save actually being added?

Code (ags) Select

// pressing save button that opens Save gui
show_save_game_dialog();
Title: Re: Custom Save/Load Button Problem
Post by: Crimson Wizard on Fri 19/06/2020 11:12:12
Quote from: Totora Games on Fri 19/06/2020 05:28:26
Hi everyone! Sorry if this is a question that was asked a lot of times before. I'm programming a point & click adventure and I have design some custom GUI's as well. I made Save/Load buttons, but non of them seems to be working at all. If I save a game and then try to load it, when I enter the Load GUI the ListBox where the save games supposedly meant to be is empty. I can't find a solution and it gets very frustrating.

I'm pretty sure that this is a scripting error, but I can't find a way to solve it. I have very little knowledge about scripting and that's why I post this, hoping for someone who can help me.   

It's nearly impossible to give an answer without knowing what exactly did you do. Please post related part of your script, one that deals with saving and loading.
Title: Re: Custom Save/Load Button Problem
Post by: Cassiebsg on Fri 19/06/2020 16:52:54
My guess is that you forgot to populate the list.

Also, what template, if any, are you using?
Title: Re: Custom Save/Load Button Problem
Post by: Totora Games on Fri 19/06/2020 21:01:38
Quote from: Slasher on Fri 19/06/2020 06:08:32
Without seeing how you have scripted it you could try this:

Code (ags) Select

// pressing load button that opens Load gui
show_restore_game_dialog();


Is the Save actually being added?

Code (ags) Select

// pressing save button that opens Save gui
show_save_game_dialog();


I tried this but then I get this error:

Undefined token 'show_restore_game_dialog'
Undefined token 'show_save_game_dialog'
Title: Re: Custom Save/Load Button Problem
Post by: Totora Games on Fri 19/06/2020 21:16:58
Quote from: Cassiebsg on Fri 19/06/2020 16:52:54
My guess is that you forgot to populate the list.

Also, what template, if any, are you using?

I made custom GUI's on Microsoft Paint, here's some screenshot in Google Drive links. The GUI's are for the most part in spanish, I'm from Argentina, so I apologize for that.

https://drive.google.com/file/d/1H5XgER3kSwpUJaXl34BTIN0L_Vz8lBTg/view?usp=sharing
https://drive.google.com/file/d/10ElhFzgKn4Z5DQfEDhVxhuQtHXFk2YA-/view?usp=sharing
https://drive.google.com/file/d/1VNtSHf-UoRveVfQp0LVTU5qK1lcKHbpP/view?usp=sharing
Title: Re: Custom Save/Load Button Problem
Post by: Cassiebsg on Fri 19/06/2020 21:31:49
Yes, but you need to post the code for the save/load buttons, or we can't help you.

And you didn't answer which template you are using, if any.
Title: Re: Custom Save/Load Button Problem
Post by: Totora Games on Fri 19/06/2020 21:49:31
Quote from: Cassiebsg on Fri 19/06/2020 21:31:49
Yes, but you need to post the code for the save/load buttons, or we can't help you.

And you didn't answer which template you are using, if any.

Sorry, this is the codes for the Save GUI and Load GUI. I copy them from AGS Wiki:

function btnConfirmSave_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 == "")
{
   return;
}
//If there is no saved games yet, just save it.
if(lstSaveGames.SelectedIndex == -1)
{
   gSavegame.Visible = false;
   SaveGameSlot(totalSaves+1, saveName);
}
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("Ya no hay espacio para guardar. Vas a tener que sobreescribir una partida. Perdón.");
}
}
}

function Button4_OnClick(GUIControl *control, MouseButton button)
{
gLoadGame.Visible = true;
}

function btnLoadGame_OnClick(GUIControl *control, MouseButton button)
{
  // pressing load button that opens Load gui
show_restore_game_dialog();
txtLoadName.Clickable = false;
int totalLoads = lstLoadGames.ItemCount; //holds number of saves in the list
String loadName = txtLoadName.Text; //holds TXT for Save Name; from use

//check that a name was entered to avoid problems
if(loadName == "")
{
   Display("Please select a saved game to be loaded.");
   return;
}
if(lstLoadGames.SelectedIndex == -1) //no highlighted item
{
   Display("Please select a saved game to be loaded");
}
else if(lstLoadGames.SelectedIndex >= 0) //an item is selected...
{
   RestoreGameSlot(savegameindex[lstLoadGames.SelectedIndex]);
   gLoadGame.Visible = false;
}
}
Title: Re: Custom Save/Load Button Problem
Post by: Totora Games on Fri 19/06/2020 23:19:49
Quote from: Cassiebsg on Fri 19/06/2020 21:31:49
Yes, but you need to post the code for the save/load buttons, or we can't help you.

And you didn't answer which template you are using, if any.

And the template of the game is the Empty Game
Title: Re: Custom Save/Load Button Problem
Post by: Cassiebsg on Sat 20/06/2020 07:25:58
So are you saying that all you did was just copy the text from the site and nothing else?

Did you, or did you not, connect the code to the buttons event? That is, select the button, click the events button (the one with a yellow ligthing symbol) and then click the ... to connect the button? Also, if you did so, what are your button names and which function name is shown on the event panel "On click" ?

And last, please wrap your code with the code tag (that's the # symbol here on the forum).
Title: Re: Custom Save/Load Button Problem
Post by: Totora Games on Sat 20/06/2020 18:21:49
Quote from: Cassiebsg on Sat 20/06/2020 07:25:58
So are you saying that all you did was just copy the text from the site and nothing else?

Did you, or did you not, connect the code to the buttons event? That is, select the button, click the events button (the one with a yellow ligthing symbol) and then click the ... to connect the button? Also, if you did so, what are your button names and which function name is shown on the event panel "On click" ?

And last, please wrap your code with the code tag (that's the # symbol here on the forum).

Yes, all the code I showed you was the "On Click" event for both the Save Button and the Load Button. I named them "btnConfirmSave" and "btnConfirmLoad". Also, I'd like to adress here that I can run the game without any problem, so I think that all the scripting is right but not functioning.

Here are the codes again, sorry if I didn't wrap them in the code tag earlier.

Code (ags) Select
function btnConfirmSave_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 == "")
{
   return;
}
//If there is no saved games yet, just save it.
if(lstSaveGames.SelectedIndex == -1)
{
   gSavegame.Visible = false;
   SaveGameSlot(totalSaves+1, saveName);
}
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("Ya no hay espacio para guardar. Vas a tener que sobreescribir una partida. Perdón.");
}
}
}

function btnLoadGame_OnClick(GUIControl *control, MouseButton button)
{
  // pressing load button that opens Load gui

txtLoadName.Clickable = false;
int totalLoads = lstLoadGames.ItemCount; //holds number of saves in the list
String loadName = txtLoadName.Text; //holds TXT for Save Name; from use

//check that a name was entered to avoid problems
if(loadName == "")
{
   Display("Please select a saved game to be loaded.");
   return;
}
if(lstLoadGames.SelectedIndex == -1) //no highlighted item
{
   Display("Please select a saved game to be loaded");
}
else if(lstLoadGames.SelectedIndex >= 0) //an item is selected...
{
   RestoreGameSlot(savegameindex[lstLoadGames.SelectedIndex]);
   gLoadGame.Visible = false;
}
}
Title: Re: Custom Save/Load Button Problem
Post by: Cassiebsg on Sat 20/06/2020 22:34:10
This is the code I have in an older game of mine, which is probably just copy/paste from the Sierra template (the only template that uses this kind of save/load menus).



Code (ags) Select

function show_save_game_dialog()
{
  gSaveGame.Visible = true;
  // Get the list of save games
  lstSaveGamesList.FillSaveGameList(); /// Note. I think this is the code you're missing, it populates the list
  if (lstSaveGamesList.ItemCount > 0)
  {
    // If there is at least one, set the default text
    // to be the first game's name
    txtNewSaveName.Text = lstSaveGamesList.Items[0];
  }
  else
  {
    // No save games yet, default empty text.
    txtNewSaveName.Text = "";
  }
  mouse.UseModeGraphic(eModePointer);
  gIconbar.Visible = false;
}

function show_restore_game_dialog()
{
  gRestoreGame.Visible = true;
  lstRestoreGamesList.FillSaveGameList(); // This populates the list.
  mouse.UseModeGraphic(eModePointer);
  gIconbar.Visible = false;
}

function close_save_game_dialog()
{
  gSaveGame.Visible = false;
  mouse.UseDefaultGraphic();
  gIconbar.Visible = true;
}

function close_restore_game_dialog()
{
  gRestoreGame.Visible = false;
  mouse.UseDefaultGraphic();
  gIconbar.Visible = true;
}


Put this code above your save/load _onClick, then call the functions bellow.
So your button_OnClick would look like this:

Code (ags) Select

function btnConfirmSave_OnClick(GUIControl *control, MouseButton button)
{
  show_save_game_dialog();
}

function btnLoadGame_OnClick(GUIControl *control, MouseButton button)
{
  show_restore_game_dialog();
}


Alternatively, you can try and add the lines .FillSaveGameList(); to your code and see if it solves the problem.  :)
Title: Re: Custom Save/Load Button Problem
Post by: Totora Games on Sun 21/06/2020 04:52:24
Quote from: Cassiebsg on Sat 20/06/2020 22:34:10
This is the code I have in an older game of mine, which is probably just copy/paste from the Sierra template (the only template that uses this kind of save/load menus).



Code (ags) Select

function show_save_game_dialog()
{
  gSaveGame.Visible = true;
  // Get the list of save games
  lstSaveGamesList.FillSaveGameList(); /// Note. I think this is the code you're missing, it populates the list
  if (lstSaveGamesList.ItemCount > 0)
  {
    // If there is at least one, set the default text
    // to be the first game's name
    txtNewSaveName.Text = lstSaveGamesList.Items[0];
  }
  else
  {
    // No save games yet, default empty text.
    txtNewSaveName.Text = "";
  }
  mouse.UseModeGraphic(eModePointer);
  gIconbar.Visible = false;
}

function show_restore_game_dialog()
{
  gRestoreGame.Visible = true;
  lstRestoreGamesList.FillSaveGameList(); // This populates the list.
  mouse.UseModeGraphic(eModePointer);
  gIconbar.Visible = false;
}

function close_save_game_dialog()
{
  gSaveGame.Visible = false;
  mouse.UseDefaultGraphic();
  gIconbar.Visible = true;
}

function close_restore_game_dialog()
{
  gRestoreGame.Visible = false;
  mouse.UseDefaultGraphic();
  gIconbar.Visible = true;
}


Put this code above your save/load _onClick, then call the functions bellow.
So your button_OnClick would look like this:

Code (ags) Select

function btnConfirmSave_OnClick(GUIControl *control, MouseButton button)
{
  show_save_game_dialog();
}

function btnLoadGame_OnClick(GUIControl *control, MouseButton button)
{
  show_restore_game_dialog();
}


Alternatively, you can try and add the lines .FillSaveGameList(); to your code and see if it solves the problem.  :)

It didn't work either. I really don't know what's happening, it's very strange, and of course, extremely frustrating.

I don't have any "gIconbar", maybe it has something to do with it, but I'm just guessing right now...
Title: Re: Custom Save/Load Button Problem
Post by: Crimson Wizard on Sun 21/06/2020 05:12:49
The reason why you did not see any saved games in the list box was probably because you do not call FillSaveGameList() function for your ListBox
If using your previous code you posted:
Code (ags) Select

function Button4_OnClick(GUIControl *control, MouseButton button)
{
    lstSaveGames.FillSaveGameList();
    gLoadGame.Visible = true;
}


Above assumes that gLoadGame is one GUI for both save and load, but if it's not then some adjustment has to be made.



Quote from: Totora Games on Sun 21/06/2020 04:52:24It didn't work either. I really don't know what's happening, it's very strange, and of course, extremely frustrating.

I don't have any "gIconbar", maybe it has something to do with it, but I'm just guessing right now...

Yes of course, if you do not have GUI of that name then that new script will not compile. You may fix this by removing unnecessary lines with gInconbar.

EDIT: comparing the code further, the code Cassiebsg posted has gRestoreGame, while your GUI is called gLoadGame.

Also, more importantly, her code is based on Sierra template that uses several cursors, this is why there's mouse.UseDefaultGraphic() everywhere. Which means it may not be applicable to your game as-is at all...
TBH I'd rather propose returning to your old code and making it work instead, but it's up to you.
Title: Re: Custom Save/Load Button Problem
Post by: Cassiebsg on Sun 21/06/2020 22:03:15
Yeah, like I said my code was taken from the sierra, you need to adapt it to what you have.

Also, I  did point out that you were most likely missing the line to populate the list: .FillSaveGameList(); Meaning, check how to use it in the manual so you can use and adapt it to your game.
Title: Re: Custom Save/Load Button Problem
Post by: Totora Games on Mon 22/06/2020 02:02:11
I decided to start over again with the  scripting for the Save/Load Buttons. I think that I made a mistake trying to create too many GUI's; what I mean by that is that I created a GUI (a Menu) to put the Save/Load buttons, and also two more GUI's, one for each button (sort of a Save Game Screen and a Load Game Screen). This time, I got rid of those GUI's and I tried only with the Menu GUI and the Save/Load Buttons. And it worked! But...now, when I try to save/load a file, this happens:

https://drive.google.com/file/d/1_V26xha1YpGQ2ePjPr1eh-7hggtnkCqz/view?usp=sharing
https://drive.google.com/file/d/1fvYVeHUfx7DEpsr7QHgX7YxWTx8mATBs/view?usp=sharing

I assume that those saved files were created in all my tries, but I don't know how to erase it so that the ListBox appears empty the first time you enter the game.

Thank you all for your support, you're helping me A LOT!
Title: Re: Custom Save/Load Button Problem
Post by: Crimson Wizard on Mon 22/06/2020 02:25:27
Quote from: Totora Games on Mon 22/06/2020 02:02:11
I assume that those saved files were created in all my tries, but I don't know how to erase it so that the ListBox appears empty the first time you enter the game.

You may add Delete Save button and call DeleteSaveSlot() on click to let user delete existing saves.

I don't think it's a good idea to automatically delete all existing files without asking, because player could have transferred them from another location for example.

And of course you may just go into %USERPROFILE%\Saved Games\<your game name> folder in Windows Explorer and delete these by hand.
Title: Re: Custom Save/Load Button Problem
Post by: Totora Games on Mon 22/06/2020 05:09:12
Quote from: Crimson Wizard on Mon 22/06/2020 02:25:27
Quote from: Totora Games on Mon 22/06/2020 02:02:11
I assume that those saved files were created in all my tries, but I don't know how to erase it so that the ListBox appears empty the first time you enter the game.

You may add Delete Save button and call DeleteSaveSlot() on click to let user delete existing saves.

I don't think it's a good idea to automatically delete all existing files without asking, because player could have transferred them from another location for example.

And of course you may just go into %USERPROFILE%\Saved Games\<your game name> folder in Windows Explorer and delete these by hand.

Great, that finally worked and now I can save/load games! The only thing is that I really don't know how to add a Delete Save Button, due to the fact I'm using the SaveGameDialog.