Need assistance with checking if save text box is equal to an item in a list box

Started by Gal Shemesh, Sun 02/07/2023 18:34:51

Previous topic - Next topic

Gal Shemesh

Hi everyone,

I viewed the other threads about something similar, but found the information there a little overwhelming. I'm pretty sure there is a simple solution to what I'm after and would appreciate some assistance.

I copied the code of the Save Game panel from the Sierra-style template and I'm trying to make it a little better:

1. I've added a message if players click on Save while the text box is empty.

2. I tweaked the 'else if' statement before it actually save so it will check if the lstSaveGamesList.SelectedIndex == -1, to make sure that there is no save slot selected in the list box. If there is, it goes to the final 'else' statement where it shows an overwrite confirmation GUI. The problem is that if the players write a game save name which already exists in the list box, it overwrites it no matter what. I'm willing to make it show the overwrite confirmation GUI if the text box is equal to any of the save slots in the list box, but can't figure out how to compare the text... Here's my code:

Code: ags
function btnSaveGame_OnClick(GUIControl *control, MouseButton button)
{
  int gameSlotToSaveInto = find_save_slot(txtNewSaveName.Text);
  if (gameSlotToSaveInto < 0)
  {
    Display("No more free save slots!");
  }
  // game save cannot be empty message
  else if (txtNewSaveName.Text == "")
  {
  Display("Game save cannot be empty!");
  }
  // check if no save slot was clicked on in the saves list box
  else if (lstSaveGamesList.SelectedIndex == -1)
  {
    SaveGameSlot(gameSlotToSaveInto, txtNewSaveName.Text);
    close_owning_gui(control);
  }
  // show the confirm overwriting dialogue
  else
  {
    open_gui(gOverwriteSave);
  }
}

Thanks
Gal Shemesh,
goldeng

Khris

You can use this:

Code: ags
bool ItemExists(this ListBox*, String item) {
  for (int i = 0; i < this.ItemCount; i++) {
    if (item == this.Items[i]) return true;
  }
  return false;
}

Now you can use this:
Code: ags
  else if (lstSaveGamesList.ItemExists(txtNewSaveName.Text)) ...

edit: fixed code

Gal Shemesh

Thanks @Khris for the prompt response. Much appreciated!

I'm trying to figuring out where to put this bool you mentioned - when I'm trying to put it within the function it gives me 'the nested function is not supported' error as if I missed a closing brace, but if I add one to the end of the bool code it ends my whole 'btnSaveGame_OnClick' function, leaving the rest of my code out of it. Do I need to replace the bool with one of my functions, or have it in a different place in my code?

Sorry, I'm trying my best but coding never was my strength...  :~( Hope to getting better.
Gal Shemesh,
goldeng

Khris

It's a separate function (returning a bool, so it says "bool" instead of "function" at the start). Just put it at the top of the global script, outside of other functions.

You can also put it directly above your btnSaveGame_OnClick function.

Gal Shemesh

Thanks for the clarification, @Khris! Noted down that this is a function too, only with the name 'bool'! ;-D I'm still trying to figuring out why it's not working, though. It says 'varaible is required on the left of assignment i' on the 'for (i = 0; i < this.ItemCount; i++)' line...

Code: ags
// saving a game
bool ItemExists(this ListBox*, String item) {
  for (i = 0; i < this.ItemCount; i++) {
    if (item == this.Items[i]) return true;
  }
  return false;
}

function btnSaveGame_OnClick(GUIControl *control, MouseButton button)
{
  int gameSlotToSaveInto = find_save_slot(txtNewSaveName.Text);
  if (gameSlotToSaveInto < 0)
  {
    Display("No more free save slots!");
  }
  // game save cannot be empty message
  else if (txtNewSaveName.Text == "")
  {
  Display("Game save cannot be empty!");
  }
  // check if no save slot was clicked on in the saves list box
  else if (lstSaveGamesList.SelectedIndex == -1)
  {
    SaveGameSlot(gameSlotToSaveInto, txtNewSaveName.Text);
    close_owning_gui(control);
  }
  // show the confirm overwriting dialogue
  else if (lstSaveGamesList.ItemExists(txtNewSaveName.Text))
  {
    open_gui(gOverwriteSave);
  }
}
Gal Shemesh,
goldeng

Khris


Gal Shemesh

@Khris, my man - what would I do without you! It works fantastic! Thank you so much!  ;-D  ;-D  ;-D
Gal Shemesh,
goldeng

SMF spam blocked by CleanTalk