Creating Custom Save and Load Dialogs

From Adventure Game Studio | Wiki
Jump to navigation Jump to search

Introduction

Creating custom save and load dialogs is a necessity when it comes to making dialogs and GUIs that follow some sort of constant style. Chances are that the default save/load dialogs do not match the other GUIs, so creating new ones would help the game feel more professional.

Here, I will (hopefully) make custom save/load dialogs a fairly trivial matter. This guide will step you through the creation of the save GUI...translating this to a load GUI will be discussed at the end.

It is very likely that, beyond the core coding found here, you will need to add new code specific to your game for closing other GUIs, like an option menu.


Create the GUI

Here is an example of a custom save GUI.

SaveGUI.png

In the above image, the elements used are three buttons, a text box, and a GUI list box. The GUI list box is added using the circled button. Resize and reposition these elements as needed.

Give Elements Script Names

When you click on an element of the GUI, the "Script name" property is the first one in the properties window. Give all elements of the GUI appropriate names.

In this example, I used:

Save GUI: gSavegame (when entering this, don't put the "g" in; that's added automatically)

Save Button: btnConfirmSave

Cancel Button: btnCancelSave

Delete Button: btnDeleteSave

Text Box: txtSaveName

GUI List: lstSaveGames


Script the Cancel Button

Now comes the scripting, but we'll start with the easy part. Double click the "Cancel" button and enter the following:

 //Turn off the save dialog
 gSavegame.Visible = false;

Script the Save Button

Double click the "Save" button and enter the following code:

 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("Please enter a name for your saved game.");
   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("The maximum number of saved games has been reached; overwrite or delete some old ones.");
   }
 }

This code first checks that the text box is not empty. It then checks whether the save name in the text box is already being used. If so, it overwrites. If not, a new save is created, if there is enough room - AGS is currently limited to 20 save slots. Immediately before saving, the "Save" GUI is closed, so the player can keep playing.

It is important that anything you want to do when saving is placed before/above the "SaveGameSlot()" commands. Anything below "SaveGameSlot()" will be executed, but NOT saved into the file, because it occurs after the save command.

Script the Delete Button

Double click on the "Delete" button and add the following code:

 if(lstSaveGames.SelectedIndex != -1)
 {
   DeleteSaveSlot(savegameindex[lstSaveGames.SelectedIndex]);
   //Save Dialog Prep
   lstSaveGames.FillSaveGameList();
 }
 else
 {
   Display("Please select a saved game to delete.");
 }

This simply checks that a save game is highlighted and then deletes it, without question. It would be wise to put in a Yes/No verification for deleting games to avoid accidents, but this is the standard structure for deletion.


Script the List Box

There is a small amount of work to be done on the list box. Double click it and add the following code:

  //put the selection in the save txt box
  txtSaveName.Text = lstSaveGames.Items[lstSaveGames.SelectedIndex];

This code simply puts the selected game's name in the text box. Without this, the save button code could produce strange results.

The final result should be a working save dialog. You can test it with the default Load Dialog.


Applying To a Load GUI

Applying this to a "load" GUI would be fairly easy. Simply create a nearly identical GUI to the "Save" one and, in the element script names, replace all instances of the word "Save" with "Load". Do the same with the "Cancel", "Delete", and "List Box" code, and those will be ready. For the text box element, it's pretty handy to call this when the load GUI is loaded:

 txtLoadName.Clickable = false;

This causes the text box to be uneditable, which ends up feeling a bit more polished.

The only real difference would be the "Load" button, which you would change to this:

 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;
 }

This leaves you with a set of save/load dialogues that can be customized to your needs.

Putting Dialogs in the Game

To use these dialogs, simply find any instances of the following two functions:

 SaveGameDialog();
 RestoreGameDialog();

Replace the "SaveGameDialog();" code with the following:

 //Prep the GUI
 lstSaveGames.FillSaveGameList();
 lstSaveGames.SelectedIndex = 0;
 if(lstSaveGames.SelectedIndex != -1)
 {
   txtSaveName.Text = lstSaveGames.Items[lstSaveGames.SelectedIndex];
 }
 //Activate the save GUI
 gSavegame.Visible = true;

Replace the "RestoreGameDialog()" with the following:

 //Prep the GUI
 lstLoadGames.FillSaveGameList();
 lstLoadGames.SelectedIndex = 0;
 //Activate the load GUI
 gLoadgame.Visible = true;

This will cause the save/load buttons to display your custom GUIs, rather than the default ones, as well as fill the save game list so everything displays properly.

Known Bugs

There are currently no known bugs. If you find any, please add them here.


Thanks for reading,

Clark Kromenaker

CMK2901@gmail.com