59 posts
Posted 01 March 2013 - 12:38 PM
I need a file that holds a string that represents a boolean but I think I am doing something wrong.
Here is my code anyone got a better way to do it?
local firstTime = io.open("/firstTime")
local isFirstTime = firstTime:read()
local nameStr = ""
if isFirstTime == "true" then
firstTime:write("false")
nameStr = "New Game"
else
nameStr = "Start Game"
end
firstTime:close()
1688 posts
Location
'MURICA
Posted 01 March 2013 - 01:08 PM
If it's the first time, would the file exist in the first place?
Perhaps, if you're storing some sort of configuration, you could just check if the configuration file exists, and if not, it's the first time.
59 posts
Posted 01 March 2013 - 01:17 PM
That is a much better idea….. Thanks alot!
2151 posts
Location
Auckland, New Zealand
Posted 01 March 2013 - 01:19 PM
If it's the first time, would the file exist in the first place?
Perhaps, if you're storing some sort of configuration, you could just check if the configuration file exists, and if not, it's the first time.
If the file doesn't exist it crashes, attempt to index nil.
Darkroom, your problem is in the first line.
You aren't specifying a mode to use, so by default it's using 'r' (take a look t
http://lua-users.org/wiki/IoLibraryTutorial).
You can't write using more 'r'.
local firstTime = io.open("/firstTime")
87 posts
Location
Basement
Posted 02 March 2013 - 02:43 PM
You have true as "true", should just be true.
2005 posts
Posted 03 March 2013 - 12:05 AM
No, firstTime:read() returns a string (if it works at all).
But as mentioned, just using "if firstTime then" works fine for checking if the configuration file has been written yet, if you try to open it in read mode.