A Lua module that records and rewinds values. Extremely useful in games that use a rewind mechanic. Tested with Defold and LOVE but should work with any game engine. By default it is set to work in fixed time-steps. If your game engine has variable time-steps and vsync is off, set fixedStep to false.
- Save the module (timetraveler.lua) inside your project folder
- Import it in your script:
local traveler = require("timetraveler")
- Inside the init function, initialize the module:
function init(self)
traveler.init(
{go.get_position, go.set_position},
{go.get_rotation, go.set_rotation},
{go.get_scale, go.set_scale}
)
end
- At the end of the update function add the if statement:
function update(self, dt)
-- GAME LOGIC HERE --
if doRewind then
traveler.rewind(dt)
else
traveler.record(dt)
end
end
- Save the module (timetraveler.lua) inside your project folder
- Import it in your script:
local traveler = require("timetraveler")
- Inside the load function, initialize the module:
function love.load()
traveler.init(
{function() return player.x end, function(v) player.x = v end},
{function() return player.y end, function(v) player.y = v end}
)
end
- At the end of the update function add the if statement:
function love.update(dt)
if love.keyboard.isDown('lshift') then
traveler.rewind(dt)
else
-- GAME LOGIC HERE --
traveler.record(dt)
end
end
function - Initializes the traveler and receives any number of tables. The table(s) must be composed of 2 functions: a function that retrieves the value to record and a function that sets the value recorded back. Eg.:
traveler.init(
{go.get_position, go.set_position},
{function() return self.flip end, function(v) self.flip = v end}
)
function - Records the values per frame.
function - Rewinds back the values per frame.
number - How far back in time the traveler can go. It is expressed in seconds and it defaults to 5.
boolean - True by default. If your game engine works with a variable time-step, set it to false.
number - How many frames per seconds record. Useful when you have a variable time-step engine. It is 60 by default.
Check the folder examples inside the repository.