Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Construed on Sat 05/10/2013 20:19:51

Title: Custom quit function
Post by: Construed on Sat 05/10/2013 20:19:51
Hello again genius coders of ags,
I present to you another one of my questions about the mysteries of the world.

Code (ags) Select


function btnQuit_OnClick(GUIControl *control, MouseButton button)
{
   txtNewSaveName.Text = name;  ///make the save game = the players name.

  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();
  QuitGame(1);
}



What I'm trying to do here is create a custom "Quit and save" function that saves the game as the players name and quits the game, Instead of having to do all that save/name/restore stuff you can just restore your character at the beginning of the game and each new player name needs its own slot.
I had this figured out at one point in the past, but it has become confusing to me and I would greatly appreciate it if someone could help figure this out.

Thanks for reading and have a blessed day :D

-Jared

Title: Re: Custom quit function
Post by: monkey0506 on Sat 05/10/2013 20:57:15
What is the problem? I don't seem to be able to find your question.

Some of your code is perhaps a bit superfluous, but aside from that...I don't see any reason why it shouldn't actually function.
Title: Re: Custom quit function
Post by: Construed on Sat 05/10/2013 21:46:44
For some reason when you make a new player name it saves over the previous one.
Title: Re: Custom quit function
Post by: Khris on Sat 05/10/2013 23:57:10
It looks like you aren't updating the ListBox items.

Add
Code (ags) Select
  lstSaveGamesList.FillSaveGameList();
at the start of the function.

(Saving the game does not automatically add an entry to the listbox. However, the code uses the listbox contents to determine the slot.)

You should probably also go ahead and remove the " + 1" from this:
Code (ags) Select
int gameSlotToSaveInto = lstSaveGamesList.ItemCount + 1;
Indexing starts at 0. So if ItemCount is 5, the items have indexes 0, 1, 2, 3 and 4. The next free one is thus 5, not 5 + 1.
Title: Re: Custom quit function
Post by: monkey0506 on Sun 06/10/2013 08:58:01
Quote from: Khris on Sat 05/10/2013 23:57:10You should probably also go ahead and remove the " + 1" from this...

+1'd! Heh, nice catch. Missed that one. ;)
Title: Re: Custom quit function
Post by: Construed on Sun 06/10/2013 21:07:15
First off my apologies for starting a new thread on this, I found this: http://www.adventuregamestudio.co.uk/forums/index.php?topic=45395.msg609314#msg609314 Where Khris had helped me figure it out.

However now using this code:
Code (ags) Select


///////Quit button gGui12, I had to make an extra gui so that the
//savegame could have time to process because QuitGame(0) causes the game to quit before saving otherwise.
function Button51_OnClick(GUIControl *control, MouseButton button)
{
gGui12.Visible=false;
gSaveGame.Visible=false;
QuitGame(0);
}
/////savegame function
function btnSaveGame_OnClick(GUIControl *control, MouseButton button)
{
  txtNewSaveName.Text = nick;
  int gameSlotToSaveInto = lstSaveGamesList.ItemCount;
  int i = 0;
  while (i < lstSaveGamesList.ItemCount)
  {
    if (lstSaveGamesList.Items[0] == txtNewSaveName.Text)
    {
      gameSlotToSaveInto = lstSaveGamesList.SaveGameSlots[0];
    }
    i++;
  }
  SaveGameSlot(gameSlotToSaveInto, txtNewSaveName.Text);
  close_save_game_dialog();
}
//////other part of save game function
function btnIconSave_OnClick(GUIControl *control, MouseButton button)
{
  int gameSlotToSaveInto = lstSaveGamesList.ItemCount;
 
  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
  {
  txtNewSaveName.Text = "";
  }
  mouse.UseModeGraphic(eModePointer);
  gIconbar.Visible = false;

}
//////Quit button set to execute entire save function automatically.
function btnQuit_OnClick(GUIControl *control, MouseButton button)
{
btnSaveGame_OnClick(btnSaveGame, eMouseLeft);
btnIconSave_OnClick(btnIconSave, eMouseLeft);
gGui12.Visible=true;

}

///////Button on gGui12 to continue playing instead of quitting.
function Button52_OnClick(GUIControl *control, MouseButton button)
{
gGui12.Visible=false;
}


I still modified the +1 as recommended however.
I am having the problem that when the user uses his same name it saves the same exact name twice or as many times as you try to save the same name, until the list is all a bunch of the same names.

I'm sorry if the solution has already been offered and I missed it, but with this colossal code change it's easy for me to get confused.

Thanks a mill guys!


Title: Re: Custom quit function
Post by: monkey0506 on Sun 06/10/2013 21:14:56
I don't understand why you're setting the TextBox text to anything other than the current player's name if when it saves it ONLY is meant to use the current name.

Also, why did you change this loop?:

Code (ags) Select
  while (i < lstSaveGamesList.ItemCount)
  {
    if (lstSaveGamesList.Items[0] == txtNewSaveName.Text)
    {
      gameSlotToSaveInto = lstSaveGamesList.SaveGameSlots[0];
    }
    i++;
  }


You do realize that this is only ever checking the first item in the list, right?? You're not even iterating the list, you're just doing the same comparison several times. I'm pretty sure that 0 should be an i:

Code (ags) Select
  while (i < lstSaveGamesList.ItemCount)
  {
    if (lstSaveGamesList.Items[i] == txtNewSaveName.Text)
    {
      gameSlotToSaveInto = lstSaveGamesList.SaveGameSlots[i];
    }
    i++;
  }
Title: Re: Custom quit function
Post by: Construed on Mon 07/10/2013 07:19:29
Ah, Thanks a mill monkey, That fixed er up.