## The World's Easiest Load/Save Mechanism

In order to give your game a unique look and feel, it's entirely reasonable to want to code your own load and save routines with text entry, lists of existing games and so on. However, if you're just getting started on a game, here's the simplest way to get a loading and saving mechanism working. Set up a keyboard handler (if you don't have one already) and add these two lines (assuming the variable it's checking is called k - if not change the code appropriately).

    if (k == "s") saveGame ("saved.gam");
    if (k == "l") loadGame ("saved.gam");

Easy! Of course, you might want to do more than just call the saveGame and loadGame functions. You may want to let people pick a slot to save into, for example. Feel free to use and build on this...

    objectType saveSlot1 ("") {}

    objectType saveSlot1 ("") {}

    objectType saveCancel ("") {}

    sub myGameStuff (clickFunc) {
       freeze ();
       pasteString ("SLOT 1", 25, 65);
       addScreenRegion (saveSlot1, 20, 20, 100, 100, 0, 0, -1);
       pasteString ("SLOT 2", 125, 65);
       addScreenRegion (saveSlot2, 120, 20, 200, 100, 0, 0, -1);
       pasteString ("CANCEL", 125, 65);
       addScreenRegion (saveCancel, 120, 20, 200, 100, 0, 0, -1);
       onLeftMouse (clickFunc);
    }

    sub saveGameClick () {
       var clickedWhat = getMouseOverObject ();
       if (clickedWhat) unfreeze ();
       if (clickedWhat == saveSlot1) saveGame ("saved1.gam");
       if (clickedWhat == saveSlot2) saveGame ("saved2.gam");
    }

    sub loadGameClick () {
       var clickedWhat = getMouseOverObject ();
       if (clickedWhat) unfreeze ();
       if (clickedWhat == saveSlot1) loadGame ("saved1.gam");
       if (clickedWhat == saveSlot2) loadGame ("saved2.gam");
    }

And in your keyboard handler...

    if (k == "s") myGameStuff (saveGameClick);
    if (k == "l") myGameStuff (loadGameClick);

There are much more advanced load and save examples on the SLUDGE website - and here's how to get there if you'd rather dive in at the deep end and swim from there.

### See also:

deleteFile

fileExists

getMatchingFiles

loadGame

renameFile

saveGame

SLUDGE and this SLUDGE documentation are copyright Hungry Software and contributors 2000-2012