Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Divon on Wed 04/01/2017 22:05:23

Title: Game crashing on custom load GUI if no save games exist *SOLVED*
Post by: Divon on Wed 04/01/2017 22:05:23
This is probably a silly question, but my brain is seriously not working here.  I have custom save and load GUI's for my game, and they work perfectly, except for one bug.

If I make a call to the load game GUI, and there are no save games currently created, the game crashes.  I know I can solve this with a simple 'if' statement, but I can't figure out how to phrase it.

Here is the code I use to call the GUI:
Code (ags) Select

  if (keycode == eKeyF7) //OPEN LOAD GAME GUI
  {
    lstLoadGames.FillSaveGameList();
    lstLoadGames.SelectedIndex = 0;
    lLoadName.Text = lstLoadGames.Items[lstLoadGames.SelectedIndex];
    gLoadgame.Visible = true;
  }


I know its crashing because the index is empty, but the only way I can think to fix it is by creating an auto-save when you start a new game, so you always have a save game.  That is obviously a band-aid fix.

I did do a search to see if I could find the same problem, but I couldn't find one specific to my exact error, which is "ListBox.Items: invalid index specified"

Thanks in advance, and yes, I know once I see the code I will slap myself for not realized how easy the fix is lol.
Title: Re: Game crashing on custom load GUI if no save games exist
Post by: Cassiebsg on Wed 04/01/2017 22:24:04
Hi... Some times you have to search using word just like you used on your topic...

http://www.adventuregamestudio.co.uk/forums/index.php?topic=24096.msg299216#msg299216
Title: Re: Game crashing on custom load GUI if no save games exist
Post by: Snarky on Wed 04/01/2017 22:34:41
Code (ags) Select

  if (keycode == eKeyF7) //OPEN LOAD GAME GUI
  {
    lstLoadGames.FillSaveGameList();
    if(lstLoadGames.ItemCount > 0)
    {
      lstLoadGames.SelectedIndex = 0;
      lLoadName.Text = lstLoadGames.Items[0]; // You just set it to 0, no need to look it up!
    }
    else
      lLoadName.Text = "";
    gLoadgame.Visible = true;
  }
Title: Re: Game crashing on custom load GUI if no save games exist
Post by: Divon on Thu 05/01/2017 03:10:56
awesome -- thanks guys, it worked like a charm.  I really appreciate it :)