I my game I have a 2 buttons were one saves & the other loads. When you click save it saves because I check the save game folder. But when you click the load button it it doesn't load anything.
function Button39_OnClick(GUIControl *control, MouseButton button)
{
Display("Data loaded");
if (lstRestoreGamesList.SelectedIndex >= 0)
{
RestoreGameSlot(lstRestoreGamesList.SaveGameSlots[lstRestoreGamesList.SelectedIndex]);
}
}
I am pretty sure I am missing something but I don't know.
function Button38_OnClick(GUIControl *control, MouseButton button)
{
Display("Data saved");
txtNewSaveName.Text = "PMQsave";
int gameSlotToSaveInto = lstSaveGamesList.ItemCount + 1;
int i = 0;
while (i < lstSaveGamesList.ItemCount)
{
if (lstSaveGamesList.Items[i] == txtNewSaveName.Text)
{
gameSlotToSaveInto = lstSaveGamesList.SaveGameSlots[i];
}
i++;
}
SaveGameSlot(gameSlotToSaveInto, txtNewSaveName.Text);
}
I don't think the save button is what's causing it. But I feel I should post what I have inside that button.
I don't know what the problem is but try this:
function Button39_OnClick(GUIControl *control, MouseButton button)
{
if (lstRestoreGamesList.SelectedIndex >= 0)
{
Display("Data loaded"); //Put this here instead
RestoreGameSlot(lstRestoreGamesList.SaveGameSlots[lstRestoreGamesList.SelectedIndex]);
}
}
If it doesn't display "data loaded" you know the problem is with the line above
if (lstRestoreGamesList.SelectedIndex >= 0)
And you can do the same kind of thing in other places to track down the problem.
As a side note,
if (lstRestoreGamesList.SelectedIndex >= 0)
{
RestoreGameSlot(lstRestoreGamesList.SaveGameSlots[lstRestoreGamesList.SelectedIndex]);
}
can simply become
if (lstRestoreGamesList.SelectedIndex >= 0) RestoreGameSlot(lstRestoreGamesList.SaveGameSlots[lstRestoreGamesList.SelectedIndex]);
because you don't need the brackets if there's just one line after an if statement.
Also, if you create a new game and choose the template, there's save/load code set up for you to use or copy into your own game.
Your saving function is strange.
function Button38_OnClick(GUIControl *control, MouseButton button)
{
if(lstSaveGamesList.SelectedIndex >= 0)
{
SaveGameSlot(lstSaveGamesList.SaveGameSlots[lstSaveGamesList.SelectedIndex], txtNewSaveName.Text);
Display("Data Saved");
}
}
Your Load function isn't going to work either:
function Button39_OnClick(GUIControl *control, MouseButton button)
{
if(lstSaveGamesList.SelectedIndex >=0)
{
RestoreGameSlot(lstSaveGamesList.SaveGameSlots[lstSaveGamesList.SelectedIndex]);
}
}
This should be more simple, also when you load items into a listbox, the first item has index : 0. so if you saved at Index +1, then make sure you're loading the savegame at Index +1 as well, but you could save on slot 0.
Btw, I see you indented your code this time, and it's really easier to read, keep your code always clean like that.
@Atelier: Thanks for the info. I did however use the save/load functions from the original template.
@Sephiroth: So the problem is that I am loading from index 1. I didn't notice that, Thanks.
Not really, the problems are:
1- Index +1
2- While loop is totally unecessary and dangerous.
3- RestoreGameSlot(int slot), not a string
4- ItemCount + 1, should be SelectedIndex +1
and so on, thats why I re-wrote the code so you can have a clean save/load option, it's up to you to use it, but at least you have a proper example.
I used you code.
I get this error message?
GlobalScript.asc(992): Error (line 992): Type mismatch: cannot convert 'int' to 'const string'
The error is at you "if" statement for the load function.
Ah yes, sorry, the SaveGameSlot[] is not a regular ListBox.
Edit: The code will work with a regular listbox if you change "SaveGameSlot" with "Items", like ListBox1.Items[ListBox1.SelectedIndex]
I think SaveGameSlot[] is an array of int (save slots) instead of an array of strings like listboxes. I edited the code in previous post, does it work?
There were a few small errors but I fixed them. However there is now one I don't know how to fix.
GlobalScript.asc(1006): Error (line 1006): undefined symbol 'index'
I changed the code, should work now, I'm not sure what txtNewSaveName is, but you could replace it with any Label.Text or directly with "PMQSave".
Oh I didn't notice you responded. The game runs but now it doesn't say it saved and it also doesn't load. :-\
Have you used ListBox.FillSaveGameList? If not then the listbox is empty and the index is -1 so the code won't run. It should be used whenever you show the savegame dialog. Otherwise I would advise to read the manual or directly copy the game template code...