Importing Save/Load into a blank game template (SOLVED)

Started by Mandle, Fri 13/12/2013 10:38:47

Previous topic - Next topic

Mandle

Well...I was stupid...

I didn't need any regular inventory etc. for my game and I chose the blank game from the start-up...

Now I realize I need the Save/Load and it's not in the blank game.

My question is: Is there any super-simple way to import the Save/Load into my game or do I have to figure it out piece by piece?

Cheers for your time!

Crimson Wizard

#1
Super-simple one-save-slot way: Ctrl+S to save, Ctrl+L to load:
Code: ags

function on_key_press(int keycode)
{
   if (keycode == eKeyCtrlS)
      SaveGameSlot(1, "my save");
   else if (keycode == eKeyCtrlL) {
      if (Game.GetSaveSlotDescription(1) != null)
         RestoreGameSlot(1);
      else
         Display("No saved game exists!");
   }
}


Super simple oldschool way: display default Sierra-style dialog (warning, 20 save slot limit):
Code: ags

function on_key_press(int keycode)
{
   if (keycode == eKeyCtrlS)
      SaveGameDialog();
   else if (keycode == eKeyCtrlL)
      RestoreGameDialog();
}


Next step: create menu and bind save/load with button clicks, e.g.:
Code: ags

function gSavegame_Click(GUIControl *control, MouseButton button)
{
   SaveGameDialog();
}




//-----------------------------------------------
EDIT: Sorry, I just realized you were asking of importing them from default template?
You can export any GUI from existing project by right-clicking on them in project tree and selecting "Export GUI". Import them in another project by right clicking on GUI folder and selecting "Import GUI".
Then find script functions that are bound to gui buttons in the first project and copy them to new one.

Basically, this is the script in "default" template that does save & load:
Spoiler

Display and hiding dialogs:
Code: ags

function show_save_game_dialog()
{
  gSaveGame.Visible = true;
  // Get the list of save games
  lstSaveGamesList.FillSaveGameList();
  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);
}

function show_restore_game_dialog()
{
  gRestoreGame.Visible = true;
  lstRestoreGamesList.FillSaveGameList();
  mouse.UseModeGraphic(eModePointer);
}

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

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


Handling dialog controls:
Code: ags


function btnCancelSave_OnClick(GUIControl *control, MouseButton button)
{
  close_save_game_dialog();
}

function btnSaveGame_OnClick(GUIControl *control, MouseButton button)
{
  int gameSlotToSaveInto = lstSaveGamesList.ItemCount + 1;
  int i = 0;
  while (i < lstSaveGamesList.ItemCount)
  {
    if (lstSaveGamesList.Items[i] == txtNewSaveName.Text)
    {
      gameSlotToSaveInto = lstSaveGamesList.SaveGameSlots[i];
    }
    i++;
  }
  SaveGameSlot(gameSlotToSaveInto, txtNewSaveName.Text);
  close_save_game_dialog();
}

function btnCancelRestore_OnClick(GUIControl *control, MouseButton button)
{
  close_restore_game_dialog();
}

function btnRestoreGame_OnClick(GUIControl *control, MouseButton button)
{
  if (lstRestoreGamesList.SelectedIndex >= 0)
  {
    RestoreGameSlot(lstRestoreGamesList.SaveGameSlots[lstRestoreGamesList.SelectedIndex]);
  }
  close_restore_game_dialog();
}

function lstSaveGamesList_OnSelectionCh(GUIControl *control)
{
  txtNewSaveName.Text = lstSaveGamesList.Items[lstSaveGamesList.SelectedIndex];
}

function txtNewSaveName_OnActivate(GUIControl *control)
{
  // Pressing return in the text box simulates clicking the Save button
  btnSaveGame_OnClick(control, eMouseLeft);
}

function btnDeleteSave_OnClick(GUIControl *control, MouseButton button)
{
  if (lstSaveGamesList.SelectedIndex >= 0)
  {
    DeleteSaveSlot(lstSaveGamesList.SaveGameSlots[lstSaveGamesList.SelectedIndex]);
    lstSaveGamesList.FillSaveGameList();
  }
}


Binding dialogs to key presses:
Code: ags

function on_key_press(int keycode)
{
   if (keycode == eKeyF5) show_save_game_dialog(); 
   if (keycode == eKeyF7) show_restore_game_dialog();
}

[close]

Of course, you may have to alter some things there to accomodate it in your project.

Mandle

OMG Crimson Wizard...You truly are the wizard I have suspected from reading your other helper posts.

Trying it out!

Mandle

Thank you soooo much Crimson Wizard!

It's working perfectly now!

rongel

Sorry to bring back an old thread but my question is 100% related to this!

I'm using this script (the bigger at the end of the post) to build my own save/load GUI. Everything works, it's a great help. But I would like to add a warning when the player is saving on top of old save and overwriting it. Something like: Are you sure you want to overwite the existing game? Is there a easy way to add this warning to it? I tried to find pre-made templates also but the links didn't work...
Dreams in the Witch House on Steam & GOG

Khris

If you look at lines 13 to 16 of the second snippet, that's where the script changes gameSlotToSaveInto to an existing slot.

You could try something like this:

Code: ags
int gameSlotToSaveInto;

function btnSaveGame_OnClick(GUIControl *control, MouseButton button)
{
  gameSlotToSaveInto = lstSaveGamesList.ItemCount + 1;
  overwriteSlot = false;
  int i = 0;
  while (i < lstSaveGamesList.ItemCount)
  {
    if (lstSaveGamesList.Items[i] == txtNewSaveName.Text)
    {
      gameSlotToSaveInto = lstSaveGamesList.SaveGameSlots[i];
    }
    i++;
  }
  // show confirmation
  if (gameSlotToSaveInto != lstSaveGamesList.ItemCount + 1) {
    gOverwriteSlot.Visible = true; // confirmation GUI is named gOverwriteSlot
    return;
  }
  // otherwise proceed as usual
  SaveGameSlot(gameSlotToSaveInto, txtNewSaveName.Text);
  close_save_game_dialog();
}

// these functions just illustrate what the content of the two button handlers is,
// don't just paste them without linking them!
function btnOverwriteSaveGameYes_OnClick(GUIControl *control, MouseButton button)
{
  gOverwriteSlot.Visible = false;
  SaveGameSlot(gameSlotToSaveInto, txtNewSaveName.Text);
  close_save_game_dialog();
}

function btnOverwriteSaveGameNo_OnClick(GUIControl *control, MouseButton button)
{
  gOverwriteSlot.Visible = false;
}

rongel

Thanks Khris, tested it just now and it seems to work!

It's a small thing, but it somehow makes the game seem more professional :grin:
Dreams in the Witch House on Steam & GOG

SMF spam blocked by CleanTalk