Skip to content
Download

Keep data between rooms

It spawns once, in the first room, and exists in all of them. It is the place for score, lives and game state.

  1. Create an object (Controle) with the variables moedas and vidas.
  2. In Project › Project settings… › Global objects, click + Global object and pick it.
self:onTouchStart("Player", function()
local controle = instance("Controle")
controle.moedas = controle.moedas + 1
self:destroy()
end)

Check Persistent in the object Inspector. The instance moves to the next room with its variables, timers and position.

  • restart() does not recreate a global or persistent object: reset the values in code when restarting.
  • A persistent object arrives in the new room at the same x and y.
  • To keep values after the game is closed, use Save.
  • C++: do not keep a pointer to another instance across rooms; use a Handle.