Jibble

Author Topic: [SOLVED] "Overwrite Save Slot" Function Trouble  (Read 432 times)

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: Adventure Game Studio
  1. bool slot_selected = false;    //for signaling whether an existing save slot is selected; no selection by default
  2.  
  3. function lstSaveList_OnSelectionChanged(GUIControl *control)    //selection of an existing save slot
  4. {
  5.   slot_selected = true;
  6. }
  7.  
  8. function bSaveConf_OnClick(GUIControl *control, MouseButton button)
  9. {
  10.   if (slot_selected == false)    //no existing save slot is selected
  11.   {
  12.     SaveGameSlot(lstSaveList.ItemCount ++, tbSaveName.Text);    //save to the next slot up from the existing total count
  13.     gSaveGame.Visible = false;    //close save GUI
  14.   }
  15.   else if (slot_selected == true)    //an esiting save slot is selected
  16.   {
  17.     SaveGameSlot(lstSaveList.SelectedIndex, tbSaveName.Text);    //save to the selected save slot, overwriting it (???)
  18.     gSaveGame.Visible = false;    //close save GUI
  19.   }
  20. }
« Last Edit: 29 Oct 2021, 04:16 by imagazzell »

Re: "Save Slot Overwrite" Function Trouble
« Reply #1 on: 08 Oct 2021, 07:42 »
Something like
Code: Adventure Game Studio
  1.   Display("selected index: %d", lstSaveList.SelectedIndex);
should help you debug this.

Re: "Save Slot Overwrite" Function Trouble
« Reply #2 on: 08 Oct 2021, 08:07 »
Something like
Code: Adventure Game Studio
  1.   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. ???

Re: "Save Slot Overwrite" Function Trouble
« Reply #3 on: 08 Oct 2021, 08:14 »
How and when are you (re-)populating the list of save games?

Gilbert

  • Local Moderator
  • * KILL* * KILL * * KILL *
    • Lifetime Achievement Award Winner
Re: "Save Slot Overwrite" Function Trouble
« Reply #4 on: 08 Oct 2021, 10:44 »
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?
« Last Edit: 08 Oct 2021, 10:48 by Gilbert »

Re: "Save Slot Overwrite" Function Trouble
« Reply #5 on: 08 Oct 2021, 14:19 »
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: Adventure Game Studio
  1. int a = 10;
  2. int b = a++;
  3.  
^ here b will become 10, and then a will increase by 1.
« Last Edit: 08 Oct 2021, 14:21 by Crimson Wizard »

Re: "Save Slot Overwrite" Function Trouble
« Reply #6 on: 09 Oct 2021, 03:23 »
Thank you guys for chiming in.

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: Adventure Game Studio
  1. function bSave_OnClick(GUIControl *control, MouseButton button)
  2. {
  3.   lstSaveList.FillSaveGameList();    //call save game list
  4.   lstSaveList.SelectedIndex = -1;    //no selection in list
  5.   gSaveGame.Visible = true;    //open save game GUI
  6. }

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.

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.

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.

Re: "Overwrite Save Slot" Function Trouble
« Reply #7 on: 09 Oct 2021, 08:58 »
My guess is that
Code: Adventure Game Studio
  1.   SaveGameSlot(lstSaveList.SelectedIndex, tbSaveName.Text);

needs to be

Code: Adventure Game Studio
  1.   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()

Re: "Overwrite Save Slot" Function Trouble
« Reply #8 on: 09 Oct 2021, 20:01 »
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 ;)