Game Code Logic, Initial New Start vs Continued Start

Hi all,

Can someone please help me with some logic, I feel like I am stuck in a chicken and egg situation and getting myself confused.

At the moment my JSON files that store current game status are being created and populated as and when needed, which means if you play my game from start to finish in one continuous flow. Everything works fine and dandy.

However, in the event that the player quits the app and does not play it for a while. Whilst the game is loading I intend to show a 2 second splashscreen so I can invoke a function to read in the current game settings.

EG main.lua > splashScreen.lua > mainMenu.lua

Therefore my code NEEDS the files to exist to read them in, even such files are empty, yet my game has not created them yet and so is erroring.

So I am getting stuck.

I have never attempted anything like this before so on a massive learning curve and I apologise if the answer is simple but I’m missing it.

As part of the game files, do I create “empty” files and store them as part of the app, instead of having the app create them as and when needed. If the fact the file is empty still likely to cause me a problem, if I was to populated them with a term such as “NEW GAME”, I could run a if statement to determine things. Would that work.

At the moment I keep getting errors when “checking” if a file exists. I thought I could manage them in an if statement but the code is causing an exception before that is being handled.

I just don’t know the solution.
Thanks in advance.

Angela 

You’re going down the right path, you just need to execute it successfully. All you need to do is check the file exists, and if not, create it:

      local filename = "myJsonFile.json"     local path = system.pathForFile(filename, system.DocumentsDirectory)     local fileExists = io.open(path, "r")     if (fileExists == nil) then     createMyFile()   else  fileExists:close() end     loadMyFile()  

Hi Nick,

Thanks for your help.

You solved the problem, I was following the tutorial:
https://docs.coronalabs.com/tutorial/data/jsonSaveLoad/index.html

in the tutorials load section they and lines of…

47 to 49 and this if condition was constantly erroring

they had…

if not file then         -- Error occurred; output the cause         print( "File error: " .. errorString )

I was just not getting anywhere.

I think the moral here is not to code, late on a Friday night, tired and with kids in the house… I should have been able to figure this out. 

Thank you.

I personally prefer to just use conditions such as “if file then” and “if not file then”. If I’m not mistaken, then on top of being easier to read, they also run faster than conditions such as “if file ~= nil then”.

But, I’m mixed when it comes to programming late into the night. Some times I come up with some ingenious things, other times I just spend three hours trying to debug a nil variable that I fix in 3 minutes the next morning. Madness is a method and it has its pros and cons. :smiley: