Detecting first start of app/game.

I am developing a game using Corona SDK. For some reason I need to detect the first time the app starts. I searched for tutorials regarding this topic but couldn’t find any. I have no idea how I can detect the first ever session or startup of the game.

Can anybody help me out?

Thank You

Hi @playpunkgames,

Just for clarity, do you mean detecting the first time ever the app starts, and then never again? Or do you mean, detecting when the app starts running each time the user opens it?

There are, of course, solutions to either case but the approach would be very different for each case.

Best regards,

Brent

Hello sir,

I meant the first time the starts after install

Option no 1. From your reply.

Hi @playpunkgames,

The easiest way is to create a new file in the app’s local directory when it runs. This file can just be a text file with any one character inside it (the point is to later check if the file exists or not). This guide will show you how to create a new file.

https://docs.coronalabs.com/guide/data/readWriteFiles/index.html

Then, later, just check if the file exists. If it does, you know the game has been run before. See the first example near the bottom of this document:

https://docs.coronalabs.com/api/library/system/pathForFile.html

Hope this helps,

Brent

Hi Brent , where should I put the code in composer scene or should I put it in the mail.lua

This can go in main.lua. You can also enclose it inside a system event listener to detect when the application starts.

[lua]

local function onSystemEvent( event )

    if ( event.type == “applicationStart” ) then

        – file creation and checking code here

    end

    Runtime:removeEventListener( “system”, onSystemEvent )

end

Runtime:addEventListener( “system”, onSystemEvent )

[/lua]

OK … Thank you sir

And can we put the code in another file.

Corona Rocks !

Hi @playpunkgames,

Just for clarity, do you mean detecting the first time ever the app starts, and then never again? Or do you mean, detecting when the app starts running each time the user opens it?

There are, of course, solutions to either case but the approach would be very different for each case.

Best regards,

Brent

Hello sir,

I meant the first time the starts after install

Option no 1. From your reply.

Hi @playpunkgames,

The easiest way is to create a new file in the app’s local directory when it runs. This file can just be a text file with any one character inside it (the point is to later check if the file exists or not). This guide will show you how to create a new file.

https://docs.coronalabs.com/guide/data/readWriteFiles/index.html

Then, later, just check if the file exists. If it does, you know the game has been run before. See the first example near the bottom of this document:

https://docs.coronalabs.com/api/library/system/pathForFile.html

Hope this helps,

Brent

Hi Brent , where should I put the code in composer scene or should I put it in the mail.lua

This can go in main.lua. You can also enclose it inside a system event listener to detect when the application starts.

[lua]

local function onSystemEvent( event )

    if ( event.type == “applicationStart” ) then

        – file creation and checking code here

    end

    Runtime:removeEventListener( “system”, onSystemEvent )

end

Runtime:addEventListener( “system”, onSystemEvent )

[/lua]

OK … Thank you sir

And can we put the code in another file.

Corona Rocks !