Save Load Cycle Guide
##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
- [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):
- [Rotation table](Rotation table)
- [Id table](Id table)
- [Location table](Location table)
- [Velocity table](Velocity table)
[Methods](Object Methods):
.ApplyVelocity()
.ClearRouting()
.CreateJob()
.Delete()
.GetNearbyObjects()
.LeaveMap()
.NavigateTo()
.Sound()
[Properties](Object Properties):
.Active
.Age
.AiSetTarget
.AiWalkSpeed
.AnimateRotation
.AnimateVelocity
.AssignedRoom
.Attacker
.AttackTimer
.AvailableMoney
.AvatarControl
.BodyArmour
.BoilingPoint
.BookInCell
.Carried
.CarrierId
.Carrying
.CarryingBook
.Category
.Cell
.CellId
.CellIdTimer
.CloseTimer
-
.Connections
⚠️ .Contents
.CutsceneActor
.Damage
.Dest
.Door
.Driver
.Energy
.Equipment
.Entity
-
.ExternalPower
⚠️ .FailedToFindCell
.FeedJobId
.FireEngine
.Fireman
.FollowerId
.Fuel
.Gang
.Garbage
.HealingJobId
.Hidden
.Hose
.HoseOffset
.Hostage
.Id
.Intensity
.IsExternalTeacher
.IsNewIntake
.JobId
.LastAccess
.LastAte
.LastTreated
.LastVisitors
.LeavingMap
.Library
.Loaded
.Locked
.Misbehaviour
.Mode
.Naked
.Name
.Needs
.Office
-
.On
⚠️ .Open
.OpenDir
.Opened
.OpenOnTrigger
.OpenTimer
.Operation
.OperationTimer
.Or
.Patrol
.PermitPlayerControl
.PlayerOrderPos
.Pos
-
.Powered
⚠️ .Prisoner
.ProcessingRoom
.Quantity
.ReloadTimer
.RemotelyOpenable
.RequiredCellType
.RestState
.Result
.RotateDir
.SectorTimer
.Shackled
.Slot0..7
.SnitchTimer
.Speed
.State
.Station
.StatusEffects
.SubType
.TargetObject
.TazerTrained
.TimeOfLastMisconduct
.Timer
.Tooltip
.Triggered
.TriggeredBy
.TriggeredTime
.Type
.UnlockingDoor
.Vel
.WallCheckTimer
.Walls
.WatchingFight
.Weight
-
.Wired
⚠️ .WorldObject