Skip to content

Save Load Cycle Guide

aubergine10 edited this page Oct 19, 2015 · 1 revision

##Overview

Your scripts must handle the save-load cycle to prevent strange things happening when a savegame is loaded. In this guide we'll look at what happens when a game is saved and loaded, and then implement a robust solution to dealing with the side-effects.

This guide assumes you are already familiar with [Object Events](Object Events)

##Saving

When saving a game, the following data will NOT be stored in the .prison file:

  • Custom variables of any type (including functions) whether local or global
  • Custom [Object Properties](Object Properties) that are not one of the following types:
  • boolean
  • number
  • string
  • Custom [Object Methods](Object Methods)

Thus, you must reinitialise all of these when the save is loaded...

##Loading

When a .prison file (save game) is loaded, the following happens (worded for clarity, not technical accuracy):

  • The map and all objects (including any saved properties) that were on it are loaded
  • The scripts associated with scripted objects are initialised
  • The Update() event is triggered

##Reinitialising

To deal with restoring your script state, you can do some trickery with the Update() event - or more specifically the function that handles it. Here's an example:

-- despam Update
local Now   = Game.Time
local Delay = 2 -- game seconds = prison minutes
local Ready = Now() + Delay

function Create() -- called once when your object is first placed on map

  -- your code here

  -- replace Update() with RecurringUpdate()
  _G.Update = _G.RecurringUpdate
  _G.RecurringUpdate = nil
end

function Update() -- called once when savegame is loaded

  -- your code here

  -- replace Update() with RecurringUpdate()
  _G.Update = _G.RecurringUpdate
  _G.RecurringUpdate = nil
end

function RecurringUpdate() -- called repeatedly, every frame
  if Now() > Ready then

    -- your code here

    Ready = Now() + Delay
  end
end

The code sample above is a great template for your scripts:

  • Create() is called only once, when the object is first created on the map
  • Update() is called once, whenever a save game is loaded
  • RecurringUpdate() is called repeatedly thereafter, and it's despammed to minimise performance impacts

^ Open "Pages" to Search



Guides:

  • [Lua Basics](Lua Basics Guide)
  • [Save-Load Cycle](Save-Load Cycle Guide)

[Globals](Object Globals):

  • [Game](Game (Global))
  • [me](me (Global))
  • [Object](Object (Global))
  • [this](this (Global))

[Events](Object Events):

Psuedo-Types:

  • [Rotation table](Rotation table)
  • [Id table](Id table)
  • [Location table](Location table)
  • [Velocity table](Velocity table)

[Methods](Object Methods):

[Properties](Object Properties):

Clone this wiki locally