Game crashing on custom load GUI if no save games exist *SOLVED*

Started by Divon, Wed 04/01/2017 22:05:23

Previous topic - Next topic

Divon

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

  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.

Cassiebsg

There are those who believe that life here began out there...

Snarky

Code: ags

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

Divon

awesome -- thanks guys, it worked like a charm.  I really appreciate it :)

SMF spam blocked by CleanTalk