Hey guys! This is pretty much a stock GUI I found on a tutorial webpage somewhere, but it doesn't work at all. The game just crashes when you try to save...stating problems in the code lines...then "Error: DeleteSprite" or something of that nature. The problem is, it doesn't display it all the time, sometimes it works...And if you try to save over a game you already made, it will act like it worked, but won't save at all! It also won't display the confirmation GUI...guh, this tutorial is so bad it makes my head hurt...and it was the better one I could find...
If there is a better tutorial out there for making your own save/load GUIs, could anyone turn me on to it? I got this one off of
http://www.twin-design.com/agsezine/9/tutorial.cfm
So without further ado, here is the code:
function sgs_getslots() {
int i = 1; //Loop counter starts at 1
while (i <= 6) {
if (GetSaveSlotDescription(i, sgs_struct[i].t)) { //Slot exists
sgs_struct[i].spr=LoadSaveSlotScreenshot(i, SGS_WIDTH, SGS_HEIGHT);
}
else { //There is no saved game for this slot
StrCopy(sgs_struct[i].t, ""); //Blank description
sgs_struct[i].spr=SGS_EMPTY_SPRITE;
}
SetLabelText(SAVELOAD, 6+i, sgs_struct[i].t); //Set GUI label to description
SetLabelColor(SAVELOAD, 6+i, SGS_LABEL_COLOR); //Set GUI label color
SetButtonPic(SAVELOAD, i, 1, sgs_struct[i].spr); //Set button to have screenshot pic
i=i+1; //next slot
}
//Get current game ss, too
Wait(1); //make sure the screen is updated
SaveGameSlot(100, "temp"); //Save current game in temp slot
sgs_struct[0].spr=LoadSaveSlotScreenshot(100, SGS_WIDTH, SGS_HEIGHT); //load up screenshot
DeleteSaveSlot(100); //Delete tomporary slot
}
function sgs_close() { //This cleans out sprite cache and eliminates memory leaks
int i=0;
GUIOff(SAVELOAD);
SetDefaultCursor(); //restores cursor back to how it was
while (i <= 6) {
if (sgs_struct[i].spr != SGS_EMPTY_SPRITE) {
DeleteSprite(sgs_struct[i].spr);
}
i=i+1;
}
sgs_saveload=0;
}
function sgs_do_save() {
sgs_close(); // Close the GUI
Wait(1); //Make sure the screen is updated without the GUI
SaveGameSlot(sgs_selected_slot, sgs_struct[sgs_selected_slot].t);
}
function sgs_check_do_save() {
if (sgs_struct[sgs_selected_slot].spr != SGS_EMPTY_SPRITE) { //already a game there
sgs_saveload=3; //Record that we are in the "confirm" state
CentreGUI(CONFIRM);
GUIOn(CONFIRM);
}
else {
sgs_do_save();
}
}
function sgs_save() {
SetLabelText(SAVELOAD, 0, "Save Game");
sgs_saveload = 1; //changing this int to 1 means save game.
sgs_selected_slot = -1; //Make the currently selected slot "none."
sgs_getslots(); //call another function to get screenshots loaded
CentreGUI(SAVELOAD);
GUIOn(SAVELOAD);
SetMouseCursor(6); //Use the pointer cursor
}
function sgs_load() {
SetLabelText(SAVELOAD, 0, "Load Game");
sgs_saveload = 2; //changing the int to 2 means load game.
sgs_selected_slot = -1; //Make the currently selected slot "none."
sgs_getslots(); //call another function to get screenshots loaded
CentreGUI(SAVELOAD);
GUIOn(SAVELOAD);
SetMouseCursor(6); //pointer cursor
}
function sgs_interface_click (int button) {
int labl; //loop variable
if (sgs_saveload == 3) { //CONFIRM dialog
GUIOff(CONFIRM); //turn off GUI either way
sgs_saveload=2; //Back to save state
if (button == 1) { //OK
sgs_do_save();
} //else Cancel, so do nothing
}
else { //SAVELOAD dialog
if ((button == 14) || ((button == 13) && (sgs_selected_slot < 0))) { //Cancel or OK with nothing selected
sgs_close();
}
else if (button == 13) { //OK
if (sgs_saveload == 1) {//SAVE the game
sgs_check_do_save();
}
else { //LOAD
sgs_close();
RestoreGameSlot(sgs_selected_slot);
}
}
else if ((button <= 6) && ((sgs_saveload == 1) || (sgs_struct[button].spr != SGS_EMPTY_SPRITE))) {
if (sgs_saveload == 1) { //SAVE
if (sgs_selected_slot > 0) { //If a different slot was already selected
SetButtonPic(SAVELOAD, sgs_selected_slot, 1, sgs_struct[sgs_selected_slot].spr); //Set the old slot back to its saved picture
if (GetSaveSlotDescription(sgs_selected_slot, sgs_struct[sgs_selected_slot].t) == 0) { //Get the old slot description back, too.
StrCopy(sgs_struct[sgs_selected_slot].t, ""); //If there is no description, use and empty string
}
SetLabelText(SAVELOAD, 6+sgs_selected_slot, sgs_struct[sgs_selected_slot].t); //Set the GUI label to the old description
}
SetButtonPic(SAVELOAD, button, 1, sgs_struct[0].spr); //change selected button to have current game screenshot
}
sgs_selected_slot = button;
WaitMouseKey(20); //check for double-click
if (IsButtonDown(LEFT)) { //Double-click
if (sgs_saveload == 1) { //Save
sgs_check_do_save(); //causes a save immediately
}
else {
sgs_close();
RestoreGameSlot(sgs_selected_slot);
}
}
//We now highlight the selected slot by setting it's corresponding label color
labl = 1;
while (labl <= 6) {
SetLabelColor(SAVELOAD, 6+labl, SGS_LABEL_COLOR); //whatever I said it was
labl=labl+1;
}
SetLabelColor(SAVELOAD, 6+button, SGS_SELECT_COLOR); //again, whatever I said it was
}
}
}
function sgs_on_key_press(int keycode) {
int len;
if ((sgs_saveload == 1) && (sgs_selected_slot > 0)) {
//only run this code on condition:
//a) doing a SAVE (sgs_saveload == 1)
//b) a save slot IS selected (sgs_selected_slot > 0)
len = StrLen(sgs_struct[sgs_selected_slot].t); // Get the current length of the slot description
if ((keycode == 8) && (len>0)) { //Delete key pressed and there is something to delete
StrSetCharAt(sgs_struct[sgs_selected_slot].t, len-1, 0); //Erase current character
}
if (keycode == 13) { //ENTER is pressed
sgs_check_do_save();
}
if ((keycode >= ' ') && (keycode < 127) && (len < 18)) { //Normal character
//add this character on end of string:
StrFormat(sgs_struct[sgs_selected_slot].t, "%s%c", sgs_struct[sgs_selected_slot].t, keycode);
sgs_flash=0; // This will force an update of the label
}
}
}
function sgs_repeatedly_execute() {
string sgs_desc;
//Save dialog typing flashing cursor
if ((sgs_saveload == 1) && (sgs_selected_slot > 0)) {
if (sgs_flash == 0) { //When flash count is zero, display text with cursor
StrCopy(sgs_desc, sgs_struct[sgs_selected_slot].t); //Copy actual text
StrCat(sgs_desc, "*"); //and add "*" for cursor
sgs_flash = 20; //set flash countdown
SetLabelText(SAVELOAD, 6+sgs_selected_slot, sgs_desc);
}
else {
sgs_flash = sgs_flash - 1; //decrease count
}
if (sgs_flash == 10) { //When flash count is ten, display text without cursor
SetLabelText(SAVELOAD, 6+sgs_selected_slot, sgs_struct[sgs_selected_slot].t);
}
}
}
Any thoughts?
Edit by strazer: [ code ]yfied.
I really should get Vel/Darth to update that page. That tutorial only works with the version of AGS that was extant at the time, as CJ has subsequnetly changed the Dynamic sprite handling slightly. If you download the source code of the "Pixel Hunt" game on http://www.ssh.me.uk/pixelhunt.html then you can see an updated version that should work with current AGS.
Direct link to source code: http://www.freeweb.telco4u.net/superscottishhero/phsource.zip
See also the previous thread on the subject: http://www.adventuregamestudio.co.uk/yabb/index.php?topic=21857.0