Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Game Code Logic, Initial New Start vs Continued Start
Started by AngelaMcCall Nov 29 2019 02:03 PM

3 replies to this topic

Best Answer AngelaMcCall , 30 November 2019 - 03:23 AM

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.

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

AngelaMcCall

[GLOBAL: userInfoPane.html]
AngelaMcCall
  • Enthusiast

  • 33 posts
  • Corona SDK

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 

 

 

 



[TOPIC: post.html]
#2

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,867 posts
  • Corona SDK

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()
 


[TOPIC: post.html]
#3

AngelaMcCall

[GLOBAL: userInfoPane.html]
AngelaMcCall
  • Enthusiast

  • 33 posts
  • Corona SDK

  Best Answer

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.



[TOPIC: post.html]
#4

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Corona Geek

  • 1,020 posts
  • Corona SDK

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. :D


  • AngelaMcCall likes this


[topic_controls]
[/topic_controls]