I can't make a saveslot

Started by Salamdandersad, Mon 21/10/2024 17:37:35

Previous topic - Next topic

Salamdandersad

Hello guys, I'm developing a game for the first time with this engine. I have the first rooms, the music, and the GUI for the start and pause menus. The game is in first person, Myst-style. However, when I try to configure the save and load, it doesn't allow me.

The game works with this code. However, when I try to save, it throws me the error message that I have set (it's in Spanish because I'm from Spain).

The button configuration for the GUIs is correct because I see the error messages. Do I need to do anything extra?

<<// main global script file

// called when the game starts, before the first room is loaded
function game_start()
{
    gGui1.Visible = true; // Muestra el menú de inicio
    gPauseMenu.Visible = false; // Asegúrate de que el menú de pausa esté oculto al inicio
    // Auto-save on the save slot 999
    SetRestartPoint();
}

// called on every game cycle, except when the game is blocked
function repeatedly_execute()
{
}

// called on every game cycle, even when the game is blocked
function repeatedly_execute_always()
{
}

// called when a key is pressed
function on_key_press(eKeyCode keycode, int mod)
{
    // Si el juego está pausado, no se procesan otras teclas
    if (IsGamePaused())
    {
        if (keycode == eKeyEscape)
        {
            // Si se presiona Escape, oculta el menú de pausa
            gPauseMenu.Visible = false;
            // Aquí puedes agregar cualquier lógica para reanudar el juego
        }
        else
        {
            // No reacciona a ninguna otra tecla
            return;
        }
    }

    // Lógica para el menú de inicio
    if (keycode == eKeyQ && (mod & eKeyModCtrl))
    {
        // Ctrl-Q will quit the game
        QuitGame(1);
    }
    else if (keycode == eKeyF9)
    {
        // F9 will restart the game
        RestartGame();
    }
    else if (keycode == eKeyF12)
    {
        // F12 will save a screenshot to the save game folder
        SaveScreenShot("screenshot.pcx");
    }
    else if (mod & eKeyModCtrl)
    {
        if (keycode == eKeyS)
        {
            // Ctrl-S will give the player all defined inventory items
            Debug(0, 0);
        }
        else if (keycode == eKeyV)
        {
            // Ctrl-V will show game engine version and build date
            Debug(1, 0);
        }
        else if (keycode == eKeyA)
        {
            // Ctrl-A will show walkable areas
            Debug(2, 3);
        }
        else if (keycode == eKeyX)
        {
            // Ctrl-X will let the player teleport to any room
            Debug(3, 0);
        }
    }

    // Manejo de la tecla Escape para pausar el juego
    if (keycode == eKeyEscape)
    {
        if (gPauseMenu.Visible == false)
        {
            // Mostrar el menú de pausa
            gPauseMenu.Visible = true;
            // Aquí puedes detener la lógica del juego si es necesario
        }
        else
        {
            // Ocultar el menú de pausa
            gPauseMenu.Visible = false;
            // Reanudar el juego
            // Si necesitas hacer algo específico para reanudar, hazlo aquí
        }
    }
}

// called when a mouse button is clicked
function on_mouse_click(MouseButton button)
{
    if (IsGamePaused())
    {
        // El juego está pausado, así que no procesar clics del ratón
        return;
    }
    else if (button == eMouseLeft)
    {
        // clic izquierdo, intenta usar el modo de cursor actual en esta posición
        Room.ProcessClick(mouse.x, mouse.y, mouse.Mode);
    }
    else if (button == eMouseRight)
    {
        // clic derecho, cambia el modo de cursor
        mouse.SelectNextMode();
    }
}

// Maneja el clic del botón para comenzar el juego
function Button1_OnClick(GUIControl *control, MouseButton button)
{
    // Cambia al jugador a la room 1
    player.ChangeRoom(1);
    // Oculta el menú de inicio después de comenzar el juego
    gGui1.Visible = false;
}

// Maneja el clic del botón "Resume" en el menú de pausa
function btnResume_OnClick(GUIControl *control, MouseButton button)
{
    // Oculta el menú de pausa
    gPauseMenu.Visible = false;
    // Aquí puedes agregar cualquier lógica adicional para reanudar el juego
}

// Maneja el clic del botón "Save" en el menú de pausa
function btnSave_OnClick(GUIControl *control, MouseButton button)
{
    // Guarda el juego en el slot 1, con la descripción "Juego guardado"
    if (SaveGameSlot(1, "Juego guardado en el menú de pausa"))
    {
        // Notifica al jugador que el juego se ha guardado correctamente
        Display("El juego se ha guardado correctamente.");
    }
    else
    {
        // Si el guardado falla, muestra un mensaje de error
        Display("Error: No se pudo guardar el juego en el slot 1.");
        Debug(0, 0); // Mostrar información de depuración
    }
}

// Maneja el clic del botón "Load" en el menú de inicio
function btnLoad_OnClick(GUIControl *control, MouseButton button)
{
    // Verifica si existe un archivo de guardado en el slot 1
    if (File.Exists("savegame1.sav")) // Cambiado para verificar la existencia del archivo
    {
        RestoreGameSlot(1); // Cambiado a RestoreGameSlot
        // Oculta el menú de inicio después de cargar el juego
        gGui1.Visible = false; // Opcional: si quieres ocultar el menú de inicio al cargar
    }
    else
    {
        Display("No hay juego guardado en slot 1.");
    }
}

// Función que se llama cuando se muestra el menú de pausa
function on_pause_menu_show()
{
    // Detén cualquier lógica de juego si es necesario
}

// Función que se llama cuando se oculta el menú de pausa
function on_pause_menu_hide()
{
    // Reanuda cualquier lógica de juego si es necesario
}>>

eri0o

#1
  • There is a code formatting option in the forums, it's better to use it to ensure any code doesn't get interpreted as tag and disappears
  • When mentioning an error message, it's a good idea to paste here the text of the error message - usually just hitting Ctrl+C with the message box that pops up will copy the text. Alternatively you can screenshot and use an image host like imgur to link the image directly here (there is also an image tag in the forums!)

Other than this I am not sure how you made your gGui1, what it has and it would be nice to check the functions from it are actually linked to the globalscript by checking the events tab - I know you said you did, but since there's a mysterious error message and it's not commented what you are seeing it's hard to tell what could be wrong. :/

Ah, right, just to check the error isn't file access, one good idea is running one of the templates and checking if you can save and load with them.

Crimson Wizard

#2
Quote from: Salamdandersad on Mon 21/10/2024 17:37:35if (File.Exists("savegame1.sav")) // Cambiado para verificar la existencia del archivo

That is NOT how AGS save files are called, and not how save directory is accessed, and in general that's not how you test for the save's existance.


First of all, about correct code, the existing method for testing is trying to get save's description. If it returns "null", then the save does not exist:
https://adventuregamestudio.github.io/ags-manual/Game.html#gamegetsaveslotdescription
Code: ags
    if (Game.GetSaveSlotDescription(1) != null)


Now, why your code did not work:
1. AGS saves are called "agssave.NNN", not "savegameN.sav". Save slot 1 would be: "agssave.001"
2. In order to access save files directly you would need to tell AGS to check save directory like "$SAVEGAMEDIR$/agssave.001".
More info on file locations may be found here: https://adventuregamestudio.github.io/ags-manual/File.html#fileopen

But I don't recommend doing that, because that will rely on engine using disk files. If it happens that some engine port on another system will use different kind of save system, then this method will likely fail. So using Game.GetSaveSlotDescription method would be best.

Salamdandersad

Oh, okay, that's fine. As I said, this is my first time with this engine, but not with others, and I automatically thought it would be the same file handling as other engines.

So I understand that the code for saving is fine, but I would just have to change the file type.

How can I integrate Game.GetSaveSlotDescription into the code then?

Thanks  :smiley:

Crimson Wizard

#4
Quote from: Salamdandersad on Mon 21/10/2024 20:57:49How can I integrate Game.GetSaveSlotDescription into the code then?

Simply replacing one function with another:

Code: ags
    // Verifica si existe un archivo de guardado en el slot 1
    if (Game.GetSaveSlotDescription(1) != null) // Cambiado para verificar la existencia del archivo
    {
        RestoreGameSlot(1); // Cambiado a RestoreGameSlot
        // Oculta el menú de inicio después de cargar el juego
        gGui1.Visible = false; // Opcional: si quieres ocultar el menú de inicio al cargar
    }
    else
    {
        Display("No hay juego guardado en slot 1.");
    }

Salamdandersad

Thanks so much now work it!!! :-D

SMF spam blocked by CleanTalk