one click load button not working/And one click inventory

Started by Icey, Sat 23/04/2011 08:10:49

Previous topic - Next topic

Icey

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.

Code: ags

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.
Code: ags

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.

Atelier

I don't know what the problem is but try this:

Code: ags

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

Code: ags

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,

Code: ags

 if (lstRestoreGamesList.SelectedIndex >= 0)
  {
    RestoreGameSlot(lstRestoreGamesList.SaveGameSlots[lstRestoreGamesList.SelectedIndex]);
  }


can simply become

Code: ags

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.

Sephiroth

#2
Your saving function is strange.

Code: ags

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:

Code: ags

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.

Icey

#3
@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.

Sephiroth

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.

Icey

#5
I used you code.
I get this error message?
Code: ags

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.

Sephiroth

#6
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?

Icey

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'

Sephiroth

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".

Icey

Oh I didn't notice you responded. The game runs but now it doesn't say it saved and it also doesn't load. :-\

Sephiroth

#10
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...

SMF spam blocked by CleanTalk