Forum rules - please read before posting.

Can I load a save through script?

edited October 2022 in Technical Q&A

Hi all,
I was just wondering if it's possible to load a save through scripting?
I tried this: AC.SaveSystem.LoadGame(1);
But nothing happened (Id 1 does exist)

Thank you :)

EDIT: I've made it work but it doesn't seem to be going through the OnLoad or OnStart actionlists, is there a way for it to trigger the OnLoad cutscene thru script?

«1

Comments

  • SaveSystem.LoadGame is the right function call. It shouldn't prevent OnLoad from running - though OnStart won't be run in the case of loading a save.

    What's your AC version?

  • Hi Chris,
    That's weird it doesn't run automatically for me.
    My AC version is v1.70.1
    An older version (because i've changed the player scripts lol) but I think it should still work?

  • It should - and it does on my end when testing.

    How are you testing for OnLoad running, and have you changed any script that may have affected it?

  • I'm using an Actionlist that does the Call Event on a script on a game object which then does the load save part. The save loads but it doesn't run onload automatically. Is there a way to call it in script?

  • You can run the OnLoad cutscene with:

    KickStarter.sceneSettings.OnLoad ();
    

    To have it run after the loading process is complete, hook into the OnFinishLoading, in a script attached to the PersistentEngine prefab:

    using UnityEngine;
    using AC;
    
    public class RunLoadCutscene : MonoBehaviour
    {
    
        private void OnEnable () { EventManager.OnFinishLoading += OnFinishLoading; }
        private void OnDisable () { EventManager.OnFinishLoading -= OnFinishLoading; }
    
        private void OnFinishLoading ()
        {
            KickStarter.sceneSettings.OnLoad ();
        }
    
    }
    
  • edited October 2022

    Thanks Chris, I managed to get that to work but now I have an issue that only shows up when I build to android.

    Where my onload cutscene script (LoadTheGame) doesn't run if AC.SaveSystem.LoadGame was used... I have a Debug.Log to check if its being run and if I comment it out then the Debug.Log comes through, but if I don't then it doesn't and the cutscene doesn't run at all etc.
    Hope that makes sense. Just wondering if it's something I can fix?

    https://imgur.com/a/zKIamrk

    Here's the simple script. It works in the editor but not on android build... which is why I was late to reply lol. Thank you :)

  • The difference may be down to the longer load times on Android. It's not reliable to use Invoke to run the function a set time later, as you can't predict how long the load operation will take. The use of events, as above, ensures that the code gets run only once the loading is complete - regardless of how long it takes.

  • edited October 2022

    Hi Chris,
    Thank you for that, I'm not sure if I've used events before, how do I do that?

    Edit: Ok I think I understand, so would I be able to use OnFinishLoading inside my script and it would still work? I was worried the gameobject the script is attached to would get deleted when the scene changes or something

  • It will need to be persistent, i.e. survive scene changes.

    You can make it so by attaching it to AC's PersistentEngine prefab.

  • edited October 2022

    Thanks so much again for your help Chris!
    I have the script attached to PersistentEngine but it doesn't seem to go through, even the debug.log. I've probably done something dumb again lol, here is the pastebin
    https://pastebin.com/qwh94YNv

  • If it's attached to PersistentEngine, remove the Awake function as it may interfere.

    The event itself looks fine, though - it should be called. Are you testing in builds or the Editor as well?

    If the event isn't called, then it may be due to the same underlying reason the OnLoad cutscene isn't being called. It's not an issue I can recreate, however.

    It may be that the loading process is failing, somehow. Try replacing your OnEnable/OnDisable functions with this replacement that has them also hook into the OnBeforeLoading and OnFailLoading events - what Logs show then?

    private void OnEnable()
    {
        EventManager.OnFinishLoading += OnFinishLoading;
        EventManager.OnBeforeLoading += OnBeforeLoading;
        EventManager.OnFailLoading += OnFailLoading;
    }
    
    private void OnDisable()
    {
        EventManager.OnFinishLoading -= OnFinishLoading;
        EventManager.OnBeforeLoading -= OnBeforeLoading;
        EventManager.OnFailLoading -= OnFailLoading;
    }
    
    private void OnBeforeLoading (SaveFile saveFile)
    {
        Debug.Log ("Request made to load " + saveFile);
    }
    
    private void OnFailLoading (int saveID)
    {
        Debug.Log ("Failed to load save ID " + saveID);
    }
    
  • edited October 2022

    Hi Chris,
    I'm testing in both. It works in the editor but on the android it stops working.

    Since removing the invoke it doesn't work in editor at all

    I get "Request made to load AC.SaveFile" and no fail log

    But it doesn't get to the OnLoad cutscene again lol and no log from OnFinishLoading
    It's like it's getting lost after the save loads

  • Does loading the save file involve switching scene, or do you have Always reload scene when loading a save file? checked in the Settings Manager? If either of these things are true, the loading process will be slightly different.

    Running the game on Android, however, shouldn't be a factor - at least, node in AC's save-system code. If you've made modification to AC's code, I'd suggest backing up a copy and trying the original release.

    The load process occurs in the SaveSystem script - starting with the ReceiveDataToLoad function. If no scene loading is required, it'll then run _OnLevelWasLoaded, followed by ReturnToGameplay. If scene loading is required, it'll run _OnLevelWasLoaded via the SceneLoaded event hook. Placing Debug.Log statements in these functions may help shed some light.

  • edited October 2022

    Hi Chris,
    I have Always reload scene loading a save file checked and yes it involved switching scenes. I haven't changed AC's code

  • Use logs to see which of the SceneLoaded / _OnLevelWasLoaded / ReturnToGameplay functions are running, in that case.

  • I put comments in ReturnToGameplay and OnLevelWasLoaded straight after the { and got comments on both, what did you mean I should do sorry?

  • The ReturnToGameplay function is what calls the OnLoad cutscene to be run - see the line:

    KickStarter.sceneSettings.OnLoad ();
    

    Use logs to try to find out if it's being run, or why not.

  • OK I put a debug.log right under KickStarter.sceneSettings.OnLoad (); and nothing came up so I dont think its being run


  • So here's what I found, I get Debug.Log "1" and "4" but not "2" and "3". What does this mean? It's not loading game?

  • The loadingGame variable is used to determine the condition under which a scene-change takes place. It should have been set to LoadingGame.InNewScene on line 538.

    There are three such lines where the loadingGame variable is set: 538, 544 and 1764. Try placing logs under each that print what its being set to, i.e.:

    Debug.Log ("LoadingGame: " + loadingGame);
    

    Is it being set to InNewScene, and then No before the code above is run, or is it not being set at all? Check the stacktraces of each instance of these new logs, to see where/when they're being called.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.