How to use story.migrate for version updates
masterWhen updating your Ink files, you can prevent player save corruption by providing a migration function to story.migrate. This function is called during story:load_state().
state: The loaded state table.old_version: The version of the saved state.new_version: The version of the current book (defined by the'version'constant in your Ink content, or0if not specified).
If new_version > old_version, you can modify the state table (e.g., renaming variables or updating visit counts) before returning it.
local function migrate(state, old_version, new_version)
if new_version == old_version then
return state
end
-- Example: Migration for version 2
if new_version == 2 then
local old_mood = state.variables['mood']
if old_mood then
-- Convert old numeric mood to new string mood
state.variables['mood'] = old_mood < 50 and 'sadly' or 'sunny'
end
end
return state
end
story.migrate = migrate
story:load_state(saved_state)