Only on time

Hello everyone! I don’t know if anyone’s noticed, but I feel like I’ve been asking a lot of questions lately. I want to thank everyone for all their help though. I REALLY appreciate it! Thanks again.

Anyway, I wan’t my app to display an image and do some stuff on the first launch and ONLY the first launch. How would you, or how do you, do this? I’m using ICE in my project, so if you want you can answer using ICE if it’s needed. Thanks you!

Nathan [import]uid: 39302 topic_id: 26364 reply_id: 326364[/import]

Hi, I am using a sqlite database for all my stuff and I took a quick look at Ice, and correct me if I am wrong. Ice seems more like a scoring system and you don’t get that control as you do with a database. A database is more powerful if you know how to write sql queries. You can do magic with a db.

But for your question, you could either do it in two ways. A database or a simple textfile. I give you a database sample of code here.

require "sqlite3";  
  
--Opens the database or creates it if it doesn't exists  
local path = system.pathForFile("My\_game\_database.db",system.DocumentsDirectory);  
db = sqlite3.open( path );   
  
--Setup the table for userdata if it doesn't exist - the first time  
local tablesetup = [[CREATE TABLE IF NOT EXISTS gamedata (id INTEGER PRIMARY KEY, opened INTEGER);]];  
db:exec( tablesetup );  
  
--Check if gamesettings is empty, if so fill it up with defaults  
for row in db:nrows("SELECT max(id) as RET FROM gamedata") do  
 if not (row.RET) then  
 local tablesetup = [[INSERT INTO gamedata (opened) VALUES(0);]];  
 db:exec( tablesetup );  
 isOpened = 0;  
 else  
 for row in db:nrows("SELECT opened FROM gamedata") do   
  
 isOpened = row.opened;  
 end  
 end  
end  
  
if isOpened == 0 then  
 -- Show your message the first time here :)  
end  
--Update the database so that we know how many times it has been opened  
local tablesetup = [[UPDATE gamedata set opened = opened+1;]];  
db:exec( tablesetup );  

This code snippet creates a database for you and if you want to delete it, you will find it in the projects sandbox - document folder.

Joakim [import]uid: 81188 topic_id: 26364 reply_id: 106893[/import]

Have you looked at Peach’s Ego system? (http://techority.com/2011/12/28/ego-easy-saving-and-loading-in-your-corona-apps/)

I would use this if you only need one process to determine if the app has been opened before. It will create a very small text file that should not interfere with performance. How you can use this is create a file that saves a number 1 as soon as the image you want loaded is displayed. Then you have a condition that checks if the file’s number is < 1. If it is, the image has never been displayed, if not, the image has been displayed and will not display again. That’s my pseudo code, here’s an example:

[lua]
local ego = require(“ego”)
local saveFile = ego.saveFile
local loadFile = ego.loadFile

–Create appOpenedBefore.txt
local isFirstRun = loadFile (“appOpenedBefore.txt”)

–If the file is empty (this means it is the first time you’ve run the app) save it as 0
–The second time this is run, isFirstRun will not be “empty” and you will not save isFirstRun to 0
local function checkForFile ()
if isFirstRun == “empty” then
isFirstRun = 0
saveFile(“appOpenedBefore.txt”, isFirstRun)
end
end
checkForFile()

–Then you need your image function to be run IF this is the first time the app has been opened
–Make sure to parse isFirstRun to a number or else it will be read as a string and you will get
–errors
if (tonumber(isFirstRun) < 1) then
–Run the image code and set isFirstRun to 1 and SAVE
isFirstRun = 1;
saveFile(“appOpenedBefore.txt”, isFirstRun)
end[/lua]

After this is run once, isFirstRun will be set to 1 and the if statement with the image code will never run again. I hope this helps you out! [import]uid: 50511 topic_id: 26364 reply_id: 106896[/import]

Do I need to do something in the ego.lua file? [import]uid: 39302 topic_id: 26364 reply_id: 106918[/import]

Nevermind. I got it. :slight_smile: Thank you so much! pretend like I’m thanking you for the rest of the day… :stuck_out_tongue: [import]uid: 39302 topic_id: 26364 reply_id: 106920[/import]

Your welcome man! Hope it helps out! [import]uid: 50511 topic_id: 26364 reply_id: 108327[/import]

You should be able to do this with Ice like so:

[code]

local box = ice:loadBox( “someName” )

if box:hasValue( “appHasBeenRanBefore” ) then – App has been ran before

else – App hasn’t been ran before

box:store( “appHasBeenRanBefore”, true ) – Flag that the app has now been ran
box:save()

– Display image here and whatever else you want to do on first load

end

[/code] [import]uid: 5833 topic_id: 26364 reply_id: 108336[/import]