I was wondering would I use this https://docs.coronalabs.com/api/event/system/type.html or what would I use to display something every 3rd time my app has launched?
You need to save a value out to a file in system.DocumentsDirectory that keeps a count of how many times the app has been started. Corona has a system event that triggers on start and on resume (see: https://docs.coronalabs.com/api/event/system/index.html)..)
When you get one of those, read the file, increment the value. If it’s 3, reset the value to 0 and save the file back out and then do whatever you want to do on every third launch.
Rob
Would something like this work?
local systemLaunch = 0 local function onSystemEvent( event ) if event.type == "applicationStart" then systemLaunch = 1 elseif systemLaunch == 1 then systemLaunch = 2 elseif systemLaunch == 2 then systemLaunch = 3 elseif systemLaunch == 3 then -- Show Pop up systemLaunch = 0 end end Runtime:addEventListener( “system”, onSystemEvent )
No, I would do something like this:
local systemLaunch = 0 local function onSystemEvent( event ) if event.type == "applicationStart" or event.type == "applicationResume" then systemLaunch = systemLaunch + 1 if systemLaunch \>= 3 then systemLaunch = 0 -- Show Pop up end end end Runtime:addEventListener( “system”, onSystemEvent )
But that’s not enough. You have to save the variable systemLaunch out to a file everytime you update it. You also have to read the value back in to start:
local systemLaunch local function onSystemEvent( event ) if event.type == "applicationStart" or event.type == "applicationResume" then -- load your file systemLaunch = valueFromFile + 1 if systemLaunch \>= 3 then systemLaunch = 0 -- Show Pop up -- save the value of systemLaunch to a file end end end Runtime:addEventListener( “system”, onSystemEvent )
Hi Rob,
When I try using the code, I get an error “valueFromFile” a nil value. What should I replace valueFromFile with?
I’m not giving you working code on purpose. I don’t know your app. I don’t know the best way for “YOU” to save the data. I don’t know if you’re using a settings table to hold those values or if you want to save and load the one value in it’s own file or not. You have to make those decisions yourself.
“valueFromFile” isn’t a real variable, it’s something you are supposed to figure out from when you read the saved data.
Rob
Well, if I use this:
local function onSystemEvent( event ) if event.type == "applicationStart" or event.type == "applicationResume" then -- load your file systemLaunch = systemLaunch + 1 if systemLaunch \>= 3 then systemLaunch = 0 -- Show Pop up -- save the value of systemLaunch to a file end end end Runtime:addEventListener( "system" , onSystemEvent )
Will it keep systemLaunch on File, or will it restart at zero. I want to display a pop up that reminds a user about the ‘pro’ version.
systemLaunch is a variable. It will go away when your app exits. As long as you’re just suspended and resume, it will still be there, but if your app exits, the variable will go away and when the user launches the app again, your value will be 0.
If you want this to work you will have to learn how to save data to a file and read it back in. There are many ways to do this. Look through our tutorials, search the forums for “save data”, and such.
Rob
Would this work?
from here https://github.com/robmiracle/Simple-Table-Load-Save-Functions-for-Corona-SDK
display.setStatusBar( display.HiddenStatusBar ) local loadsave = require("loadsave") local systemFile = {} systemFile.number = 0 loadsave.saveTable(systemFile, "myTable.json", system.DocumentsDirectory) newTable = loadsave.loadTable("myTable.json", system.DocumentsDirectory) loadsave.printTable(newTable) local function onSystemEvent( event ) if event.type == "applicationStart" or event.type == "applicationResume" then -- load your file systemLaunch = systemFile.number + 1 if systemLaunch \>= 3 then systemLaunch = 0 -- Show Pop up -- save the value of systemLaunch to a file end end end Runtime:addEventListener( "system" , onSystemEvent )
Close…
display.setStatusBar( display.HiddenStatusBar ) local loadsave = require("loadsave") local settings settings = loadsave.loadTable( "myTable.json", system.DocumentsDirectory ) if settings == nil then settings = {} settings.launchCount = 0 loadsve.saveTable( settings, "myTable.json", system.DocumentsDirectory ) end local function onSystemEvent( event ) if event.type == "applicationStart" or event.type == "applicationResume" then -- load your file settings.launchCount = settings.launchCount + 1 if settings.launchCount \>= 3 then settings.launchCount = 0 loadsave.saveTable( settings, "myTable.json", system.DocumentsDirectory ) -- Show Pop up end end end Runtime:addEventListener( "system" , onSystemEvent )
It’s always safest to write the table out any time you change it’s values. If someone force quits your app, the settings won’t be saved, so writing immediately works best. You only need to read the table on app first start, after that the table stays in memory.
Rob
You need to save a value out to a file in system.DocumentsDirectory that keeps a count of how many times the app has been started. Corona has a system event that triggers on start and on resume (see: https://docs.coronalabs.com/api/event/system/index.html)..)
When you get one of those, read the file, increment the value. If it’s 3, reset the value to 0 and save the file back out and then do whatever you want to do on every third launch.
Rob
Would something like this work?
local systemLaunch = 0 local function onSystemEvent( event ) if event.type == "applicationStart" then systemLaunch = 1 elseif systemLaunch == 1 then systemLaunch = 2 elseif systemLaunch == 2 then systemLaunch = 3 elseif systemLaunch == 3 then -- Show Pop up systemLaunch = 0 end end Runtime:addEventListener( “system”, onSystemEvent )
No, I would do something like this:
local systemLaunch = 0 local function onSystemEvent( event ) if event.type == "applicationStart" or event.type == "applicationResume" then systemLaunch = systemLaunch + 1 if systemLaunch \>= 3 then systemLaunch = 0 -- Show Pop up end end end Runtime:addEventListener( “system”, onSystemEvent )
But that’s not enough. You have to save the variable systemLaunch out to a file everytime you update it. You also have to read the value back in to start:
local systemLaunch local function onSystemEvent( event ) if event.type == "applicationStart" or event.type == "applicationResume" then -- load your file systemLaunch = valueFromFile + 1 if systemLaunch \>= 3 then systemLaunch = 0 -- Show Pop up -- save the value of systemLaunch to a file end end end Runtime:addEventListener( “system”, onSystemEvent )
Hi Rob,
When I try using the code, I get an error “valueFromFile” a nil value. What should I replace valueFromFile with?
I’m not giving you working code on purpose. I don’t know your app. I don’t know the best way for “YOU” to save the data. I don’t know if you’re using a settings table to hold those values or if you want to save and load the one value in it’s own file or not. You have to make those decisions yourself.
“valueFromFile” isn’t a real variable, it’s something you are supposed to figure out from when you read the saved data.
Rob
Well, if I use this:
local function onSystemEvent( event ) if event.type == "applicationStart" or event.type == "applicationResume" then -- load your file systemLaunch = systemLaunch + 1 if systemLaunch \>= 3 then systemLaunch = 0 -- Show Pop up -- save the value of systemLaunch to a file end end end Runtime:addEventListener( "system" , onSystemEvent )
Will it keep systemLaunch on File, or will it restart at zero. I want to display a pop up that reminds a user about the ‘pro’ version.
systemLaunch is a variable. It will go away when your app exits. As long as you’re just suspended and resume, it will still be there, but if your app exits, the variable will go away and when the user launches the app again, your value will be 0.
If you want this to work you will have to learn how to save data to a file and read it back in. There are many ways to do this. Look through our tutorials, search the forums for “save data”, and such.
Rob
Would this work?
from here https://github.com/robmiracle/Simple-Table-Load-Save-Functions-for-Corona-SDK
display.setStatusBar( display.HiddenStatusBar ) local loadsave = require("loadsave") local systemFile = {} systemFile.number = 0 loadsave.saveTable(systemFile, "myTable.json", system.DocumentsDirectory) newTable = loadsave.loadTable("myTable.json", system.DocumentsDirectory) loadsave.printTable(newTable) local function onSystemEvent( event ) if event.type == "applicationStart" or event.type == "applicationResume" then -- load your file systemLaunch = systemFile.number + 1 if systemLaunch \>= 3 then systemLaunch = 0 -- Show Pop up -- save the value of systemLaunch to a file end end end Runtime:addEventListener( "system" , onSystemEvent )
Close…
display.setStatusBar( display.HiddenStatusBar ) local loadsave = require("loadsave") local settings settings = loadsave.loadTable( "myTable.json", system.DocumentsDirectory ) if settings == nil then settings = {} settings.launchCount = 0 loadsve.saveTable( settings, "myTable.json", system.DocumentsDirectory ) end local function onSystemEvent( event ) if event.type == "applicationStart" or event.type == "applicationResume" then -- load your file settings.launchCount = settings.launchCount + 1 if settings.launchCount \>= 3 then settings.launchCount = 0 loadsave.saveTable( settings, "myTable.json", system.DocumentsDirectory ) -- Show Pop up end end end Runtime:addEventListener( "system" , onSystemEvent )
It’s always safest to write the table out any time you change it’s values. If someone force quits your app, the settings won’t be saved, so writing immediately works best. You only need to read the table on app first start, after that the table stays in memory.
Rob