save game with screen shot prob

Started by left, Thu 05/05/2005 18:07:11

Previous topic - Next topic

left

Hi, i am (foolishly) trying to create a save/ load gui following the method that was shown in the tech archive. Basically, i am having problems with it, I followed the instructions as best i could but to no avail.

When i run the game the gui comes up fine, but when you type in the name nto the text box and press save it says that this line of code is referencing an invalid item:

ListBoxGetItemText(SAVEGAME, 1, ListBoxGetSelected(SAVEGAME, 1), text);

as far as i can tell there is nothing wrong with the code, but i am obviously wrong. Should i post all the code i have used? it is basically the same as suggested in the tech archive.

Also, should definitions go before the game start function, and is it nessesary to end them in a similar way to how ags automatically ends its functions e.g. #sectionend.

thanks

Pumaman

Hazarding a guess, I'd say it's likely that there are no items in the listbox, and so GetSelected is returning -1, and then you're trying to get the text of that item.

Without seeing the script around it, I can't really suggest a solution.

strazer

QuoteAlso, should definitions go before the game start function, and is it nessesary to end them in a similar way to how ags automatically ends its functions e.g. #sectionend.

If you mean custom functions, they have to be placed before the code where they are called. They don't have to be enclosed by #section definitions.

left

#3
Hi here is the code, like i said it is from the tech archive so i have not really changed anything, i just tried to paste it into the relevant places but i have probably got loads wrong.

I have created 2 gui's: SAVEGAME and LOADGAME

with the objects being

0= label
1= list box
2 = save button or load button
3 = cancel button
4 = 104 by 78 size button for image
5 = text box (only on save game gui)

and here is the code

Code: ags


THIS BIT GOES AT THE TOP BEFORE THE GAME START FUNCTION

#define NEW_SAVE_FILE_TEXT "-> Create New File"
//This is the text used by the savegame dialog for the "new file" slot
//   in the savegame list. 

// These are used by the save/load system
string NewSaveSlotText;  // will contain the NEW_SAVE_FILE_TEXT string
int SaveLoadScreenshot = 0;  // pointer to screenshot sprite



function ShowSaveGameDialog() {	// Display custom savegame dialog
	string text;

 // Create temp save so we can grab a preview shot for the save box  
  SaveGameSlot(1, "temp");
  
  SaveLoadScreenshot = LoadSaveSlotScreenshot(1, 104, 78);
  DeleteSaveSlot(1);
  
  // Screenshot successfly loaded, set as button pic
  if(SaveLoadScreenshot != 0) {  //if there is an image
    SetButtonPic(SAVEGAME, 4, 1, SaveLoadScreenshot);

  }
  
  // Load up the savegame list
  ListBoxClear(SAVEGAME, 1);
  ListBoxSaveGameList(SAVEGAME, 1);
  
  // Take care of housekeeping
  if(ListBoxGetNumItems(SAVEGAME, 1) == 20) {
    // All 20 saves used, don't allow creation of new one
    Display("The save directory is full.[[Please replace a previous save.");
    ListBoxSetSelected(SAVEGAME, 1, ListBoxGetNumItems(SAVEGAME, 1) - 1);
    ListBoxGetItemText(SAVEGAME, 1,  ListBoxGetNumItems(SAVEGAME, 1) - 1, text);
    SetTextBoxText(SAVEGAME, 5, text);
  }
  else {
     // Still room, add a "new file" option to the end of the listbox
    ListBoxAdd(SAVEGAME, 1, NewSaveSlotText);
    ListBoxSetSelected(SAVEGAME, 1, ListBoxGetNumItems(SAVEGAME, 1) - 1);
    SetTextBoxText(SAVEGAME, 5, ""); //set the text box text to nothing
  }
  
  CentreGUI(SAVEGAME);
  GUIOn(SAVEGAME);
}

function ShowLoadGameDialog() {
  // Display custom loadgame dialog
  ListBoxClear(LOADGAME, 1);
  ListBoxSaveGameList(LOADGAME, 1);
  
  if(ListBoxGetNumItems(LOADGAME, 1) == 0) {
    Display("There are no files in the save directory.[[Record your progress by clicking the 'save' button, then you can go back to the point where you saved with the 'load' button.");
    return;
  }
  
  ListBoxSetSelected(LOADGAME, 1, 0);

  SaveLoadScreenshot = LoadSaveSlotScreenshot(savegameindex[0], 104, 78);
  
  if(SaveLoadScreenshot != 0) {
    SetButtonPic(LOADGAME, 4, 1, SaveLoadScreenshot);
  }

  CentreGUI(LOADGAME);
  GUIOn(LOADGAME);
}

THEN IN GAME START FUNCTION

#sectionstart game_start  // 
function game_start() {
  // called when the game starts, before the first room is loaded
  
  // Initialize the string we check against to decide whether to replace or not
  StrCopy(NewSaveSlotText, NEW_SAVE_FILE_TEXT);
  
  // Set size of savegame images to match size of button we put them on
  game.screenshot_width = 104;
  game.screenshot_height = 78;

  GUIOff(0);
  GUIOff(2);
  GUIOff(3);
  GUIOff(4);
  SetInvDimensions(60,40); //sets the inv to these dimensions
}
#sectionend game_start  //

THEN INSIDE INTERFACE CLICK


  if (interface == SAVEGAME) {  // Save Game
  
    string text; //define string name
    
    
    if(button == 1) {  // Listbox
      // Fill the textbox with description of clicked save slot
      ListBoxGetItemText(SAVEGAME, 1, ListBoxGetSelected(SAVEGAME, 1), text);
      if(StrComp(text, NewSaveSlotText)) { // existing slot, use existing desc
        SetTextBoxText(SAVEGAME, 5, text);
      } 
      else    // new file slot, just empty it
        SetTextBoxText(SAVEGAME, 5, "");
  }
 
else if(button == 3) {  // Cancel button
      // clean up the sprite cache
      if(SaveLoadScreenshot != 0) {
        SetButtonPic(SAVEGAME, 4, 1, 343);  // 2055 is completely transparent
        DeleteSprite(SaveLoadScreenshot);
        SaveLoadScreenshot = 0;
      }
      GUIOff(SAVEGAME);
}
    else {  // Save button or return in textbox
      // clean up sprite cache
      if(SaveLoadScreenshot != 0) {
        SetButtonPic(SAVEGAME, 4, 1, 343);  // 2055 is completely transparent
        DeleteSprite(SaveLoadScreenshot);
        SaveLoadScreenshot = 0;
      }
      // don't get the GUI caught in screenshot!
     GUIOff(SAVEGAME);
     SetDefaultCursor();
     Wait(1);
      // save the game
      
      ListBoxGetItemText(SAVEGAME, 1, ListBoxGetSelected(SAVEGAME, 1), text);
      if(StrComp(text, NewSaveSlotText)) {
        GetTextBoxText(SAVEGAME, 5, text);
        SaveGameSlot(savegameindex[ListBoxGetSelected(SAVEGAME, 1)], text);
      }
      else {
        GetTextBoxText(SAVEGAME, 5, text);
        SaveGameSlot(ListBoxGetSelected(SAVEGAME, 1), text);
      }
    }
}  // end if interface SAVEGAME
  
  if (interface == LOADGAME) {  // Load Game
    if(button == 1) {  // Listbox
      // Clean sprite cache
      if(SaveLoadScreenshot != 0) {
        SetButtonPic(LOADGAME, 4, 1, 343);  // 2055 is completely transparent
        DeleteSprite(SaveLoadScreenshot);
        SaveLoadScreenshot = 0;
      }
     
      // Load up new preview shot
      SaveLoadScreenshot = LoadSaveSlotScreenshot(savegameindex[ListBoxGetSelected(LOADGAME, 1)], 104, 78);
       
      if(SaveLoadScreenshot != 0) {
        SetButtonPic(LOADGAME, 4, 1, SaveLoadScreenshot);
      }
      else {
        SetButtonPic(LOADGAME, 4, 1, 343);  // 2997 is completely transparent
      }
    }
    else if(button == 3) {  // Cancel button
      // clean cache
      if(SaveLoadScreenshot != 0) {
        SetButtonPic(LOADGAME, 4, 1, 343);  // 2997 is completely transparent
        DeleteSprite(SaveLoadScreenshot);
        SaveLoadScreenshot = 0;
      }
      GUIOff(LOADGAME);
    }
    else if(button == 2) {  // Load button
      // clean...yeah
      if(SaveLoadScreenshot != 0) {
        SetButtonPic(LOADGAME, 4, 1, 343);
        DeleteSprite(SaveLoadScreenshot);
        SaveLoadScreenshot = 0;
      }
      RestoreGameSlot(savegameindex[ListBoxGetSelected(LOADGAME, 1)]);
      GUIOff(LOADGAME);
    }
  }  // end if interface LOADGAME
}


the end

sorry i cant find how format the code text, what is the text you put b4 it for this again?

i realise its alot to ask but any help is greatly appreciated

thanks ben

Edit by strazer:

Use the
[ code ]xyz[ /code ]
tags (without spaces).

left


Pumaman

Try putting in some debugging on the line before the error -- something like this:

Display("Selected item: %d", ListBoxGetSelected(SAVEGAME, 1));

so that you can check what its value is.

left

the value turned out as expected to be -1. but i still cant figure it out.

I have also tried to use the alternative code (shown in a tutorial) which uses structs and so on. That works a little better but it does not completely work either.

Basically, i dont really need screen shots, i need to create a save load function, but i want to control how it looks. Is there a way of changing the way the pre built save/loa game gui's look, or can you point me towards a tutorial of how to create your own save load system or tell me how to do it. i have tried that as well, but as you can tell my coding is not the best so it does not work either.

thanks

Pumaman

It's always quite hard to follow these old-style save/load scripts. I can't see an obvious reason why it should be -1; does the first item in the list box look like it's selected at the time?

SMF spam blocked by CleanTalk