Save after object remove

How to save after object remove then when relaunch the object will not display again ?

main.lua

local loadsave = require("loadsave") myTable = loadsave.loadTable("Table.json") if myTable ==nil then myTable = {} loadsave.saveTable(myTable, "Table.json") else end local arr = {} function onTouch(e) if e.phase == "began" then e.target:removeSelf() loadsave.saveTable(myTable, "Table.json") end end for i = 0 ,4 do arr[i] ={} for j = 0,4 do arr[i][j] = display.newRect(0,0,40,40) arr[i][j].x = i\*44+60 arr[i][j].y = j\*44+100 arr[i][j]:addEventListener("touch",onTouch) end end

loadsave.lua

local \_ = {} local json = require("json") local DefaultLocation = system.DocumentsDirectory local RealDefaultLocation = DefaultLocation local ValidLocations = { [system.DocumentsDirectory] = true, [system.CachesDirectory] = true, [system.TemporaryDirectory] = true } function \_.saveTable(t, filename, location) if location and (not ValidLocations[location]) then error("Attempted to save a table to an invalid location", 2) elseif not location then location = DefaultLocation end local path = system.pathForFile( filename, location) local file = io.open(path, "w") if file then local contents = json.encode(t) file:write( contents ) io.close( file ) return true else return false end end function \_.loadTable(filename, location) if location and (not ValidLocations[location]) then error("Attempted to load a table from an invalid location", 2) elseif not location then location = DefaultLocation end local path = system.pathForFile( filename, location) local contents = "" local myTable = {} local file = io.open( path, "r" ) if file then -- read all contents of file into a string local contents = file:read( "\*a" ) myTable = json.decode(contents); io.close( file ) return myTable end return nil end function \_.changeDefault(location) if location and (not location) then error("Attempted to change the default location to an invalid location", 2) elseif not location then location = RealDefaultLocation end DefaultLocation = location return true end return \_

This is what I do is easy to follow

ok first make a separate lua.

Lets call it block.lua (you can name it anything you want) after you made the file. Copy and paste the code below.

local json = require "json" local State = {} local path = system.pathForFile( "block.txt", system.DocumentsDirectory ) --You can rename the .txt file to anything local file = io.open( path, "r" ) if file then -- read all contents of file into a string local contents = file:read( "\*a" ) State = json.decode(contents); io.close( file ) else --SET DEFAULT VALUES HERE State.someDefaultValue = "Anything, can be tables too" end local function SaveState(event) if( event==nil or event.type ~= "applicationStart" ) then local path = system.pathForFile( "block.txt", system.DocumentsDirectory ) --You can rename the .txt file to anthing local file = io.open( path, "w" ) if file then file:write( json.encode(State) ) io.close( file ) end end end Runtime:addEventListener( "system", SaveState ); return State

in main.lua file put this

object = require("block") --you can change object to anything you want

Now in your file where your using the block put this code.

if object == nil then object = 0 end

With this you can make a function with if block == 0 block appears. But when block == 1 it will never appear. When you relunch the item will never appear, because of the number being saved.

EDIT: I made another reply please disregard this entire reply, my next reply suits the need to have the item stay removed after it has been removed.

I don’t know how to use , can you show me how to put this code in my main.lua

local loadsave = require("loadsave") myTable = loadsave.loadTable("Table.json") if myTable ==nil then myTable = {} loadsave.saveTable(myTable, "Table.json") else end local arr = {} function onTouch(e) if e.phase == "began" then e.target:removeSelf() end end for i = 0 ,4 do arr[i] ={} for j = 0,4 do arr[i][j] = display.newRect(0,0,40,40) arr[i][j].x = i\*44+60 arr[i][j].y = j\*44+100 arr[i][j]:addEventListener("touch",onTouch) end end

Thank you.

Sorry for late respone. I’m giving you another way to save when the item is destroyed. Please disregard my last post. You can still use the other saving method for other needs. Is still really useful. With your code I would modify it like this.

ego = require "ego" --This goes in main.lua at the bottom before main() saveFile = ego.saveFile --This goes in main.lua at the bottom before main() loadFile = ego.loadFile --This goes in main.lua at the bottom before main() boxload = loadFile("boxload.txt") local boxy = {} disbox = {} disbox[1]={ x= 50, y=200} disbox[2]={ x=100, y=200} disbox[3]={ x=150, y=200} disbox[4]={x= 200, y=200} for i = 1, 4 do boxy[i] = display.newRect(0,0,40,40) boxy[i].x = disbox[i].x boxy[i].y = disbox[i].y if boxload == "dead1" then boxy[1].alpha = 0 end end print(boxload) local function destroy() display.remove(boxy[1]) saveFile("boxload.txt", "dead1") end boxy[1]:addEventListener("touch", destroy)

Now if you want to remove more boxes just simply add boxload2, boxload3 ect… each having their own saved file for the item to stay removed.

Make a file call ego and copy this

module (..., package.seeall) function saveFile( fileName, fileData ) local path = system.pathForFile( fileName, system.DocumentsDirectory ) local file = io.open( path, "w+" ) if file then file:write( fileData ) io.close( file ) end end function loadFile( fileName ) local path = system.pathForFile( fileName, system.DocumentsDirectory ) local file = io.open( path, "r" ) if file then local fileData = file:read( "\*a" ) io.close( file ) return fileData else file = io.open( path, "w" ) file:write( "N/A" ) io.close( file ) return "N/A" end end

This is what I do is easy to follow

ok first make a separate lua.

Lets call it block.lua (you can name it anything you want) after you made the file. Copy and paste the code below.

local json = require "json" local State = {} local path = system.pathForFile( "block.txt", system.DocumentsDirectory ) --You can rename the .txt file to anything local file = io.open( path, "r" ) if file then -- read all contents of file into a string local contents = file:read( "\*a" ) State = json.decode(contents); io.close( file ) else --SET DEFAULT VALUES HERE State.someDefaultValue = "Anything, can be tables too" end local function SaveState(event) if( event==nil or event.type ~= "applicationStart" ) then local path = system.pathForFile( "block.txt", system.DocumentsDirectory ) --You can rename the .txt file to anthing local file = io.open( path, "w" ) if file then file:write( json.encode(State) ) io.close( file ) end end end Runtime:addEventListener( "system", SaveState ); return State

in main.lua file put this

object = require("block") --you can change object to anything you want

Now in your file where your using the block put this code.

if object == nil then object = 0 end

With this you can make a function with if block == 0 block appears. But when block == 1 it will never appear. When you relunch the item will never appear, because of the number being saved.

EDIT: I made another reply please disregard this entire reply, my next reply suits the need to have the item stay removed after it has been removed.

I don’t know how to use , can you show me how to put this code in my main.lua

local loadsave = require("loadsave") myTable = loadsave.loadTable("Table.json") if myTable ==nil then myTable = {} loadsave.saveTable(myTable, "Table.json") else end local arr = {} function onTouch(e) if e.phase == "began" then e.target:removeSelf() end end for i = 0 ,4 do arr[i] ={} for j = 0,4 do arr[i][j] = display.newRect(0,0,40,40) arr[i][j].x = i\*44+60 arr[i][j].y = j\*44+100 arr[i][j]:addEventListener("touch",onTouch) end end

Thank you.

Sorry for late respone. I’m giving you another way to save when the item is destroyed. Please disregard my last post. You can still use the other saving method for other needs. Is still really useful. With your code I would modify it like this.

ego = require "ego" --This goes in main.lua at the bottom before main() saveFile = ego.saveFile --This goes in main.lua at the bottom before main() loadFile = ego.loadFile --This goes in main.lua at the bottom before main() boxload = loadFile("boxload.txt") local boxy = {} disbox = {} disbox[1]={ x= 50, y=200} disbox[2]={ x=100, y=200} disbox[3]={ x=150, y=200} disbox[4]={x= 200, y=200} for i = 1, 4 do boxy[i] = display.newRect(0,0,40,40) boxy[i].x = disbox[i].x boxy[i].y = disbox[i].y if boxload == "dead1" then boxy[1].alpha = 0 end end print(boxload) local function destroy() display.remove(boxy[1]) saveFile("boxload.txt", "dead1") end boxy[1]:addEventListener("touch", destroy)

Now if you want to remove more boxes just simply add boxload2, boxload3 ect… each having their own saved file for the item to stay removed.

Make a file call ego and copy this

module (..., package.seeall) function saveFile( fileName, fileData ) local path = system.pathForFile( fileName, system.DocumentsDirectory ) local file = io.open( path, "w+" ) if file then file:write( fileData ) io.close( file ) end end function loadFile( fileName ) local path = system.pathForFile( fileName, system.DocumentsDirectory ) local file = io.open( path, "r" ) if file then local fileData = file:read( "\*a" ) io.close( file ) return fileData else file = io.open( path, "w" ) file:write( "N/A" ) io.close( file ) return "N/A" end end