Create AutoSave in Lua

  • #1, by darren-beckettFriday, 23. October 2015, 11:31 9 years ago
    How do I perform this action using Lua?

    Execute Autosave #1

    Great Poster

    384 Posts


  • #2, by afrlmeFriday, 23. October 2015, 11:57 9 years ago
    You can't. There's currently no Lua functions that I know of relating to save / load game.

    Why do you want to use Lua for this? If you are wanting to create an automated autosave system then sure you can use Lua script to a degree for checking when it should save the game, but you will still need to create a called by other action which contains an execute autosave action part.

    How I sorted out my autosave system was to create a loop in Lua script using the mainLoop event handler, then inside the loop function I listen out for two things...

    1. scene change & 2. scene is not a menu. I update a variable with new scene if it doesn't equal the last scene & if it's not a menu then I call the called by other action containing the autosave actions.

    -- * variables * --
    local ps -- empty variable(s)
    
    -- * function used to check if game should autosave (saves on new scene [playable]) * --
    function autosaveSystem()
     if game.CurrentScene:getName() ~= ps then
      ps = game.CurrentScene:getName() -- update scene name
      if not game.CurrentScene:getBool(VSceneIsMenu) and not game.CurrentScene:isEmpty() then 
       startAction("Actions[autosave_system]")
      end
     end
    end
    
    registerEventHandler("mainLoop", "autosaveSystem") -- creates a loop function which loops autosaveSystem() function
    

    Imperator

    7278 Posts

  • #3, by darren-beckettFriday, 23. October 2015, 13:47 9 years ago
    I have a global key handler which watches for the Back Key (Android), so i wanted to create a save point before going to the menu.
    I'll create an action and call that then, thanks.

    Great Poster

    384 Posts