[SOLVED] "Overwrite Save Slot" Function Trouble

Started by imagazelle, 08 Oct 2021, 07:27

« previous - next »

imagazelle

Hey, guys. I'm trying to script (what I thought would be) a simple "save slot overwrite" function in a custom save GUI, but all I'm getting is overwriting of the slot at the top of the save list, no matter which other slot I have selected. Can you please take a look and tell me what I've gotten wrong here?

Code: ags

bool slot_selected = false;    //for signaling whether an existing save slot is selected; no selection by default

function lstSaveList_OnSelectionChanged(GUIControl *control)    //selection of an existing save slot
{
  slot_selected = true;
}

function bSaveConf_OnClick(GUIControl *control, MouseButton button)
{
  if (slot_selected == false)    //no existing save slot is selected
  {
    SaveGameSlot(lstSaveList.ItemCount ++, tbSaveName.Text);    //save to the next slot up from the existing total count
    gSaveGame.Visible = false;    //close save GUI
  }
  else if (slot_selected == true)    //an esiting save slot is selected
  {
    SaveGameSlot(lstSaveList.SelectedIndex, tbSaveName.Text);    //save to the selected save slot, overwriting it (???)
    gSaveGame.Visible = false;    //close save GUI
  }
}

Khris

Something like
Code: ags
  Display("selected index: %d", lstSaveList.SelectedIndex);

should help you debug this.

imagazelle

Quote from: Khris on 08 Oct 2021, 07:42
Something like
Code: ags
  Display("selected index: %d", lstSaveList.SelectedIndex);

should help you debug this.

Hi, Khris. Thanks for the suggestion. I did already check with that, however, and it did appear that things were working correctly (if I selected slot 3, it would display a 3; if slot 8, an 8), yet after I saved and went back to the list, only the top slot, not the one I had selected, would be overwritten. ???

Khris

How and when are you (re-)populating the list of save games?

Gilbert

#4
I haven't thoroughly read the codes (and that I haven't done AGS related stuff for a while so I may not be that useful here), but these are my observations.

  • I guess that slot_selected somehow is never set to true, so the false path is always chosen in the if-else if group. (So apart from just checking only the selected index in a Display() you may also check this bool variable.)
  • The ItemCount property of a listbos is readonly so you should not write something like "lstSaveList.ItemCount ++". Instead that related line should be SaveGameSlot(lstSaveList.ItemCount +1, tbSaveName.Text); in order to save to a new slot.
    When you repopulate the listbox the next time the ItemCount will be updated correspondingly.
If this is the case, though, it would also overwrite the same existing slot if you do not choose any slot in the list. Have you checked that too?

Crimson Wizard

#5
In regards to the lstSaveList.ItemCount++, I also must add. The way the ++ operator works, the value of the variable is used before ++ is applied.
For example:
Code: ags

int a = 10;
int b = a++;

^ here b will become 10, and then a will increase by 1.

imagazelle

Thank you guys for chiming in.

Quote from: Khris on 08 Oct 2021, 08:14
How and when are you (re-)populating the list of save games?
I have a function that calls it when the save GUI is opened from my main options GUI:
Code: ags

function bSave_OnClick(GUIControl *control, MouseButton button)
{
  lstSaveList.FillSaveGameList();    //call save game list
  lstSaveList.SelectedIndex = -1;    //no selection in list
  gSaveGame.Visible = true;    //open save game GUI
}


Quote from: Gilbert on 08 Oct 2021, 10:44
1. I guess that slot_selected somehow is never set to true, so the false path is always chosen in the if-else if group. (So apart from just checking only the selected index in a Display() you may also check this bool variable.)
The function starting at line 3 of the script in my OP sets the bool to true when the player makes a selection in the list box, confirmed by a "1" display-check of the bool.

Quote from: Gilbert on 08 Oct 2021, 10:44
2. The ItemCount property of a listbos is readonly so you should not write something like "lstSaveList.ItemCount ++". Instead that related line should be SaveGameSlot(lstSaveList.ItemCount +1, tbSaveName.Text); in order to save to a new slot.
When you repopulate the listbox the next time the ItemCount will be updated correspondingly.
I had actually tried it both ways, to the same result, but I've set it back to "+1" in light of Crimson Wizard's note, which I had not gleaned from the part in the scripting tutorial about the "++" shortcut.

Quote from: Gilbert on 08 Oct 2021, 10:44
If this is the case, though, it would also overwrite the same existing slot if you do not choose any slot in the list. Have you checked that too?
With nothing in the list selected, the save gets saved to a new slot, as expected. It's only when an existing slot (any slot) is selected that the top slot (and only the top slot) gets overwritten.

Khris

My guess is that
Code: ags
  SaveGameSlot(lstSaveList.SelectedIndex, tbSaveName.Text);


needs to be

Code: ags
  SaveGameSlot(lstSaveList.SaveGameSlots[lstSaveList.SelectedIndex], tbSaveName.Text);


The list items and the save game slots use different indexes, that's why you need to "convert" the list index to the save game index first.
This is explained in the manual entry for  ListBox.FillSaveGameList()

imagazelle

That appears to have done the trick, Khris! Thanks so much. I don't think I would have figured out that conversion necessity on my own, even having read the manual entry. Enjoy your coffee. I'm sure there will be plenty more where that came from ;)

SMF spam blocked by CleanTalk