I know there has been a lot of questions about custom save/load GUIs and I went through every possible thread to try and resolve my problem but I'm not getting anywhere. I did manage to crash AGS as a result of my coding attemps on these GUIs :o but CJ is trying to sort that out.
In my save GUI I have the following logical issues:
1) If txtbox is blank - don't save and tell the user to first input text
- this works fine.
2) If you limit the number of SaveGameSlots to say 20 then you should inform the user when this limit is reached and give an option to delete or overwrite existing saved games (used Listbox.SelectedIndex for that)
- This part also works fine
3) If a saved game is selected from the listbox (set the txtbox to selected game i.e auto complete) and prompt the user with a "Are you sure you want to overwrite the existing game" on a OveriteYesNo GUI. If the response is Yes then overwrite the selected game.
- This part also works fine
4) If the text entered by the user is the same as the "name" of an existing saved game in the listbox. Compare the 2 strings and if they are identical overwrite the existing game.
No matter what I do I cannot get this to work and I end up with 2 saved games in the list with the same name.
The logical option is to use
String.CompareTo(string str2, optional bool caseSensitive) //not case sensitive
To do this I need 2 strings
The first: String GameReference = txtBoxSaved.Text;
The second: The array of game names ??????????????????????????
I'm sure points 3 and 4 can be combined into one
Can somebody please help with this.
Code for the save function
function SaveGame(){
int index = lstSaved.ItemCount;
if(index <= 10){ //Limit saved games to 10
String GameReference = txtboxSaved.Text;
if(txtboxSaved.Text == ("")){
Display("PlEASE ENTER A VALID NAME OR SELECT A GAME TO OVERWRITE!");
return;
}
else if(lstSaved.SelectedIndex !=-1){
gOverwriteyesno.Visible = true;
}
else{
SaveGameSlot(index+1, GameReference);
mouse.Mode = eModeWalkto;
gSave.Visible = false;
}
}
else {
Display("CAPACITY REACHED, SELECT A GAME TO OVERWRITE OR DELETE A GAME FROM THE LIST!");
}
}
Code for the OverwriteYesNo
#sectionstart btnOverwriteYes_Click // DO NOT EDIT OR REMOVE THIS LINE
function btnOverwriteYes_Click(GUIControl *control, MouseButton button) {
String overwrite = txtboxSaved.Text;
int SaveOver = lstSaved.SelectedIndex;
SaveGameSlot(savegameindex[SaveOver], overwrite);
Wait(5);
gOverwriteyesno.Visible = false;
gSave.Visible = false;
}
#sectionend btnOverwriteYes_Click // DO NOT EDIT OR REMOVE THIS LINE
Question 2
Another issue is to deselect something in a listbox.
If you click on the item it is highlighted and I want to deslect the item by clicking on it a second time i.e set lstSaved.SelectedIndex = -1;
Thanks!!
Try a simple while loop:
bool NameIsUnique = true;
int Temp;
while (Temp < lstSaved.ItemCount) {
// Compares TextBox text to each item in the ListBox (the names of the existing games)
if (txtboxSaved.Text == lstSaved.Items[Temp]) NameIsUnique = false;
Temp ++;
}
if (NameIsUnique == true) {
// No game with same name, save a new one
}
else {
// Existing game with same name
gOverwriteyesno.Visible = true;
}
To integrate with your existing code, what about (untested):
function SaveGame(){
int index = lstSaved.ItemCount;
if(index <= 10){ //Limit saved games to 10
String GameReference = txtboxSaved.Text;
if(txtboxSaved.Text == ("")){
// No name entered
Display("PLEASE ENTER A VALID NAME OR SELECT A GAME TO OVERWRITE!");
return; // Do you need this return? Nothing after this SHOULD run anyway.
}
else { // Name entered
bool NameIsUnique = true;
int Temp;
while (Temp < lstSaved.ItemCount) {
// Compares TextBox text to each item in the ListBox (the names of the existing games)
if (txtBoxSaved.Text == lstSaved.Items[Temp]) NameIsUnique = false;
Temp ++;
}
if ((lstSaved.SelectedIndex !=-1) || (NameIsUnique == false)) {
// Existing game selected OR name of existing game entered
gOverwriteyesno.Visible = true;
}
else {
SaveGameSlot(index+1, GameReference);
mouse.Mode = eModeWalkto;
gSave.Visible = false;
}
}
}
else {
Display("CAPACITY REACHED, SELECT A GAME TO OVERWRITE OR DELETE A GAME FROM THE LIST!");
}
}
Second issue:
Somewhere, there's code for Loading/Overwriting a savegame on second click. I can't find it right now, but it should be what you need here (except set the SelectedIndex instead of Save/Load, ovbiously:)) I think it was:
//At top of script (or at least before the next bit)
int PrevSelected = -1;
//Elsewhere
function lstSaved_SelectionChanged(GUIControl *control) {
// Or whatever the existing function is called, if there is one
if (PrevSelected == lstSaved.SelectedIndex) {
lstSaved.SelectedIndex = -1;
}
PrevSelected = lstSaved.SelectedIndex;
}
Again, untested, but I think that's right.
Thanks Ashen,
Can you please just fix these so people won't get confused by it if they use this thread.
change txtBoxSaved in the while loop to txtboxSaved (my bad for not using the proper naming conventions :P)
There is a brace missing before the last else.
I tested your code but now I get this error when I want to overwrite a game.
This only happens if the strings match (while loop) and not when you select an existing game to overwrite from the listbox.
An internal error has occured. Please note down the following information.
If the problem persists, contact Chris Jones.
(ACI version 2.72.920)
Error: run_text_script1: error -6 running function 'on_key_press':
Error: Array index out of bounds (index: 20, bounds: 0..19)
in Global script (line 679)
Line 679 refers to the Overwrite GUI's yes button
Did I have to make any changes to the code for this button (see first post)
Thanks again!
Please post error messages as plain text - makes it easier for people searching in the future.
OK, fixed those two problems in my code, thanks for spotting them. The error is generated because I was posting in a hurry, and didn't think things through. You'll need to make a change to the while loop in my code, to select the game that matches.
while (Temp < lstSaved.ItemCount) {
// Compares TextBox text to each item in the ListBox (the names of the existing games)
if (txtBoxSaved.Text == lstSaved.Items[Temp]) {
NameIsUnique = false;
lstSaved.SelectedIndex = Temp;
Temp = lstSaved.ItemCount;
}
Temp ++;
}
I also added a line to skip checking the rest of the game names when it finds a match. In fact, looking at it now, you could probably do without the NameIsUnique variable. Since the while loop now selects the relevant game, just checking lstSaved.SelectedIndex !=-1 should work.
Thanks!
Sorry about the graphic :-[ I just wasn't thinking that it may not be around for that long.
I tested your new code and it works great but while testing all the possibilities I encountered another problem.
I have a delete option on the save GUI (it may seem redundant as you can overwrite but if you have a lot of saved game to get rid of it makes it a lot easier than going into the game directory) and if you delete a game and try to use the same name directly after that (could happen when you delete all the games and the lsitbox is empty) the overwrite GUI pops up. I don't get it because ListBox.FillSaveGameList()
is called in the delete function again to populate the array.
YesNo Gui yes button code
#sectionstart btnYesGUI_Click // DO NOT EDIT OR REMOVE THIS LINE
function btnYesGUI_Click(GUIControl *control, MouseButton button) {
DeleteSaveSlot(savedgames);
lstLoadGame.FillSaveGameList();
lstSaved.FillSaveGameList();
gYesno.Visible = false;
txtboxSaved.Enabled = true;
txtboxSaved.Text = "";
gSave.Visible = true;
}
#sectionend btnYesGUI_Click // DO NOT EDIT OR REMOVE THIS LINE
How do I fix this?