Skip to content

Instantly share code, notes, and snippets.

@amalloy
Created January 27, 2023 14:49
Show Gist options
  • Save amalloy/2643748e48763d3a090081a2b7ed813c to your computer and use it in GitHub Desktop.
Save amalloy/2643748e48763d3a090081a2b7ed813c to your computer and use it in GitHub Desktop.
function savePiecePositions()
savedPieces = {}
for _, obj in ipairs(getObjectsWithTag("Resettable")) do
savedPieces[obj.getGUID()] = {
data=obj.getData(),
position=obj.getPosition(),
rotation=obj.getRotation(),
}
end
end
function restorePiecePositions()
for _, obj in ipairs(getObjectsWithTag("Resettable")) do
local guid = obj.getGUID()
local saved = savedPieces[guid]
if saved == nil then
-- Piece that wasn't around when we saved; delete it
obj.destruct()
else
-- Put it back where it was, and un-save it
obj.setPositionSmooth(saved.position)
obj.setRotationSmooth(saved.rotation)
savedPieces[guid] = nil
end
end
for _, saved in pairs(savedPieces) do
-- Any saved pieces we haven't yet restored were deleted; re-spawn them.
spawnObjectData(saved)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment